Class: ChefDK::CLI
- Inherits:
-
Object
show all
- Includes:
- Chef::Mixin::ShellOut, Helpers, Mixlib::CLI
- Defined in:
- lib/chef-dk/cli.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
#chefdk_home, #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
55
56
57
58
|
# File 'lib/chef-dk/cli.rb', line 55
def initialize(argv)
@argv = argv
super() end
|
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
53
54
55
|
# File 'lib/chef-dk/cli.rb', line 53
def argv
@argv
end
|
Instance Method Details
#commands_map ⇒ Object
126
127
128
|
# File 'lib/chef-dk/cli.rb', line 126
def commands_map
ChefDK.commands_map
end
|
#exit(n) ⇒ Object
122
123
124
|
# File 'lib/chef-dk/cli.rb', line 122
def exit(n)
Kernel.exit(n)
end
|
#handle_options ⇒ Object
If no subcommand is given, then this class is handling the CLI request.
87
88
89
90
91
92
93
94
95
|
# File 'lib/chef-dk/cli.rb', line 87
def handle_options
parse_options(argv)
if config[:version]
show_version
else
show_help
end
exit 0
end
|
#have_command?(name) ⇒ Boolean
130
131
132
|
# File 'lib/chef-dk/cli.rb', line 130
def have_command?(name)
commands_map.have_command?(name)
end
|
#instantiate_subcommand(name) ⇒ Object
146
147
148
|
# File 'lib/chef-dk/cli.rb', line 146
def instantiate_subcommand(name)
commands_map.instantiate(name)
end
|
#option?(param) ⇒ Boolean
142
143
144
|
# File 'lib/chef-dk/cli.rb', line 142
def option?(param)
param =~ /^-/
end
|
#run ⇒ Object
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/chef-dk/cli.rb', line 60
def run
sanity_check!
subcommand_name, *subcommand_params = argv
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
rescue OptionParser::InvalidOption => e
err(e.message)
show_help
exit 1
end
|
#show_help ⇒ Object
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/chef-dk/cli.rb', line 111
def show_help
msg(banner)
msg("\nAvailable Commands:")
justify_length = subcommands.map(&:length).max + 2
subcommand_specs.each do |name, spec|
next if spec.hidden
msg(" #{"#{name}".ljust(justify_length)}#{spec.description}")
end
end
|
#show_version ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/chef-dk/cli.rb', line 97
def show_version
msg("Chef Development Kit Version: #{ChefDK::VERSION}")
["chef-client", "delivery", "berks", "kitchen", "inspec"].each do |component|
result = Bundler.with_clean_env { shell_out("#{component} --version") }
if result.exitstatus != 0
msg("#{component} version: ERROR")
else
version = result.stdout.lines.first.scan(/(?:master\s)?[\d+\.\(\)]+\S+/).join("\s")
msg("#{component} version: #{version}")
end
end
end
|
#subcommand_specs ⇒ Object
138
139
140
|
# File 'lib/chef-dk/cli.rb', line 138
def subcommand_specs
commands_map.command_specs
end
|
#subcommands ⇒ Object
134
135
136
|
# File 'lib/chef-dk/cli.rb', line 134
def subcommands
commands_map.command_names
end
|