Class: WavefrontCliController

Inherits:
Object
  • Object
show all
Includes:
WavefrontCli::Constants, WavefrontCli::ExceptionMixins
Defined in:
lib/wavefront-cli/controller.rb

Overview

Dynamically generate a CLI interface from files which describe each subcomand.

Constant Summary

Constants included from WavefrontCli::Constants

WavefrontCli::Constants::ALL_PAGE_SIZE, WavefrontCli::Constants::DEFAULT_CONFIG, WavefrontCli::Constants::DEFAULT_OPTS, WavefrontCli::Constants::EVENT_STATE_DIR, WavefrontCli::Constants::HUMAN_TIME_FORMAT, WavefrontCli::Constants::HUMAN_TIME_FORMAT_MS, WavefrontCli::Constants::SEARCH_SPLIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WavefrontCli::ExceptionMixins

#exception_handler

Constructor Details

#initialize(args) ⇒ WavefrontCliController

Returns a new instance of WavefrontCliController.



34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



29
30
31
# File 'lib/wavefront-cli/controller.rb', line 29

def args
  @args
end

#cmdsObject (readonly)

Returns the value of attribute cmds.



29
30
31
# File 'lib/wavefront-cli/controller.rb', line 29

def cmds
  @cmds
end

#optsObject (readonly)

Returns the value of attribute opts.



29
30
31
# File 'lib/wavefront-cli/controller.rb', line 29

def opts
  @opts
end

#twObject (readonly)

Returns the value of attribute tw.



29
30
31
# File 'lib/wavefront-cli/controller.rb', line 29

def tw
  @tw
end

#usageObject (readonly)

Returns the value of attribute usage.



29
30
31
# File 'lib/wavefront-cli/controller.rb', line 29

def usage
  @usage
end

Instance Method Details

#backtrace_message(err) ⇒ Object



137
138
139
140
141
142
143
# 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

#cli_class(cmd, opts) ⇒ Object

Get the CLI class we need to run the command we’ve been given.

Parameters:

Returns:

  • WavefrontCli::cmd



119
120
121
122
123
# 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

#default_helpString

What you see when you do ‘wf –help’ rubocop:disable Metrics/MethodLength

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

#docopt_hashHash

Returns command descriptions for docopt.

Returns:

  • (Hash)

    command descriptions for docopt.



81
82
83
84
85
# 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

#handle_interrupt!Object



46
47
48
49
50
51
# File 'lib/wavefront-cli/controller.rb', line 46

def handle_interrupt!
  raise if opts[:debug]

  puts "\nCancelled at user's request."
  exit 0
end

#handle_missing_credentials(error) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/wavefront-cli/controller.rb', line 147

def handle_missing_credentials(error)
  message = error.message.capitalize
  message << ('.') unless message.end_with?('.')

  puts "Credential error. #{message}"

  unless DEFAULT_CONFIG.exist? && DEFAULT_CONFIG.file?
    puts
    puts 'You can pass credentials on the command line or via ' \
         "environment variables. You may also run 'wf config setup' to " \
         'create a config file.'.fold(TW, 0)
  end

  exit 1
end

#import_command(path) ⇒ Object

Load a command description from a file. Each is in its own class

return [Class] new class object defining command.

Parameters:

  • f (Pathname)

    path of file to load



179
180
181
182
183
184
185
# File 'lib/wavefront-cli/controller.rb', line 179

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

#load_cli_class(cmd, opts) ⇒ Object



125
126
127
128
# 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

#load_commandsHash

Each command is defined in its own file. Dynamically load all those commands.

Returns:

  • (Hash)

    :command => CommandClass



167
168
169
170
171
172
# File 'lib/wavefront-cli/controller.rb', line 167

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_argsObject

Parse the input. The first Docopt.docopt handles the default options, the second works on the command.



90
91
92
93
94
95
96
97
# 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_cmd(cmd) ⇒ Object

Parse a command.

Parameters:

  • cmd (String)

    given command



102
103
104
105
106
107
108
# 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

#parse_opts(options) ⇒ Object



110
111
112
# File 'lib/wavefront-cli/controller.rb', line 110

def parse_opts(options)
  WavefrontCli::OptHandler.new(options).opts
end

#run_command(cli_class_obj) ⇒ Object



130
131
132
133
134
135
# 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

#sanitize_keys(options) ⇒ Object

Symbolize, and remove dashes from option keys

return [Hash] h with modified keys

Parameters:

  • h (Hash)

    options hash



192
193
194
# File 'lib/wavefront-cli/controller.rb', line 192

def sanitize_keys(options)
  options.transform_keys { |k| k.to_s.delete('-').to_sym }
end