lib/pdk/cli.rb in pdk-0.1.0 vs lib/pdk/cli.rb in pdk-0.2.0

- old
+ new

@@ -1,80 +1,83 @@ require 'cri' require 'pdk/cli/errors' -require 'pdk/cli/util/option_validator' require 'pdk/cli/util/option_normalizer' +require 'pdk/cli/util/option_validator' +require 'pdk/generators/module' +require 'pdk/i18n' require 'pdk/logger' require 'pdk/report' +require 'pdk/util/version' -require 'pdk/cli/new' -require 'pdk/cli/validate' -require 'pdk/cli/test' +module PDK::CLI + def self.run(args) + @base_cmd.run(args) + rescue PDK::CLI::FatalError => e + PDK.logger.fatal(e.message) if e.message -module PDK - module CLI - def self.base_command - @base ||= Cri::Command.new.tap do |cmd| - cmd.modify do - name 'pdk' - usage _("pdk command [options]") - summary _("Puppet Development Kit") - description _("The shortest path to better modules.") + # If FatalError was raised as the result of another exception, send the + # details of that exception to the debug log. If there was no cause + # (FatalError raised on its own outside a rescue block), send the details + # of the FatalError exception to the debug log. + cause = e.cause + if cause.nil? + e.backtrace.each { |line| PDK.logger.debug(line) } + else + PDK.logger.debug("#{cause.class}: #{cause.message}") + cause.backtrace.each { |line| PDK.logger.debug(line) } + end - flag :h, :help, _("show help for this command") do |_, c| - puts c.help - exit 0 - end + exit e.exit_code + end - format_desc = _( - "Specify desired output format. Valid formats are '%{available_formats}'. " + - "You may also specify a file to which the formatted output will be directed, " + - "for example: '--format=junit:report.xml'. This option may be specified " + - "multiple times as long as each option specifies a distinct target file." - ) % {available_formats: PDK::Report.formats.join("', '")} + def self.template_url_option(dsl) + dsl.option nil, 'template-url', _('Specifies the URL to the template to use when creating new modules, and other parts.'), argument: :required, default: PDK::Generate::Module::DEFAULT_TEMPLATE + end - option :f, :format, format_desc, { argument: :required, multiple: true } do |values| - values.compact.each do |v| - if v.include?(':') - format = v.split(':', 2).first + @base_cmd = Cri::Command.define do + name 'pdk' + usage _('pdk command [options]') + summary _('Puppet Development Kit') + description _('The shortest path to better modules.') + default_subcommand 'help' - PDK::CLI::Util::OptionValidator.enum(format, PDK::Report.formats) - else - PDK::CLI::Util::OptionValidator.enum(v, PDK::Report.formats) - end - end - end + flag nil, :version, _('show version of pdk') do |_, _| + puts PDK::Util::Version.version_string + exit 0 + end - flag :d, :debug, _("Enable debug output.") do |_, _| - PDK.logger.enable_debug_output - end - end + flag :h, :help, _('show help for this command') do |_, c| + puts c.help + exit 0 + end - cmd.add_command(Cri::Command.new_basic_help) + format_desc = _( + "Specify desired output format. Valid formats are '%{available_formats}'. " \ + 'You may also specify a file to which the formatted output will be directed, ' \ + "for example: '--format=junit:report.xml'. This option may be specified " \ + 'multiple times as long as each option specifies a distinct target file.', + ) % { available_formats: PDK::Report.formats.join("', '") } - cmd.add_command(PDK::CLI::New.command) - cmd.add_command(PDK::CLI::Validate.command) - cmd.add_command(PDK::CLI::Test.command) + option :f, :format, format_desc, argument: :required, multiple: true do |values| + values.compact.each do |v| + if v.include?(':') + format = v.split(':', 2).first + + Util::OptionValidator.enum(format, PDK::Report.formats) + else + Util::OptionValidator.enum(v, PDK::Report.formats) + end end end - def self.run(args) - base_command.run(args) - rescue PDK::CLI::FatalError => e - PDK.logger.fatal(e.message) if e.message - - # If FatalError was raised as the result of another exception, send the - # details of that exception to the debug log. If there was no cause - # (FatalError raised on its own outside a rescue block), send the details - # of the FatalError exception to the debug log. - cause = e.cause - if cause.nil? - e.backtrace.each { |line| PDK.logger.debug(line) } - else - PDK.logger.debug("#{cause.class}: #{cause.message}") - cause.backtrace.each { |line| PDK.logger.debug(line) } - end - - exit e.exit_code + flag :d, :debug, _('Enable debug output.') do |_, _| + PDK.logger.enable_debug_output end end + + require 'pdk/cli/new' + require 'pdk/cli/test' + require 'pdk/cli/validate' + + @base_cmd.add_command Cri::Command.new_basic_help end