class TTY::Command::Cmd

Attributes

argv[R]

A string arguments @api public

command[R]

A string command name, or shell program @api public

only_output_on_error[R]

Flag that controls whether to print the output only on error or not

options[R]

Hash of operations to peform @api public

uuid[R]

Unique identifier @api public

Public Class Methods

new(env_or_cmd, *args) click to toggle source

Initialize a new Cmd object

@api private

# File lib/tty/command/cmd.rb, line 31
def initialize(env_or_cmd, *args)
  opts = args.last.respond_to?(:to_hash) ? args.pop : {}
  if env_or_cmd.respond_to?(:to_hash)
    @env = env_or_cmd
    unless command = args.shift
      raise ArgumentError, 'Cmd requires command argument'
    end
  else
    command = env_or_cmd
  end

  if args.empty? && cmd = command.to_s
    raise ArgumentError, 'No command provided' if cmd.empty?
    @command = sanitize(cmd)
    @argv = []
  else
    if command.respond_to?(:to_ary)
      @command = sanitize(command[0])
      args.unshift(*command[1..-1])
    else
      @command = sanitize(command)
    end
    @argv = args.map { |i| Shellwords.escape(i) }
  end
  @env ||= {}
  @options = opts

  @uuid = SecureRandom.uuid.split('-')[0]
  @only_output_on_error = opts.fetch(:only_output_on_error) { false }
  freeze
end

Public Instance Methods

chdir(value) click to toggle source
# File lib/tty/command/cmd.rb, line 95
def chdir(value)
  return value unless options[:chdir]
  %(cd #{Shellwords.escape(options[:chdir])} && #{value})
end
environment() click to toggle source

The shell environment variables

@api public

# File lib/tty/command/cmd.rb, line 73
def environment
  @env.merge(options.fetch(:env, {}))
end
environment_string() click to toggle source
# File lib/tty/command/cmd.rb, line 77
def environment_string
  environment.map do |key, val|
    converted_key = key.is_a?(Symbol) ? key.to_s.upcase : key.to_s
    escaped_val = val.to_s.gsub(/"/, '\"')
    %(#{converted_key}="#{escaped_val}")
  end.join(' ')
end
evars(value, &block) click to toggle source
# File lib/tty/command/cmd.rb, line 85
def evars(value, &block)
  return (value || block) unless environment.any?
  "( export #{environment_string} ; #{value || block.call} )"
end
group(value) click to toggle source
# File lib/tty/command/cmd.rb, line 106
def group(value)
  return value unless options[:group]
  %(sg #{options[:group]} -c \\\"%s\\\") % [value]
end
to_command() click to toggle source

Assemble full command

@api public

# File lib/tty/command/cmd.rb, line 120
def to_command
  chdir(umask(evars(user(group(to_s)))))
end
to_hash() click to toggle source

@api public

# File lib/tty/command/cmd.rb, line 130
def to_hash
  {
    command: command,
    argv:    argv,
    uuid:    uuid
  }
end
to_s() click to toggle source

@api public

# File lib/tty/command/cmd.rb, line 125
def to_s
  [command.to_s, *Array(argv)].join(' ')
end
umask(value) click to toggle source
# File lib/tty/command/cmd.rb, line 90
def umask(value)
  return value unless options[:umask]
  %(umask #{options[:umask]} && %s) % [value]
end
update(options) click to toggle source

Extend command options if keys don't already exist

@api public

# File lib/tty/command/cmd.rb, line 66
def update(options)
  @options.update(options.update(@options))
end
user(value) click to toggle source
# File lib/tty/command/cmd.rb, line 100
def user(value)
  return value unless options[:user]
  vars = environment.any? ? "#{environment_string} " : ''
  %(sudo -u #{options[:user]} #{vars}-- sh -c '%s') % [value]
end
with_clean_env() click to toggle source

Clear environment variables except specified by env

@api public

# File lib/tty/command/cmd.rb, line 114
def with_clean_env
end

Private Instance Methods

sanitize(value) click to toggle source

Coerce to string

@api private

# File lib/tty/command/cmd.rb, line 143
def sanitize(value)
  value.to_s.dup
end