Class: MCollective::Util::TasksSupport::CLI
- Inherits:
-
Object
- Object
- MCollective::Util::TasksSupport::CLI
- Defined in:
- lib/mcollective/util/tasks_support/cli.rb,
lib/mcollective/util/tasks_support/json_formatter.rb,
lib/mcollective/util/tasks_support/default_formatter.rb
Defined Under Namespace
Classes: DefaultFormatter, JSONFormatter
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#create_task_options(task, environment, application) ⇒ Object
Adds CLI options for all defined input.
-
#initialize(support, format, verbose) ⇒ CLI
constructor
A new instance of CLI.
-
#print_result(*args) ⇒ Object
Prints an individual result.
-
#print_result_metadata(*args) ⇒ Object
Prints metadata for an individual result.
-
#print_rpc_stats(*args) ⇒ Object
Prints the RPC stats for a request.
-
#print_task_summary(*args) ⇒ Object
Prints the summary of a task over a number of machines.
-
#show_task_help(task, environment, out = $stdout) ⇒ Object
Creates help for a task.
-
#show_task_list(environment, detail, out = $stdout) ⇒ Object
Shows the task list.
-
#task_input(configuration) ⇒ Hash
Parses the given CLI input string and creates results based on it.
-
#task_list(environment, detail, out = $stdout) ⇒ Object
Retrieves the task list.
-
#task_metadata(task, environment) ⇒ Object
Retrieves task metadata from Puppet Server and cache it.
-
#transform_hash_strings(meta, input) ⇒ Object
Transform hash inputs given as json strings to hashes.
-
#twirl(msg, max, current) ⇒ Object
Draw a compact CLI progress indicator.
-
#validate_task_input(task, meta, input) ⇒ Boolean
Validates the inputs provided on the CLI would be acceptable to the task.
Constructor Details
#initialize(support, format, verbose) ⇒ CLI
Returns a new instance of CLI.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 7 def initialize(support, format, verbose) @support = support if format == :json require_relative "json_formatter" @output = CLI::JSONFormatter.new(self, verbose) else require_relative "default_formatter" @output = CLI::DefaultFormatter.new(self, verbose) end end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
5 6 7 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 5 def output @output end |
Instance Method Details
#create_task_options(task, environment, application) ⇒ Object
Adds CLI options for all defined input
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 184 def (task, environment, application) = (task, environment) return if ["metadata"]["parameters"].nil? || ["metadata"]["parameters"].empty? ["metadata"]["parameters"].sort_by {|n, _| n}.each do |name, details| type, array, _ = @support.puppet_type_to_ruby(details["type"]) properties = { :description => "%s (%s)" % [details["description"], details["type"]], :arguments => ["--%s %s" % [name.downcase, name.upcase]], :type => type } properties[:type] = :array if array if type == Hash properties[:arguments] = ["--%s JSON" % name.downcase] properties[:type] = String end properties[:arguments] = ["--[no-]%s" % name.downcase] if type == :boolean application.class.option(name.intern, properties) end end |
#print_result(*args) ⇒ Object
Prints an individual result
123 124 125 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 123 def print_result(*args) @output.print_result(*args) end |
#print_result_metadata(*args) ⇒ Object
Prints metadata for an individual result
133 134 135 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 133 def (*args) @output.(*args) end |
#print_rpc_stats(*args) ⇒ Object
Prints the RPC stats for a request
128 129 130 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 128 def print_rpc_stats(*args) @output.print_rpc_stats(*args) end |
#print_task_summary(*args) ⇒ Object
Prints the summary of a task over a number of machines
118 119 120 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 118 def print_task_summary(*args) @output.print_task_summary(*args) end |
#show_task_help(task, environment, out = $stdout) ⇒ Object
Creates help for a task
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 82 def show_task_help(task, environment, out=$stdout) out.puts("Retrieving task metadata for task %s from the Puppet Server" % task) begin = (task, environment) rescue abort($!.to_s) end out.puts out.puts("%s - %s" % [Util.colorize(:bold, task), ["metadata"]["description"]]) out.puts if ["metadata"]["parameters"].nil? || ["metadata"]["parameters"].empty? out.puts("The task takes no parameters or have none defined") out.puts else out.puts("Task Parameters:") ["metadata"]["parameters"].sort_by {|n, _| n}.each do |name, details| out.puts(" %-30s %s (%s)" % [name, details["description"], details["type"]]) end end out.puts out.puts "Task Files:" ["files"].each do |file| out.puts(" %-30s %s bytes" % [file["filename"], file["size_bytes"]]) end out.puts out.puts("Use 'mco tasks run %s' to run this task" % [task]) end |
#show_task_list(environment, detail, out = $stdout) ⇒ Object
Shows the task list
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 24 def show_task_list(environment, detail, out=$stdout) out.puts "Known tasks in the %s environment" % environment out.puts known_tasks = task_list(environment, detail, out) out.print("\r") padding = known_tasks.keys.map(&:size).max + 2 if detail known_tasks.each do |name, description| out.puts " %-#{padding}s %s" % [name, description] end out.puts out.puts("Use mco task <TASK> to see task help") else known_tasks.keys.in_groups_of(3) do |tasks| out.puts " %s" % tasks.compact.map {|t| "%-#{padding}s" % t }.join(" ") end out.puts out.puts("Use mco task <TASK> to see task help") out.puts("Pass option --detail to see task descriptions") end end |
#task_input(configuration) ⇒ Hash
Parses the given CLI input string and creates results based on it
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 141 def task_input(configuration) result = {} input = configuration[:__json_input] if input if input.start_with?("@") input.sub!("@", "") result = JSON.parse(File.read(input)) if input.end_with?("json") result = YAML.safe_load(File.read(input)) if input.end_with?("yaml") else result = JSON.parse(input) end end configuration.each do |item, value| next if item.to_s.start_with?("__") result[item.to_s] = value end result end |
#task_list(environment, detail, out = $stdout) ⇒ Object
Retrieves the task list
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 56 def task_list(environment, detail, out=$stdout) tasks = {} out.print("Retrieving tasks....") known_tasks = @support.tasks(environment) known_tasks.each_with_index do |task, idx| description = nil if detail out.print(twirl("Retrieving tasks....", known_tasks.size, idx)) = (task["name"], environment) description = ["metadata"]["description"] end tasks[task["name"]] = description || "" end tasks end |
#task_metadata(task, environment) ⇒ Object
Retrieves task metadata from Puppet Server and cache it
235 236 237 238 239 240 241 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 235 def (task, environment) @_metadata ||= {} return @_metadata[task] if @_metadata[task] @_metadata[task] = @support.(task, environment) end |
#transform_hash_strings(meta, input) ⇒ Object
Transform hash inputs given as json strings to hashes
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 215 def transform_hash_strings(, input) return unless ["metadata"]["parameters"] ["metadata"]["parameters"].each do |name, details| type, _, _ = @support.puppet_type_to_ruby(details["type"]) next unless type == Hash && input[name].is_a?(String) begin input[name] = JSON.parse(input[name]) rescue raise("Could not parse input '%s' as JSON data: %s: %s" % [name, $!.class, $!.]) end end end |
#twirl(msg, max, current) ⇒ Object
Draw a compact CLI progress indicator
248 249 250 251 252 253 254 255 256 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 248 def twirl(msg, max, current) charset = ["▖", "▘", "▝", "▗"] index = current % charset.size char = charset[index] char = Util.colorize(:green, "✓") if max == current format = "\r%s %s %#{@max.to_s.size}d / %d" format % [msg, char, current, max] end |
#validate_task_input(task, meta, input) ⇒ Boolean
Validates the inputs provided on the CLI would be acceptable to the task
172 173 174 175 176 177 178 |
# File 'lib/mcollective/util/tasks_support/cli.rb', line 172 def validate_task_input(task, , input) ok, reason = @support.validate_task_inputs(input, ) return true if ok abort(reason) end |