class ChefCLI::Command::Generate
def self.banner
def self.banner banner_headline + generator_list + "\n" end
def self.banner_headline
def self.banner_headline <<~E Usage: #{ChefCLI::Dist::EXEC} generate GENERATOR [options] Available generators: E end
def self.generator(name, class_name, description)
def self.generator(name, class_name, description) generators << GeneratorCommand.new(name, class_name, description) end
def self.generator_list
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
def self.generators
def self.generators @generators ||= [] end
def generator_for(arg)
def generator_for(arg) self.class.generators.find { |g| g.name.to_s == arg } end
def have_generator?(name)
def have_generator?(name) self.class.generators.map { |g| g.name.to_s }.include?(name) end
def initialize(*args)
def initialize(*args) super end
def needs_help?(params)
argument is a valid generator name, like `chef generate cookbook -h`,
"--help". Here we override that behavior such that if the first
In the Base class, this is defined to be true if any args match "-h" or
def needs_help?(params) return false if have_generator?(params[0]) super end
def run(params)
def run(params) if params[0] == "build-cookbook" warn "[DEPRECATION] Chef Workflow (Delivery) is end of life (EOL) as of December 31, 2020 and this generator subcommand has been removed".freeze return 1 end 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