Class: AllInRuby::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/all_in_ruby/cli.rb

Constant Summary collapse

COMMANDS =
["install", "uninstall", "status"].freeze
STATUS_LABELS =
{
  created: "created",
  updated: "instructions added",
  already_installed: "already installed, left untouched",
  removed: "instructions removed",
  not_installed: "not installed",
  installed: "installed"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(stdout: $stdout, stderr: $stderr, installer: Installer.new) ⇒ CLI

Returns a new instance of CLI.



16
17
18
19
20
# File 'lib/all_in_ruby/cli.rb', line 16

def initialize(stdout: $stdout, stderr: $stderr, installer: Installer.new)
  @stdout = stdout
  @stderr = stderr
  @installer = installer
end

Instance Method Details

#run(argv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/all_in_ruby/cli.rb', line 22

def run(argv)
  options = {agents: Installer::AGENTS.dup, scope: "local"}
  parser = build_parser(options)
  parser.parse!(argv)

  command = argv.shift
  unless COMMANDS.include?(command)
    @stderr.puts parser
    return 1
  end

  results = @installer.public_send(command, agents: options[:agents], scope: options[:scope])
  results.each do |result|
    @stdout.puts "#{result.agent.ljust(6)} #{result.path}: #{STATUS_LABELS.fetch(result.status)}"
  end
  0
rescue OptionParser::ParseError => e
  @stderr.puts e.message
  1
end