Class: VagrantDockerHostsManager::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-docker-hosts-manager/command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-docker-hosts-manager/command.rb', line 8

def execute
  opts = {
    json: false,
    dry:  false,
    lang: nil,
    no_emoji: false,
    yes: false,
    all: false
  }

  help_topic = extract_help_topic(@argv)

  parser = OptionParser.new do |o|
    o.banner = "Usage: vagrant hosts <apply|remove|view|help|version> [options]"
    o.on("--json", "Machine-readable JSON output") { opts[:json] = true }
    o.on("--lang LANG", "Force language (en|fr)")  { |v| opts[:lang] = v }
    o.on("--no-emoji", "Disable emoji in CLI output") { opts[:no_emoji] = true }
    o.on("-y", "--yes", "Auto-confirm destructive operations") { opts[:yes] = true }
    o.on("--all", "--prune", "remove: purge ALL managed blocks (every machine, incl. orphans)") { opts[:all] = true }
  end

  argv = parse_options(parser)
  return 0 unless argv

  action = argv.shift

  Util::I18n.setup!(@env, forced: opts[:lang])
  Util::I18n.set_json_mode(opts[:json])
  ENV["VDHM_DRY_RUN"]  = "1" if opts[:dry]
  ENV["VDHM_NO_EMOJI"] = "1" if opts[:no_emoji]

  dispatch(action, argv, opts, help_topic)
rescue StandardError => e
  Util::Json.emit(action: "command", status: "error", error: e.message)
  UiHelpers.error(@env.ui, "#{UiHelpers.e(:error, no_emoji: opts[:no_emoji])} #{e.message}") unless opts[:json]
  1
end