Class: TTY::Command::Cmd Private

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

Overview

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

Encapsulates the executed command

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



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
62
63
64
65
# File 'lib/tty/command/cmd.rb', line 34

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



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

def argv
  @argv
end

#commandObject (readonly)

A string command name, or shell program



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

def command
  @command
end

#only_output_on_errorObject (readonly)

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.

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



29
30
31
# File 'lib/tty/command/cmd.rb', line 29

def only_output_on_error
  @only_output_on_error
end

#optionsObject (readonly)

Hash of operations to peform



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

def options
  @options
end

#uuidObject (readonly)

Unique identifier



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

def uuid
  @uuid
end

Instance Method Details

#chdir(value) ⇒ Object

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.



101
102
103
104
105
# File 'lib/tty/command/cmd.rb', line 101

def chdir(value)
  return value unless options[:chdir]

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

#environmentObject

The shell environment variables



77
78
79
# File 'lib/tty/command/cmd.rb', line 77

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

#environment_stringObject

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.



81
82
83
84
85
86
87
# File 'lib/tty/command/cmd.rb', line 81

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

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.



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

def evars(value, &block)
  return (value || block) unless environment.any?

  "( export #{environment_string} ; #{value || block.call} )"
end

#group(value) ⇒ Object

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.



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

def group(value)
  return value unless options[:group]

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

#to_commandObject

Assemble full command



129
130
131
# File 'lib/tty/command/cmd.rb', line 129

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

#to_hashObject



139
140
141
142
143
144
145
# File 'lib/tty/command/cmd.rb', line 139

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

#to_sObject



134
135
136
# File 'lib/tty/command/cmd.rb', line 134

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

#umask(value) ⇒ Object

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.



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

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



70
71
72
# File 'lib/tty/command/cmd.rb', line 70

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

#user(value) ⇒ Object

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.



107
108
109
110
111
112
# File 'lib/tty/command/cmd.rb', line 107

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



123
124
# File 'lib/tty/command/cmd.rb', line 123

def with_clean_env
end