Class: ChefCLI::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_bin_dir, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_expand_path, #omnibus_install?, #omnibus_root, #package_home, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



60
61
62
63
# File 'lib/chef-cli/cli.rb', line 60

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.



58
59
60
# File 'lib/chef-cli/cli.rb', line 58

def argv
  @argv
end

Instance Method Details

#commands_mapObject



140
141
142
# File 'lib/chef-cli/cli.rb', line 140

def commands_map
  ChefCLI.commands_map
end

#exit(n) ⇒ Object



136
137
138
# File 'lib/chef-cli/cli.rb', line 136

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

#handle_optionsObject

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



92
93
94
95
96
97
98
99
100
# File 'lib/chef-cli/cli.rb', line 92

def handle_options
  parse_options(argv)
  if config[:version]
    show_version
  else
    show_help
  end
  exit 0
end

#have_command?(name) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/chef-cli/cli.rb', line 144

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

#instantiate_subcommand(name) ⇒ Object



167
168
169
# File 'lib/chef-cli/cli.rb', line 167

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

#option?(param) ⇒ Boolean

Is a passed parameter actually an option aka does it start with ‘-’

Parameters:

  • param (String)

    The passed parameter to check

Returns:

  • (Boolean)


163
164
165
# File 'lib/chef-cli/cli.rb', line 163

def option?(param)
  param[0] == "-"
end

#run(enforce_license: false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef-cli/cli.rb', line 65

def run(enforce_license: false)
  path_check!

  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(enforce_license, 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_helpObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef-cli/cli.rb', line 124

def show_help
  msg(banner)
  msg("Available Commands:")

  justify_length = subcommands.map(&:length).max + 2
  subcommand_specs.each do |name, spec|
    next if spec.hidden

    msg("    #{name.to_s.ljust(justify_length)}#{spec.description}")
  end
end

#show_versionObject



102
103
104
105
106
107
108
# File 'lib/chef-cli/cli.rb', line 102

def show_version
  if omnibus_install?
    show_version_via_version_manifest
  else
    msg("#{ChefCLI::Dist::CLI_PRODUCT} version: #{ChefCLI::VERSION}")
  end
end

#show_version_via_version_manifestObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chef-cli/cli.rb', line 110

def show_version_via_version_manifest
  msg("#{ChefCLI::Dist::PRODUCT} version: #{component_version("build_version")}")

  { "#{ChefCLI::Dist::INFRA_CLIENT_PRODUCT}": ChefCLI::Dist::INFRA_CLIENT_GEM,
    "#{ChefCLI::Dist::INSPEC_PRODUCT}": ChefCLI::Dist::INSPEC_CLI,
    "#{ChefCLI::Dist::CLI_PRODUCT}": ChefCLI::Dist::CLI_GEM,
    "#{ChefCLI::Dist::HAB_PRODUCT}": ChefCLI::Dist::HAB_SOFTWARE_NAME,
    "Test Kitchen": "test-kitchen",
    "Cookstyle": "cookstyle",
  }.each do |prod_name, component|
    msg("#{prod_name} version: #{component_version(component)}")
  end
end

#subcommand_specsObject



152
153
154
# File 'lib/chef-cli/cli.rb', line 152

def subcommand_specs
  commands_map.command_specs
end

#subcommandsObject



148
149
150
# File 'lib/chef-cli/cli.rb', line 148

def subcommands
  commands_map.command_names
end