class ChefCLI::CLI

Attributes

argv[R]

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# File lib/chef-cli/cli.rb, line 60
def initialize(argv)
  @argv = argv
  super() # mixlib-cli #initialize doesn't allow arguments
end

Public Instance Methods

commands_map() click to toggle source
# File lib/chef-cli/cli.rb, line 140
def commands_map
  ChefCLI.commands_map
end
exit(n) click to toggle source
# File lib/chef-cli/cli.rb, line 136
def exit(n)
  Kernel.exit(n)
end
handle_options() click to toggle source

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

# 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) click to toggle source
# File lib/chef-cli/cli.rb, line 144
def have_command?(name)
  commands_map.have_command?(name)
end
instantiate_subcommand(name) click to toggle source
# File lib/chef-cli/cli.rb, line 167
def instantiate_subcommand(name)
  commands_map.instantiate(name)
end
option?(param) click to toggle source

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

@param [String] param The passed parameter to check

@return [Boolean]

# File lib/chef-cli/cli.rb, line 163
def option?(param)
  param[0] == "-"
end
run(enforce_license: false) click to toggle source
# 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_help() click to toggle source
# 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_version() click to toggle source
# 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_manifest() click to toggle source
# 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_specs() click to toggle source
# File lib/chef-cli/cli.rb, line 152
def subcommand_specs
  commands_map.command_specs
end
subcommands() click to toggle source
# File lib/chef-cli/cli.rb, line 148
def subcommands
  commands_map.command_names
end

Private Instance Methods

component_version(name) click to toggle source
# File lib/chef-cli/cli.rb, line 177
def component_version(name)
  if gem_manifest_hash[name].is_a?(Array)
    gem_manifest_hash[name].first
  elsif manifest_hash.key? name
    manifest_field(name)
  else
    manifest_hash.dig("software", name, "locked_version") || "unknown"
  end
end
drive_upcase(path) click to toggle source

upcase drive letters for comparison since ruby has a String#capitalize function

# File lib/chef-cli/cli.rb, line 219
def drive_upcase(path)
  if Chef::Platform.windows? && path[0] =~ /^[A-Za-z]$/ && path[1, 2] == ":\\"
    path.capitalize
  else
    path
  end
end
env() click to toggle source
# File lib/chef-cli/cli.rb, line 227
def env
  ENV
end
gem_manifest_hash() click to toggle source
# File lib/chef-cli/cli.rb, line 192
def gem_manifest_hash
  require "json" unless defined?(JSON)
  @gem_manifest_hash ||= JSON.parse(read_gem_version_manifest_json)
end
manifest_field(field) click to toggle source
# File lib/chef-cli/cli.rb, line 173
def manifest_field(field)
  manifest_hash[field] || "unknown"
end
manifest_hash() click to toggle source
# File lib/chef-cli/cli.rb, line 187
def manifest_hash
  require "json" unless defined?(JSON)
  @manifest_hash ||= JSON.parse(read_version_manifest_json)
end
normalized_exit_code(maybe_integer) click to toggle source
# File lib/chef-cli/cli.rb, line 205
def normalized_exit_code(maybe_integer)
  if maybe_integer.is_a?(Integer) && (0..255).cover?(maybe_integer)
    maybe_integer
  else
    0
  end
end
path_check!() click to toggle source

catch the cases where users setup only the embedded_bin_dir in their path, or when they have the embedded_bin_dir before the omnibus_bin_dir – both of which will defeat appbundler and interact very badly with our intent.

# File lib/chef-cli/cli.rb, line 234
def path_check!
  # When installed outside of omnibus, trust the user to configure their PATH
  return true unless omnibus_install?

  paths = env[path_key].split(File::PATH_SEPARATOR)
  paths.map! { |p| drive_upcase(Chef::Util::PathHelper.cleanpath(p)) }
  embed_index = paths.index(drive_upcase(Chef::Util::PathHelper.cleanpath(omnibus_embedded_bin_dir)))
  bin_index = paths.index(drive_upcase(Chef::Util::PathHelper.cleanpath(omnibus_bin_dir)))
  if embed_index
    if bin_index
      if embed_index < bin_index
        err("WARN: #{omnibus_embedded_bin_dir} is before #{omnibus_bin_dir} in your #{path_key}, please reverse that order.")
        err("WARN: consider using `#{ChefCLI::Dist::EXEC} shell-init <shell>` command to setup your environment correctly.")
      end
    else
      err("WARN: only #{omnibus_embedded_bin_dir} is present in your path, you must add #{omnibus_bin_dir} before that directory.")
      err("WARN: consider using `#{ChefCLI::Dist::EXEC} shell-init <shell>` command to setup your environment correctly.")
    end
  end
end
path_key() click to toggle source

Find PATH or Path correctly if we are on Windows

# File lib/chef-cli/cli.rb, line 214
def path_key
  env.keys.grep(/\Apath\Z/i).first
end
read_gem_version_manifest_json() click to toggle source
# File lib/chef-cli/cli.rb, line 201
def read_gem_version_manifest_json
  File.read(File.join(omnibus_root, "gem-version-manifest.json"))
end
read_version_manifest_json() click to toggle source
# File lib/chef-cli/cli.rb, line 197
def read_version_manifest_json
  File.read(File.join(omnibus_root, "version-manifest.json"))
end