Class: TTY::Command::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/command/cmd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_or_cmd, *args) ⇒ Cmd

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new Cmd object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#argvObject (readonly)

A string arguments


15
16
17
# File 'lib/tty/command/cmd.rb', line 15

def argv
  @argv
end

#commandObject (readonly)

A string command name, or shell program


11
12
13
# File 'lib/tty/command/cmd.rb', line 11

def command
  @command
end

#only_output_on_errorObject (readonly)

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


26
27
28
# File 'lib/tty/command/cmd.rb', line 26

def only_output_on_error
  @only_output_on_error
end

#optionsObject (readonly)

Hash of operations to peform


19
20
21
# File 'lib/tty/command/cmd.rb', line 19

def options
  @options
end

#uuidObject (readonly)

Unique identifier


23
24
25
# File 'lib/tty/command/cmd.rb', line 23

def uuid
  @uuid
end

Instance Method Details

#chdir(value) ⇒ Object


95
96
97
98
# File 'lib/tty/command/cmd.rb', line 95

def chdir(value)
  return value unless options[:chdir]
  %(cd #{Shellwords.escape(options[:chdir])} && #{value})
end

#environmentObject

The shell environment variables


73
74
75
# File 'lib/tty/command/cmd.rb', line 73

def environment
  @env.merge(options.fetch(:env, {}))
end

#environment_stringObject


77
78
79
80
81
82
83
# 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) ⇒ Object


85
86
87
88
# 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) ⇒ Object


106
107
108
109
# File 'lib/tty/command/cmd.rb', line 106

def group(value)
  return value unless options[:group]
  %(sg #{options[:group]} -c \\\"%s\\\") % [value]
end

#to_commandObject

Assemble full command


120
121
122
# File 'lib/tty/command/cmd.rb', line 120

def to_command
  chdir(umask(evars(user(group(to_s)))))
end

#to_hashObject


130
131
132
133
134
135
136
# File 'lib/tty/command/cmd.rb', line 130

def to_hash
  {
    command: command,
    argv:    argv,
    uuid:    uuid
  }
end

#to_sObject


125
126
127
# File 'lib/tty/command/cmd.rb', line 125

def to_s
  [command.to_s, *Array(argv)].join(" ")
end

#umask(value) ⇒ Object


90
91
92
93
# File 'lib/tty/command/cmd.rb', line 90

def umask(value)
  return value unless options[:umask]
  %(umask #{options[:umask]} && %s) % [value]
end

#update(options) ⇒ Object

Extend command options if keys don't already exist


66
67
68
# File 'lib/tty/command/cmd.rb', line 66

def update(options)
  @options.update(options.update(@options))
end

#user(value) ⇒ Object


100
101
102
103
104
# 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_envObject

Clear environment variables except specified by env


114
115
# File 'lib/tty/command/cmd.rb', line 114

def with_clean_env
end