Class: Rbcli::Command
- Inherits:
-
Object
- Object
- Rbcli::Command
- Extended by:
- CmdLibrary
- Defined in:
- lib/rbcli/engine/command.rb
Class Method Summary collapse
- .action(&block) ⇒ Object
- .config_default(*params) ⇒ Object
- .config_defaults(filename) ⇒ Object
-
.description(desc) ⇒ Object
Interface Functions.
-
.descriptions(indent_size, justification) ⇒ Object
Returns all descriptions for display in CLI help.
- .extern(path: nil, envvars: nil, &block) ⇒ Object
- .parameter(name, description, short: nil, type: :boolean, default: nil, required: false, permitted: nil, prompt: nil) ⇒ Object
-
.parseopts(*args) ⇒ Object
This method reads the parameters provided by the class and parses them from the CLI.
- .remote_permitted ⇒ Object
- .remote_permitted? ⇒ Boolean
-
.runcmd(cmd, local_params, cliopts) ⇒ Object
Run a given command.
- .script(path: nil, envvars: nil) ⇒ Object
- .usage(usage) ⇒ Object
Instance Method Summary collapse
Methods included from CmdLibrary
Class Method Details
.action(&block) ⇒ Object
60 |
# File 'lib/rbcli/engine/command.rb', line 60 def self.action █ @data[:action] = block; end |
.config_default(*params) ⇒ Object
64 |
# File 'lib/rbcli/engine/command.rb', line 64 def self.config_default *params; Rbcli::Config::add_default *params; end |
.config_defaults(filename) ⇒ Object
63 |
# File 'lib/rbcli/engine/command.rb', line 63 def self.config_defaults filename; Rbcli::Config::add_defaults(filename); end |
.description(desc) ⇒ Object
Interface Functions
58 |
# File 'lib/rbcli/engine/command.rb', line 58 def self.description desc; @data[:description] = desc; end |
.descriptions(indent_size, justification) ⇒ Object
Returns all descriptions for display in CLI help
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rbcli/engine/command.rb', line 125 def self.descriptions indent_size, justification #descmap = @commands.map { |name, klass| [name, klass.description] }.to_h @commands.map do |name, cmdobj| desc = '' if Rbcli.configuration(:me, :remote_execution) and cmdobj.remote_permitted? indent_size -= 3 indent_size.times { desc << ' ' } desc << '* ' else indent_size.times { desc << ' ' } end desc << name.ljust(justification) desc << cmdobj.class.data[:description] end.join("\n") end |
.extern(path: nil, envvars: nil, &block) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/rbcli/engine/command.rb', line 78 def self.extern path: nil, envvars: nil, &block if path == :default callerscript = caller_locations.first.absolute_path path = "#{File.dirname(callerscript)}/scripts/#{File.basename(callerscript, ".*")}.sh" end block = nil unless block_given? require 'rbcli/features/scriptwrapper' @data[:extern] = Rbcli::Scriptwrapper.new path, envvars, block end |
.parameter(name, description, short: nil, type: :boolean, default: nil, required: false, permitted: nil, prompt: nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rbcli/engine/command.rb', line 65 def self.parameter name, description, short: nil, type: :boolean, default: nil, required: false, permitted: nil, prompt: nil default ||= false if (type == :boolean || type == :bool || type == :flag) @data[:paramlist][name.to_sym] = { description: description, type: type, default: default, required: required, permitted: permitted, short: short, prompt: prompt } end |
.parseopts(*args) ⇒ Object
This method reads the parameters provided by the class and parses them from the CLI
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rbcli/engine/command.rb', line 144 def self.parseopts *args params = @data[:paramlist] command_name = self.name.split('::')[-1].downcase command_desc = @data[:description] command_usage = @data[:usage] optx = Trollop:: do data = Rbcli.configuration(:me) <<-EOS #{data[:description]} Selected Command: #{command_name.ljust(21)}#{command_desc} Usage: #{data[:scriptname]} [options] #{command_name} [parameters] #{if command_usage then "\n" + command_usage + "\n" end} Command-specific Parameters: EOS params.each do |name, opts| opt name, opts[:description], type: opts[:type], default: opts[:default], required: (opts[:prompt].nil? and opts[:required]), permitted: opts[:permitted], short: opts[:short] end if params.is_a? Hash end optx[:args] = ARGV params.each do |name, data| given_symbol = (name.to_s + '_given').to_sym if data[:prompt] and not (optx.key?(given_symbol) and optx[given_symbol]) if data[:type] == :bool or data[:type] == :boolean or data[:type] == :flag answer = 'INVALID_STRING' while answer.downcase != 'y' and answer.downcase != 'n' and answer.downcase != '' print 'Invalid entry. '.red unless answer == 'INVALID_STRING' print data[:prompt] + " (#{(data[:default]) ? 'Y/n' : 'y/N'}): " answer = gets.chomp if answer.downcase == 'y' optx[name] = true elsif answer.downcase == 'n' optx[name] = false elsif answer.downcase == '' optx[name] = data[:default] end end elsif data[:type] == :string print data[:prompt] print " (default: \"#{data[:default]}\"): " unless data[:default].nil? answer = gets.chomp answer = data[:default] if answer.empty? optx[name] = answer end end end optx end |
.remote_permitted ⇒ Object
61 |
# File 'lib/rbcli/engine/command.rb', line 61 def self.remote_permitted; @data[:remote_permitted] = true; end |
.remote_permitted? ⇒ Boolean
62 |
# File 'lib/rbcli/engine/command.rb', line 62 def self.remote_permitted?; @data[:remote_permitted]; end |
.runcmd(cmd, local_params, cliopts) ⇒ Object
Run a given command
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rbcli/engine/command.rb', line 103 def self.runcmd cmd, local_params, cliopts args = local_params.delete :args params = local_params global_opts = cliopts config = Rbcli::config raise Exception.new("Command #{cmd} can only have one of `action`, `script`, or `extern` defined.") if (@commands[cmd].data[:extern] or @commands[cmd].data[:script]) and @commands[cmd].data[:action] if cliopts[:remote_exec] Rbcli::RemoteExec.new(@commands[cmd], cliopts[:remote_exec], cliopts[:identity], params, args, global_opts, config).run #remote_exec @commands[cmd], params, args, global_opts, config return end @commands[cmd].data[:extern].execute params, args, global_opts, config unless @commands[cmd].data[:extern].nil? @commands[cmd].data[:script].execute params, args, global_opts, config unless @commands[cmd].data[:script].nil? @commands[cmd].data[:action].call params, args, global_opts, config unless @commands[cmd].data[:action].nil? end |
.script(path: nil, envvars: nil) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/rbcli/engine/command.rb', line 88 def self.script path: nil, envvars: nil if path == :default or path.nil? callerscript = caller_locations.first.absolute_path path = "#{File.dirname(callerscript)}/scripts/#{File.basename(callerscript, ".*")}.sh" end require 'rbcli/features/scriptwrapper' @data[:script] = Rbcli::Scriptwrapper.new path, envvars, nil, true end |
.usage(usage) ⇒ Object
59 |
# File 'lib/rbcli/engine/command.rb', line 59 def self.usage usage; @data[:usage] = usage; end |
Instance Method Details
#data ⇒ Object
53 |
# File 'lib/rbcli/engine/command.rb', line 53 def data; self.class.data; end |