Class: ChefCLI::Command::Generate

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-cli/command/generate.rb

Defined Under Namespace

Classes: GeneratorCommand

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#check_license_acceptance, #needs_version?, #run_with_default_options

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(*args) ⇒ Generate

chef generate app path/to/basename –skel=path/to/skeleton –example chef generate file name [path/to/cookbook_root] (inferred from cwd) –from=source_file



85
86
87
# File 'lib/chef-cli/command/generate.rb', line 85

def initialize(*args)
  super
end

Class Method Details



78
79
80
# File 'lib/chef-cli/command/generate.rb', line 78

def self.banner
  banner_headline + generator_list + "\n"
end


65
66
67
68
69
70
71
# File 'lib/chef-cli/command/generate.rb', line 65

def self.banner_headline
  <<~E
    Usage: #{ChefCLI::Dist::EXEC} generate GENERATOR [options]

    Available generators:
  E
end

.generator(name, class_name, description) ⇒ Object



47
48
49
# File 'lib/chef-cli/command/generate.rb', line 47

def self.generator(name, class_name, description)
  generators << GeneratorCommand.new(name, class_name, description)
end

.generator_listObject



73
74
75
76
# File 'lib/chef-cli/command/generate.rb', line 73

def self.generator_list
  justify_size = generators.map { |g| g.name.size }.max + 2
  generators.map { |g| "  #{g.name.to_s.ljust(justify_size)}#{g.description}" }.join("\n")
end

.generatorsObject



43
44
45
# File 'lib/chef-cli/command/generate.rb', line 43

def self.generators
  @generators ||= []
end

Instance Method Details

#generator_for(arg) ⇒ Object



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

def generator_for(arg)
  self.class.generators.find { |g| g.name.to_s == arg }
end

#have_generator?(name) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/chef-cli/command/generate.rb', line 122

def have_generator?(name)
  self.class.generators.map { |g| g.name.to_s }.include?(name)
end

#needs_help?(params) ⇒ Boolean

In the Base class, this is defined to be true if any args match “-h” or “–help”. Here we override that behavior such that if the first argument is a valid generator name, like ‘chef generate cookbook -h`, we delegate the request to the specified generator.

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/chef-cli/command/generate.rb', line 116

def needs_help?(params)
  return false if have_generator?(params[0])

  super
end

#run(params) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef-cli/command/generate.rb', line 89

def run(params)
  if ( generator_spec = generator_for(params[0]) )
    params.shift
    generator = GeneratorCommands.build(generator_spec.class_name, params)
    generator.run
  else
    msg(banner)
    1
  end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
  # ChefCLI::Command::Base also handles this error in the same way, but it
  # does not have access to the correct option parser, so it cannot print
  # the usage correctly. Therefore, invalid CLI usage needs to be handled
  # here.
  err("ERROR: #{e.message}\n")
  msg(generator.opt_parser)
  1
end