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_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.



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

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.



54
55
56
# File 'lib/chef-cli/cli.rb', line 54

def argv
  @argv
end

Instance Method Details

#commands_mapObject



157
158
159
# File 'lib/chef-cli/cli.rb', line 157

def commands_map
  ChefCLI.commands_map
end

#exit(n) ⇒ Object



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

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

#handle_optionsObject

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



88
89
90
91
92
93
94
95
96
# File 'lib/chef-cli/cli.rb', line 88

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

#have_command?(name) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/chef-cli/cli.rb', line 161

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

#instantiate_subcommand(name) ⇒ Object



177
178
179
# File 'lib/chef-cli/cli.rb', line 177

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

#option?(param) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/chef-cli/cli.rb', line 173

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

#run(enforce_license: false) ⇒ Object



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

def run(enforce_license: false)
  sanity_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



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chef-cli/cli.rb', line 141

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_versionObject



98
99
100
101
102
103
104
# File 'lib/chef-cli/cli.rb', line 98

def show_version
  if omnibus_install?
    show_version_via_version_manifest
  else
    show_version_via_shell_out
  end
end

#show_version_via_shell_outObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef-cli/cli.rb', line 118

def show_version_via_shell_out
  msg("#{ChefCLI::Dist::PRODUCT} version: #{ChefCLI::VERSION}")
  { "#{ChefCLI::Dist::INFRA_CLIENT_PRODUCT}": "#{ChefCLI::Dist::INFRA_CLIENT_CLI}",
    "#{ChefCLI::Dist::INSPEC_PRODUCT}": "#{ChefCLI::Dist::INSPEC_CLI}",
    "Test Kitchen": "kitchen",
    "Cookstyle": "cookstyle",
  }.each do |name, cli|
    # @todo when Ruby 2.5/2.6 support goes away this if statement can go away
    if Gem::Version.new(Bundler::VERSION) >= Gem::Version.new("2")
      result = Bundler.with_unbundled_env { shell_out("#{cli} --version") }
    else
      result = Bundler.with_clean_env { shell_out("#{cli} --version") }
    end

    if result.exitstatus != 0
      msg("#{name} version: ERROR")
    else
      version = result.stdout.lines.first.scan(/(?:master\s)?[\d+\.\(\)]+\S+/).join("\s")
      msg("#{name} version: #{version}")
    end
  end
end

#show_version_via_version_manifestObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef-cli/cli.rb', line 106

def show_version_via_version_manifest
  msg("#{ChefCLI::Dist::PRODUCT} version: #{manifest_field("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}",
    "Test Kitchen": "test-kitchen",
    "Cookstyle": "cookstyle",
  }.each do |name, gem|
    msg("#{name} version: #{gem_version(gem)}")
  end
end

#subcommand_specsObject



169
170
171
# File 'lib/chef-cli/cli.rb', line 169

def subcommand_specs
  commands_map.command_specs
end

#subcommandsObject



165
166
167
# File 'lib/chef-cli/cli.rb', line 165

def subcommands
  commands_map.command_names
end