Class: Stove::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ Cli

Returns a new instance of Cli.



6
7
8
# File 'lib/stove/cli.rb', line 6

def initialize(argv, stdin=STDIN, stdout=STDOUT, stderr=STDERR, kernel=Kernel)
  @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
end

Instance Method Details

#execute!Object



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/stove/cli.rb', line 10

def execute!
  $stdout, $stderr = @stdout, @stderr

  # Parse the options hash
  option_parser.parse!(@argv)

  # Login command
  if @argv.first == 'login'
    if options[:username].nil? || options[:username].to_s.strip.empty?
      raise "Missing argument `--username'!"
    end

    if options[:key].nil? || options[:key].to_s.strip.empty?
      raise "Missing argument `--key'!"
    end

    Config.username = options[:username]
    Config.key      = options[:key]
    Config.endpoint = options[:endpoint] unless options[:endpoint].nil?
    Config.save

    @stdout.puts "Successfully saved config to `#{Config.__path__}'!"
    @kernel.exit(0)
    return
  end

  # Override configs
  Config.endpoint        = options[:endpoint]        if options[:endpoint]
  Config.username        = options[:username]        if options[:username]
  Config.key             = options[:key]             if options[:key]
  Config.artifactory     = options[:artifactory]     if options[:artifactory]
  Config.artifactory_key = options[:artifactory_key] if options[:artifactory_key]
  Config.ssl_verify      = options[:ssl_verify]

  # Set the log level
  Stove.log_level = options[:log_level]

  # Useful debugging output for when people type the wrong fucking command
  # and then open an issue like it's somehow my fault
  Stove::Log.info("Options: #{options.inspect}")
  Stove::Log.info("ARGV: #{@argv.inspect}")

  # Make a new cookbook object - this will raise an exception if there is
  # no cookbook at the given path
  cookbook = Cookbook.new(options[:path])

  # Now execute the actual runners (validations and errors might occur)
  runner = Runner.new(cookbook, options)
  runner.run

  # If we got this far, everything was successful :)
  @kernel.exit(0)
rescue => e
  Stove::Log.init($stderr)
  Stove::Log.error('Stove experienced an error!')
  Stove::Log.error(e.class.name)
  Stove::Log.error(e.message)
  Stove::Log.error(e.backtrace.join("\n"))

  @kernel.exit(e.respond_to?(:exit_code) ? e.exit_code : 500)
ensure
  $stdout, $stderr = STDOUT, STDERR
end