class WavefrontCommandBase

A base class which all command classes extend.

Public Instance Methods

_commands() click to toggle source

Inheriting classes must override this method @return [Array]

# File lib/wavefront-cli/commands/base.rb, line 68
def _commands
  []
end
_options() click to toggle source

Inheriting classes must override this method @return [Array]

# File lib/wavefront-cli/commands/base.rb, line 39
def _options
  []
end
acl_commands() click to toggle source

Anything which takes ACLs provides the same interface @return [Array]

# File lib/wavefront-cli/commands/base.rb, line 58
def acl_commands
  ["acls #{CMN} <id>",
   "acl #{CMN} clear <id>",
   "acl #{CMN} grant (view | modify) on <id> to <name>...",
   "acl #{CMN} revoke (view | modify) on <id> from <name>..."]
end
commands(term_width = TW) click to toggle source

@param term_width [Integer] force a terminal width. Makes

testing far simpler.

@return [String] the subcommands the command offers.

# File lib/wavefront-cli/commands/base.rb, line 104
def commands(term_width = TW)
  text_arr = %w[Usage:]

  _commands.flatten.each do |cmd|
    text_arr.<< '  ' + "#{CMD} #{word} #{cmd}\n".cmd_fold(term_width)
  end

  text_arr.<< "  #{CMD} #{word} --help"
  text_arr.join("\n")
end
common_options() click to toggle source

Many commands have these options @return [Array]

# File lib/wavefront-cli/commands/base.rb, line 31
def common_options
  ['-E, --endpoint=URI       cluster endpoint',
   '-t, --token=TOKEN        Wavefront authentication token']
end
description() click to toggle source
# File lib/wavefront-cli/commands/base.rb, line 10
def description
  "view and manage #{things}"
end
docopt() click to toggle source

@return [String] a full options string which docopt understands

# File lib/wavefront-cli/commands/base.rb, line 167
def docopt
  commands + "\n\n" + options + "\n\n" + postscript
end
global_option_text(width, term_width) click to toggle source
# File lib/wavefront-cli/commands/base.rb, line 132
def global_option_text(width, term_width)
  text_arr = ['Global options:']
  global_options.each { |o| text_arr.<< opt_row(o, width, term_width) }
  text_arr.<< ''
end
global_options() click to toggle source

All commands have these options @return [Array]

# File lib/wavefront-cli/commands/base.rb, line 17
def global_options
  ['-c, --config=FILE    path to configuration file',
   '-P, --profile=NAME   profile in configuration file',
   '-D, --debug          enable debug mode',
   '-n, --noop           do not perform API calls',
   '-V, --verbose        be verbose',
   '-f, --format=STRING  output format',
   '-M, --items-only     only show items in machine-parseable formats',
   '-h, --help           show this message']
end
opt_row(opt_str, width, term_width = TW) click to toggle source

Formats an option string.

@param opt_str [String] the option string @param width [Integer] the width of the short + long options

columns. This is used to indent following lines

@param term_width [Integer] the width of the user's terminal

# File lib/wavefront-cli/commands/base.rb, line 145
def opt_row(opt_str, width, term_width = TW)
  format("  %s %-#{width}s %s",
         *opt_str.split(/\s+/, 3)).opt_fold(term_width, width + 5)
end
option_column_width() click to toggle source

@return [Integer] the width of the column containing short and

long options
# File lib/wavefront-cli/commands/base.rb, line 153
def option_column_width
  (global_options + _options).flatten.map do |o|
    o.split(/\s+/, 3)[0..1].join(' ').size
  end.max
end
options(term_width = TW) click to toggle source

@param term_width [Integer] force a terminal width. Makes

testing far simpler.

@return [String] the options the command understands.

# File lib/wavefront-cli/commands/base.rb, line 119
def options(term_width = TW)
  width = option_column_width
  text_arr = if global_options.empty?
               []
             else
               global_option_text(width, term_width)
             end

  text_arr.<< 'Options:'
  _options.flatten.each { |o| text_arr.<< opt_row(o, width, term_width) }
  text_arr.join("\n")
end
postscript() click to toggle source

@return [String] which will be printed underneath the options.

# File lib/wavefront-cli/commands/base.rb, line 161
def postscript
  ''
end
sdk_class() click to toggle source

@return [String] the name of the SDK class which does the work

for this command.
# File lib/wavefront-cli/commands/base.rb, line 89
def sdk_class
  word.capitalize
end
sdk_file() click to toggle source

@return [String] the name of the SDK file which does the work

for this command.
# File lib/wavefront-cli/commands/base.rb, line 96
def sdk_file
  word
end
tag_commands() click to toggle source

Anything which takes tags provides the same interface @return [Array]

# File lib/wavefront-cli/commands/base.rb, line 46
def tag_commands
  ["tags #{CMN} <id>",
   "tag set #{CMN} <id> <tag>...",
   "tag clear #{CMN} <id>",
   "tag add #{CMN} <id> <tag>",
   "tag delete #{CMN} <id> <tag>",
   "tag pathsearch #{CMN} [-al] [-o offset] [-L limit] <word>"]
end
thing() click to toggle source
# File lib/wavefront-cli/commands/base.rb, line 78
def thing
  word
end
things() click to toggle source
# File lib/wavefront-cli/commands/base.rb, line 82
def things
  thing + 's'
end
word() click to toggle source

@return [String] the command keyword

# File lib/wavefront-cli/commands/base.rb, line 74
def word
  self.class.name.sub(/WavefrontCommand/, '').downcase
end