Class: ChefDK::CLI

Inherits:
Object
  • Object
show all
Includes:
Helpers, Mixlib::CLI
Defined in:
lib/chef-dk/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#err, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_root, #stderr, #stdout, #system_command

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



50
51
52
53
# File 'lib/chef-dk/cli.rb', line 50

def initialize(argv)
  @argv = argv
  super() # mixlib-cli #initialize doesn't allow arguments
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



48
49
50
# File 'lib/chef-dk/cli.rb', line 48

def argv
  @argv
end

Instance Method Details

#commands_mapObject



100
101
102
# File 'lib/chef-dk/cli.rb', line 100

def commands_map
  ChefDK.commands_map
end

#exit(n) ⇒ Object



96
97
98
# File 'lib/chef-dk/cli.rb', line 96

def exit(n)
  Kernel.exit(n)
end

#handle_optionsObject

If no subcommand is given, then this class is handling the CLI request.



76
77
78
79
80
81
82
83
84
# File 'lib/chef-dk/cli.rb', line 76

def handle_options
  parse_options(argv)
  if config[:version]
    msg("Chef Development Kit Version: #{ChefDK::VERSION}")
  else
    show_help
  end
  exit 0
end

#have_command?(name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/chef-dk/cli.rb', line 104

def have_command?(name)
  commands_map.have_command?(name)
end

#instantiate_subcommand(name) ⇒ Object



120
121
122
# File 'lib/chef-dk/cli.rb', line 120

def instantiate_subcommand(name)
  commands_map.instantiate(name)
end

#option?(param) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/chef-dk/cli.rb', line 116

def option?(param)
  param =~ /^-/
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef-dk/cli.rb', line 55

def run
  subcommand_name, *subcommand_params = argv

  #
  # Runs the appropriate subcommand if the given parameters contain any
  # subcommands.
  #
  if subcommand_name.nil? || option?(subcommand_name)
    handle_options
  elsif have_command?(subcommand_name)
    subcommand = instantiate_subcommand(subcommand_name)
    exit_code = subcommand.run_with_default_options(subcommand_params)
    exit normalized_exit_code(exit_code)
  else
    err "Unknown command `#{subcommand_name}'."
    show_help
    exit 1
  end
end

#show_helpObject



86
87
88
89
90
91
92
93
94
# File 'lib/chef-dk/cli.rb', line 86

def show_help
  msg(banner)
  msg("\nAvailable Commands:")

  justify_length = subcommands.map(&:length).max + 2
  subcommand_specs.each do |name, spec|
    msg("    #{"#{name}".ljust(justify_length)}#{spec.description}")
  end
end

#subcommand_specsObject



112
113
114
# File 'lib/chef-dk/cli.rb', line 112

def subcommand_specs
  commands_map.command_specs
end

#subcommandsObject



108
109
110
# File 'lib/chef-dk/cli.rb', line 108

def subcommands
  commands_map.command_names
end