Class: Cumulus::Common::Commands
- Inherits:
-
Object
- Object
- Cumulus::Common::Commands
- 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.
Direct Known Subclasses
AutoScaling::Commands, Cumulus::CloudFront::Commands, EC2::Commands, ELB::Commands, IAM::Commands, Kinesis::Commands, Route53::Commands, S3::Commands, SQS::Commands, SecurityGroups::Commands, VPC::Commands
Class Method Summary collapse
- .banner_message ⇒ Object
- .command_details ⇒ Object
- .execute(arguments) ⇒ Object
-
.format_message(message, args = Hash.new) ⇒ Object
use this helper function to format help messages.
- .help_message ⇒ Object
- .manager ⇒ Object
-
.manager_name ⇒ Object
Retrieves the AWS module name by checking what ruby module the class is in.
-
.parse(arguments) ⇒ Object
the main function called by the command line parser.
- .usage_message ⇒ Object
- .valid_options ⇒ Object
- .verify(arguments) ⇒ Object
Class Method Details
.banner_message ⇒ Object
31 32 33 34 35 36 |
# File 'lib/common/Commands.rb', line 31 def self. [ "#{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_details ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/common/Commands.rb', line 38 def self.command_details [ ["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.(, args = Hash.new) # default pad is the size of the smallest command pad = args.key?(:padding) ? args[:padding] : .reduce(0) do |memo, line| if line.class == Array && line.first.size > memo line.first.size else memo end end = .reduce(String.new) do |memo, line| if line.class == Array memo + "\t%-#{pad}s - %s\n" % line else memo + line + "\n" end end.chomp = "\t"*args[:indent] + .gsub("\n", "\n" + "\t"*args[:indent]) if args.key?(:indent) end |
.help_message ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/common/Commands.rb', line 47 def self. [ "#{}", "", "#{}", "", "Commands", "#{command_details}" ] end |
.manager ⇒ Object
58 59 60 61 |
# File 'lib/common/Commands.rb', line 58 def self.manager require "common/manager/Manager" Cumulus::Common::Manager.new end |
.manager_name ⇒ Object
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 exit end if !verify(arguments) puts exit end execute(arguments) end |
.usage_message ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/common/Commands.rb', line 23 def self. = [0] = [0].push("help") "Usage: cumulus #{manager_name}" + .reduce(String.new) do |memo, param_list| memo + " [" + param_list.join("|") + "]" end + " <asset>" end |
.valid_options ⇒ Object
68 69 70 71 72 |
# File 'lib/common/Commands.rb', line 68 def self. # 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 >= .size) and (.size < 1 or [0].include?(arguments[0])) and (.size < 2 or [1].include?(arguments[1])) and (.size < 3 or [2].include?(arguments[2])) end |