Class: Baker::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



14
15
16
# File 'lib/cli.rb', line 14

def initialize(args)
  @args = args.dup
end

Instance Attribute Details

#argsObject (readonly)

The array of (unparsed) command-line options



10
11
12
# File 'lib/cli.rb', line 10

def args
  @args
end

#optionsObject (readonly)

The hash of (parsed) command-line options



12
13
14
# File 'lib/cli.rb', line 12

def options
  @options
end

Class Method Details

.run(args) ⇒ Object



3
4
5
6
7
# File 'lib/cli.rb', line 3

def self.run(args)
  cli = new(args)
  cli.parse_options!
  cli.run
end

Instance Method Details

#option_parserObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cli.rb', line 18

def option_parser
  # @logger = Logger.new
  @option_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} <server>"

    opts.on("-s", "--setup", "sets up chef, must be ran once before you can run chef recipes.") do
      options[:setup] = true
    end

    opts.on("-h", "--help", "Display this help message.") do
      puts opts
      exit
    end

    opts.on("-V", "--version", "Display the baker version, and exit.") do
      puts "Baker Version #{Baker::Version}"
      exit
    end
  end
end

#parse_options!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cli.rb', line 39

def parse_options!
  # defaults
  @options = {:host => '', :setup => false}

  if args.empty?
    warn "Please specifiy the server to run the recipes on."
    warn option_parser
    exit 1
  end

  option_parser.parse!(args)
  options[:host].concat(*args)
end

#runObject



53
54
55
56
57
58
59
60
# File 'lib/cli.rb', line 53

def run
  # puts "%%% options #{options.inspect}"
  if options[:setup]
    Baker.setup(options)
  else
    Baker.run(options)
  end
end