Class: WavefrontCommandBase

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-cli/commands/base.rb

Overview

A base class which all command classes extend.

Instance Method Summary collapse

Instance Method Details

#column_widthsObject



74
75
76
77
78
# File 'lib/wavefront-cli/commands/base.rb', line 74

def column_widths
  (global_options + _options).flatten.map do |o|
    o.split(/\s+/, 3)[0..1].join(' ').size
  end.max
end

#commandsObject

Returns a string describing the subcommands the command offers.



53
54
55
56
57
# File 'lib/wavefront-cli/commands/base.rb', line 53

def commands
  _commands.flatten.each_with_object("Usage:\n") do |cmd, ret|
    ret.<< '  ' + "#{CMD} #{word} #{cmd}\n".cmd_fold + "\n"
  end + "  #{CMD} #{word} --help"
end

#common_optionsObject

Many commands have these options



19
20
21
22
# File 'lib/wavefront-cli/commands/base.rb', line 19

def common_options
  ['-E, --endpoint=URI       cluster endpoint',
   '-t, --token=TOKEN        Wavefront authentication token']
end

#docoptObject

Returns a full options string which docopt understands



88
89
90
# File 'lib/wavefront-cli/commands/base.rb', line 88

def docopt
  commands + "\n\n" + options + "\n" + postscript
end

#global_optionsObject

All commands have these options



8
9
10
11
12
13
14
15
# File 'lib/wavefront-cli/commands/base.rb', line 8

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',
   '-h, --help           show this message']
end

#opt_row(opt, width) ⇒ Object



70
71
72
# File 'lib/wavefront-cli/commands/base.rb', line 70

def opt_row(opt, width)
  format("  %s %-#{width}s %s\n", *opt.split(/\s+/, 3))
end

#optionsObject

Returns a string describing the options the command understands.



61
62
63
64
65
66
67
68
# File 'lib/wavefront-cli/commands/base.rb', line 61

def options
  width = column_widths
  ret = "Global options:\n"
  global_options.each { |o| ret.<< opt_row(o, width) }
  ret.<< "\nOptions:\n"
  _options.flatten.each { |o| ret.<< opt_row(o, width) }
  ret
end

#postscriptObject

Returns a string which will be printed underneath the options.



82
83
84
# File 'lib/wavefront-cli/commands/base.rb', line 82

def postscript
  ''
end

#sdk_classObject

Returns the name of the SDK class which does the work for this command.



43
44
45
# File 'lib/wavefront-cli/commands/base.rb', line 43

def sdk_class
  word.capitalize
end

#sdk_fileObject



47
48
49
# File 'lib/wavefront-cli/commands/base.rb', line 47

def sdk_file
  word
end

#tag_commandsObject

Anything which takes tags provides the same interface



26
27
28
29
30
31
32
# File 'lib/wavefront-cli/commands/base.rb', line 26

def tag_commands
  ["tags #{CMN} [-f format] <id>",
   "tag set #{CMN} <id> <tag>...",
   "tag clear #{CMN} <id>",
   "tag add #{CMN} <id> <tag>",
   "tag delete #{CMN} <id> <tag>"]
end

#wordObject

The command keyword



36
37
38
# File 'lib/wavefront-cli/commands/base.rb', line 36

def word
  self.class.name.sub(/WavefrontCommand/, '').downcase
end