class Beaker::Command

@api public
for munging the environment correctly. Probably poorly named.
An object that represents a “command” on a remote host. Is responsible

def args_string args = @args

Returns:
  • (String) - String of the arguments for command.

Parameters:
  • args (Array) -- An array of arguments to the command.
def args_string args = @args
  args.flatten.compact.join(' ')
end

def cmd_line host, cmd = @command, env = @environment, pc = @prepend_cmds

Returns:
  • (String) - This returns the fully formed command line invocation.

Parameters:
  • pc (String) -- An optional list of commands to prepend
  • env (Hash) -- An optional hash of environment variables to be used
  • cmd (String) -- An command to call.
  • host (Host) -- An object that implements {Beaker::Host}'s
def cmd_line host, cmd = @command, env = @environment, pc = @prepend_cmds
  env_string = env.nil? ? '' : environment_string_for( host, env )
  cygwin = ((host['platform'] =~ /windows/) and host.is_cygwin? and @cmdexe) ? 'cmd.exe /c' : nil
  # This will cause things like `puppet -t -v agent` which is maybe bad.
  [env_string, cygwin, pc, cmd, options_string, args_string].compact.reject(&:empty?).join(' ')
end

def environment_string_for host, env

Other tags:
    Note: - I dislike the principle of this method. There is host specific

Returns:
  • (String) - Returns a string containing command line arguments that

Parameters:
  • env (Hash{String=>String}) -- An optional Hash containing
  • host (Host) -- A Host object
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
  if not host.is_powershell?
    environment_string = env_array.join(' ')
    "env #{environment_string}"
  else
    environment_string = ''
    env_array.each_with_index do |env|
      environment_string += "set #{env} && "
    end
    environment_string
  end
end

def initialize command, args = [], options = {}

Other tags:
    Note: - For backwards compatability we must support any number of strings

Other tags:
    Example: My favorite example of a signature that we must maintain -
    Example: Recommended usage programmatically: -

Parameters:
  • options (Hash) -- These are addition options to the command. They
  • args (Array) -- These are addition arguments to the command
  • command (String) -- The program to call, either an absolute path
def initialize command, args = [], options = {}
  @command = command
  @options = options
  @args    = args
  @environment = {}
  @cmdexe = @options.delete(:cmdexe) || false
  @prepend_cmds = @options.delete(:prepend_cmds) || nil
  # 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

def options_string opts = @options

Other tags:
    Note: - Why no. Not the least bit Unixy, why do you ask?

Returns:
  • (String) - String of the options and flags for command.

Parameters:
  • opts (Hash) -- These are the options that the command takes
def options_string opts = @options
  flags = []
  options = opts.dup
  options.each_key do |key|
    if options[key] == nil
      flags << key
      options.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 = {}, {}
  options.each_key do |key|
    if key.to_s.length == 1
      short_opts[key] = options[key]
    else
      long_opts[key] = options[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