Class: Cumulus::Common::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/common/Commands.rb

Overview

Public: Base class for the command line parser class.

Classes that extend this class must provide the following methods:

self.manager - returns the manager for the AWS module that is being used.

Additionally, the following methods can be set to change the behavior of the parser:

self.usage_message - returns the usage instructions.
self.banner_message - returns the title, purpose, and behavior of the module. This is displayed in the help message.
self.command_details - returns basic instructions on how to use each module command. This is displayed in the help message.
self.valid_options - returns an array of the valid arguments where each argument is an array of valid commands.
self.verify - returns true/false of whether or not the arguments passed in are valid (proper order, right commands, etc).
self.execute - runs the correct method on the manager based on the array of arguments passed in.

For your convenience, many of the help and usage messages can be formatted correctly with the self.format_message method.

To use this method, pass in the message as an array of 'lines' where each line is either a string, or a command-instruction pair.

The following super class is one example of what each method could look like. Change them in inherited classes as necessary.

Class Method Summary collapse

Class Method Details



31
32
33
34
35
36
# File 'lib/common/Commands.rb', line 31

def self.banner_message
  format_message [
    "#{manager_name}: Manage #{manager_name.upcase}s.",
    "\tCompiles #{manager_name.upcase}s that are defined with configuration files and syncs the resulting #{manager_name.upcase} assets with AWS.",
  ]
end

.command_detailsObject



38
39
40
41
42
43
44
45
# File 'lib/common/Commands.rb', line 38

def self.command_details
  format_message [
    ["diff", "get a list of resources that have different definitions locally than in AWS (supplying the name of the group will diff only that group)"],
    ["list", "list the resources defined in configuration"],
    ["migrate", "create resource configuration files that match the definitions in AWS"],
    ["sync", "sync the local resource definition with AWS (supplying the name of the resource will sync only that group). Also adds and removes users from groups"],
  ]
end

.execute(arguments) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/common/Commands.rb', line 81

def self.execute(arguments)
  if arguments[0] == "diff" and arguments.size == 2
    manager.diff_one(arguments[1])
  elsif arguments[0] == "sync" and arguments.size == 2
    manager.sync_one(arguments[1])
  else
    manager.method(arguments[0]).call
  end
end

.format_message(message, args = Hash.new) ⇒ Object

use this helper function to format help messages



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/common/Commands.rb', line 92

def self.format_message(message, args = Hash.new)
  # default pad is the size of the smallest command
  pad = args.key?(:padding) ? args[:padding] : message.reduce(0) do |memo, line|
    if line.class == Array && line.first.size > memo
      line.first.size
    else
      memo
    end
  end

  message = message.reduce(String.new) do |memo, line|
    if line.class == Array
      memo + "\t%-#{pad}s - %s\n" % line
    else
      memo + line + "\n"
    end
  end.chomp

  message = "\t"*args[:indent] + message.gsub("\n", "\n" + "\t"*args[:indent]) if args.key?(:indent)

  message
end

.help_messageObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/common/Commands.rb', line 47

def self.help_message
  format_message [
    "#{banner_message}",
    "",
    "#{usage_message}",
    "",
    "Commands",
    "#{command_details}"
  ]
end

.managerObject



58
59
60
61
# File 'lib/common/Commands.rb', line 58

def self.manager
  require "common/manager/Manager"
  Cumulus::Common::Manager.new
end

.manager_nameObject

Retrieves the AWS module name by checking what ruby module the class is in.



64
65
66
# File 'lib/common/Commands.rb', line 64

def self.manager_name
  manager.class.to_s.split("::")[1].downcase
end

.parse(arguments) ⇒ Object

the main function called by the command line parser. DON’T OVERRIDE



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/common/Commands.rb', line 116

def self.parse(arguments)
  if arguments.size >= 1 and arguments[0] == "help"
    puts help_message
    exit
  end

  if !verify(arguments)
    puts usage_message
    exit
  end

  execute(arguments)
end

.usage_messageObject



23
24
25
26
27
28
29
# File 'lib/common/Commands.rb', line 23

def self.usage_message
  options = valid_options
  options[0] = options[0].push("help")
  "Usage: cumulus #{manager_name}" + options.reduce(String.new) do |memo, param_list|
    memo + " [" + param_list.join("|") + "]"
  end + " <asset>"
end

.valid_optionsObject



68
69
70
71
72
# File 'lib/common/Commands.rb', line 68

def self.valid_options
  # The first array is the list of all possible first commands
  # The second array is the list of all possible second commands and so on.
  [["diff", "list", "migrate", "sync"]]
end

.verify(arguments) ⇒ Object



74
75
76
77
78
79
# File 'lib/common/Commands.rb', line 74

def self.verify(arguments)
  (arguments.size >= valid_options.size) and
  (valid_options.size < 1 or valid_options[0].include?(arguments[0])) and
  (valid_options.size < 2 or valid_options[1].include?(arguments[1])) and
  (valid_options.size < 3 or valid_options[2].include?(arguments[2]))
end