Class: Beaker::Command
- Inherits:
-
Object
- Object
- Beaker::Command
- Defined in:
- lib/beaker/command.rb
Overview
An object that represents a “command” on a remote host. Is responsible for munging the environment correctly. Probably poorly named.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
An array of additional arguments to be supplied to the command.
-
#command ⇒ Object
A string representing the (possibly) incomplete command.
-
#environment ⇒ Object
A hash key-values where the keys are environment variables to be set.
-
#options ⇒ Object
A hash of options.
Instance Method Summary collapse
-
#args_string(args = @args) ⇒ String
String of the arguments for command.
-
#cmd_line(host, cmd = @command, env = @environment) ⇒ String
This returns the fully formed command line invocation.
-
#environment_string_for(host, env) ⇒ String
Construct the environment string for this command.
-
#initialize(command, args = [], options = {}) ⇒ Command
constructor
A new instance of Command.
-
#options_string(opts = @options) ⇒ String
String of the options and flags for command.
Constructor Details
#initialize(command, args = [], options = {}) ⇒ Command
For backwards compatability we must support any number of strings or symbols (or arrays of strings an symbols) and essentially ensure they are in a flattened array, coerced to strings, and call #join(‘ ’) on it. We have options for the command line invocation that must be turned into ‘–key=value’ and similarly joined as well as a hash of environment key value pairs, and finally we need a hash of options to control the default envs that are included.
Returns a new instance of Command.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/beaker/command.rb', line 47 def initialize command, args = [], = {} @command = command @options = @args = args @environment = {} @cmdexe = @options.delete(:cmdexe) || false # this is deprecated and will not allow you to use a command line # option of `--environment`, please use ENV instead. [:ENV, :environment, 'environment', 'ENV'].each do |k| if @options[k].is_a?(Hash) @environment = @environment.merge(@options.delete(k)) end end end |
Instance Attribute Details
#args ⇒ Object
An array of additional arguments to be supplied to the command
18 19 20 |
# File 'lib/beaker/command.rb', line 18 def args @args end |
#command ⇒ Object
A string representing the (possibly) incomplete command
9 10 11 |
# File 'lib/beaker/command.rb', line 9 def command @command end |
#environment ⇒ Object
A hash key-values where the keys are environment variables to be set
12 13 14 |
# File 'lib/beaker/command.rb', line 12 def environment @environment end |
#options ⇒ Object
A hash of options. Keys with values of nil are considered flags
15 16 17 |
# File 'lib/beaker/command.rb', line 15 def @options end |
Instance Method Details
#args_string(args = @args) ⇒ String
Returns String of the arguments for command.
117 118 119 |
# File 'lib/beaker/command.rb', line 117 def args_string args = @args args.flatten.compact.join(' ') end |
#cmd_line(host, cmd = @command, env = @environment) ⇒ String
Returns This returns the fully formed command line invocation.
70 71 72 73 74 75 76 77 |
# File 'lib/beaker/command.rb', line 70 def cmd_line host, cmd = @command, env = @environment env_string = env.nil? ? '' : environment_string_for( host, env ) cygwin = ((host['platform'] =~ /windows/) and @cmdexe) ? 'cmd.exe /c' : nil # This will cause things like `puppet -t -v agent` which is maybe bad. [env_string, cygwin, cmd, , args_string].compact.reject(&:empty?).join(' ') end |
#environment_string_for(host, env) ⇒ String
I dislike the principle of this method. There is host specific knowledge contained here. Really the relationship should be reversed where a host is asked for an appropriate Command when given a generic Command.
Construct the environment string for this command
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/beaker/command.rb', line 138 def environment_string_for host, env return '' if env.empty? env_array = [] env.each_key do |key| val = env[key] if val.is_a?(Array) val = val.join(':') else val = val.to_s end env_array << "#{key.to_s.upcase}=\"#{val}\"" end environment_string = env_array.join(' ') "env #{environment_string}" end |
#options_string(opts = @options) ⇒ String
Why no. Not the least bit Unixy, why do you ask?
Returns String of the options and flags for command.
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 |
# File 'lib/beaker/command.rb', line 84 def opts = @options flags = [] = opts.dup .each_key do |key| if [key] == nil flags << key .delete(key) end end short_flags, long_flags = flags.partition {|flag| flag.to_s.length == 1 } parsed_short_flags = short_flags.map {|f| "-#{f}" } parsed_long_flags = long_flags.map {|f| "--#{f}" } short_opts, long_opts = {}, {} .each_key do |key| if key.to_s.length == 1 short_opts[key] = [key] else long_opts[key] = [key] end end parsed_short_opts = short_opts.map {|k,v| "-#{k}=#{v}" } parsed_long_opts = long_opts.map {|k,v| "--#{k}=#{v}" } return (parsed_short_flags + parsed_long_flags + parsed_short_opts + parsed_long_opts).join(' ') end |