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

#_commandsArray

Inheriting classes must override this method

Returns:



60
61
62
# File 'lib/wavefront-cli/commands/base.rb', line 60

def _commands
  []
end

#_optionsArray

Inheriting classes must override this method

Returns:



32
33
34
# File 'lib/wavefront-cli/commands/base.rb', line 32

def _options
  []
end

#acl_commandsArray

Anything which takes ACLs provides the same interface

Returns:



50
51
52
53
54
55
# File 'lib/wavefront-cli/commands/base.rb', line 50

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) ⇒ String

Returns the subcommands the command offers.

Parameters:

  • term_width (Integer) (defaults to: TW)

    force a terminal width. Makes testing far simpler.

Returns:

  • (String)

    the subcommands the command offers.



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

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

#common_optionsArray

Many commands have these options

Returns:



24
25
26
27
# File 'lib/wavefront-cli/commands/base.rb', line 24

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

#docoptString

Returns a full options string which docopt understands.

Returns:

  • (String)

    a full options string which docopt understands



145
146
147
# File 'lib/wavefront-cli/commands/base.rb', line 145

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

#global_optionsArray

All commands have these options

Returns:



12
13
14
15
16
17
18
19
# File 'lib/wavefront-cli/commands/base.rb', line 12

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, term_width = TW) ⇒ Object

Formats an option string.

Parameters:

  • opt (String)

    the option string

  • width (Integer)

    the width of the short + long options columns. This is used to indent following lines

  • term_width (Integer) (defaults to: TW)

    the width of the user’s terminal



123
124
125
126
# File 'lib/wavefront-cli/commands/base.rb', line 123

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

#option_column_widthInteger

Returns the width of the column containing short and long options.

Returns:

  • (Integer)

    the width of the column containing short and long options



131
132
133
134
135
# File 'lib/wavefront-cli/commands/base.rb', line 131

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) ⇒ String

rubocop:disable Metrics/AbcSize

Parameters:

  • term_width (Integer) (defaults to: TW)

    force a terminal width. Makes testing far simpler.

Returns:

  • (String)

    the options the command understands.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/wavefront-cli/commands/base.rb', line 100

def options(term_width = TW)
  width = option_column_width
  ret = ''

  unless global_options.empty?
    ret.<< "Global options:\n"
    global_options.each { |o| ret.<< opt_row(o, width, term_width) }
    ret.<< "\n"
  end

  ret.<< "Options:\n"
  _options.flatten.each { |o| ret.<< opt_row(o, width, term_width) }
  ret
end

#postscriptString

Returns which will be printed underneath the options.

Returns:

  • (String)

    which will be printed underneath the options.



139
140
141
# File 'lib/wavefront-cli/commands/base.rb', line 139

def postscript
  ''
end

#sdk_classString

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

Returns:

  • (String)

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



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

def sdk_class
  word.capitalize
end

#sdk_fileString

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

Returns:

  • (String)

    the name of the SDK file which does the work for this command.



80
81
82
# File 'lib/wavefront-cli/commands/base.rb', line 80

def sdk_file
  word
end

#tag_commandsArray

Anything which takes tags provides the same interface

Returns:



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

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>"]
end

#wordString

Returns the command keyword.

Returns:

  • (String)

    the command keyword



66
67
68
# File 'lib/wavefront-cli/commands/base.rb', line 66

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