Class: Stove::Cli

Inherits:
Object
  • Object
show all
Includes:
Logify
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.



8
9
10
# File 'lib/stove/cli.rb', line 8

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



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
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stove/cli.rb', line 12

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.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]

  # 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
  log.info("Options: #{options.inspect}")
  log.info("ARGV: #{@argv.inspect}")

  # Yank command
  if @argv.first == 'yank'
    name = @argv[1] || Cookbook.new(options[:path]).name

    if Community.yank(name)
      @stdout.puts "Successfully yanked #{name}!"
      @kernel.exit(0)
    else
      @stderr.puts "I could not find a cookbook named #{name}!"
      @kernel.exit(1)
    end

    return
  end

  # 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
  log.error('Stove experienced an error!')
  log.error(e.class.name)
  log.error(e.message)
  log.error(e.backtrace.join("\n"))

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