class WavefrontCliController
Dynamically generate a CLI interface from files which describe each subcomand.
Attributes
Public Class Methods
# File lib/wavefront-cli/controller.rb, line 34 def initialize(args) @args = args @cmds = load_commands @usage = docopt_hash cmd, opts = parse_args @opts = parse_opts(opts) cli_class_obj = cli_class(cmd, @opts) run_command(cli_class_obj) rescue Interrupt handle_interrupt! end
Public Instance Methods
# File lib/wavefront-cli/controller.rb, line 137 def backtrace_message(err) if opts[:debug] warn "Backtrace:\n\t#{err.backtrace.join("\n\t")}" else puts "Re-run command with '-D' for backtrace." end end
Get the CLI class we need to run the command we've been given.
@param cmd [String] @return WavefrontCli::cmd
# File lib/wavefront-cli/controller.rb, line 119 def cli_class(cmd, opts) load_cli_class(cmd, opts) rescue StandardError => e exception_handler(e) end
What you see when you do 'wf –help' @return [String]
rubocop:disable Metrics/MethodLength
# File lib/wavefront-cli/controller.rb, line 57 def default_help s = ['Wavefront CLI', '', 'Usage:', " #{CMD} command [options]", " #{CMD} --version", " #{CMD} --help", '', 'Commands:'] cmds.sort.each do |k, v| s.<< format(' %-18<command>s %<desc>s', command: k, desc: v.description) end s.<< '' s.<< "Use '#{CMD} <command> --help' for further information." s.join("\n") end
@return [Hash] command descriptions for docopt.
# File lib/wavefront-cli/controller.rb, line 81 def docopt_hash cmds.each_with_object(default: default_help) do |(k, v), ret| ret[k.to_sym] = v.docopt end end
# File lib/wavefront-cli/controller.rb, line 46 def handle_interrupt! raise if opts[:debug] puts "\nCancelled at user's request." exit 0 end
@param error [WavefrontCli::Exception::CredentialError]
# File lib/wavefront-cli/controller.rb, line 147 def handle_missing_credentials(error) if DEFAULT_CONFIG.exist? && DEFAULT_CONFIG.file? abort "Credential error. #{error.message}" else puts 'No credentials supplied on the command line or via ' \ 'environment variables, and no configuration file found. ' \ "Please run 'wf config setup' to create configuration." .fold(TW, 0) exit 1 end end
Load a command description from a file. Each is in its own class
@param f [Pathname] path of file to load return [Class] new class object defining command.
# File lib/wavefront-cli/controller.rb, line 175 def import_command(path) return if path.extname != '.rb' || path.basename.to_s == 'base.rb' k_name = path.basename.to_s[0..-4] require(CMD_DIR + k_name) Object.const_get("WavefrontCommand#{k_name.capitalize}").new end
# File lib/wavefront-cli/controller.rb, line 125 def load_cli_class(cmd, opts) require_relative File.join('.', cmds[cmd].sdk_file) Object.const_get('WavefrontCli').const_get(cmds[cmd].sdk_class).new(opts) end
Each command is defined in its own file. Dynamically load all those commands. @return [Hash] :command => CommandClass
# File lib/wavefront-cli/controller.rb, line 163 def load_commands CMD_DIR.children.each_with_object({}) do |f, ret| k = import_command(f) ret[k.word.to_sym] = k if k end end
Parse the input. The first Docopt.docopt handles the default options, the second works on the command.
# File lib/wavefront-cli/controller.rb, line 90 def parse_args Docopt.docopt(usage[:default], version: WF_CLI_VERSION, argv: args) rescue Docopt::Exit => e cmd = args.empty? ? nil : args.first.to_sym abort e.message unless usage.key?(cmd) parse_cmd(cmd) end
Parse a command. @param cmd [String] given command
# File lib/wavefront-cli/controller.rb, line 102 def parse_cmd(cmd) [cmd, sanitize_keys(Docopt.docopt(usage[cmd], argv: args))] rescue Docopt::DocoptLanguageError => e abort "Mangled command description:\n#{e.message}" rescue Docopt::Exit => e abort e.message end
# File lib/wavefront-cli/controller.rb, line 110 def parse_opts(options) WavefrontCli::OptHandler.new(options).opts end
# File lib/wavefront-cli/controller.rb, line 130 def run_command(cli_class_obj) cli_class_obj.validate_opts cli_class_obj.run rescue StandardError => e exception_handler(e) end
Symbolize, and remove dashes from option keys
@param h [Hash] options hash return [Hash] h with modified keys
# File lib/wavefront-cli/controller.rb, line 188 def sanitize_keys(options) options.transform_keys { |k| k.to_s.delete('-').to_sym } end