Class: TTY::Command::Cmd
- Inherits:
-
Object
- Object
- TTY::Command::Cmd
- Defined in:
- lib/tty/command/cmd.rb
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
A string arguments.
-
#command ⇒ Object
readonly
A string command name, or shell program.
-
#only_output_on_error ⇒ Object
readonly
Flag that controls whether to print the output only on error or not.
-
#options ⇒ Object
readonly
Hash of operations to peform.
-
#uuid ⇒ Object
readonly
Unique identifier.
Instance Method Summary collapse
- #chdir(value) ⇒ Object
-
#environment ⇒ Object
The shell environment variables.
- #environment_string ⇒ Object
- #evars(value, &block) ⇒ Object
- #group(value) ⇒ Object
-
#initialize(env_or_cmd, *args) ⇒ Cmd
constructor
private
Initialize a new Cmd object.
-
#to_command ⇒ Object
Assemble full command.
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #umask(value) ⇒ Object
-
#update(options) ⇒ Object
Extend command options if keys don’t already exist.
- #user(value) ⇒ Object
-
#with_clean_env ⇒ Object
Clear environment variables except specified by env.
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
#argv ⇒ Object (readonly)
A string arguments
15 16 17 |
# File 'lib/tty/command/cmd.rb', line 15 def argv @argv end |
#command ⇒ Object (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_error ⇒ Object (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 |
#options ⇒ Object (readonly)
Hash of operations to peform
19 20 21 |
# File 'lib/tty/command/cmd.rb', line 19 def @options end |
#uuid ⇒ Object (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 [:chdir] %(cd #{Shellwords.escape([:chdir])} && #{value}) end |
#environment ⇒ Object
The shell environment variables
73 74 75 |
# File 'lib/tty/command/cmd.rb', line 73 def environment @env.merge(.fetch(:env, {})) end |
#environment_string ⇒ Object
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 [:group] %(sg #{[:group]} -c \\\"%s\\\") % [value] end |
#to_command ⇒ Object
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_hash ⇒ Object
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_s ⇒ Object
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 [:umask] %(umask #{[: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.update(.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 [:user] vars = environment.any? ? "#{environment_string} " : '' %(sudo -u #{[:user]} #{vars}-- sh -c '%s') % [value] end |
#with_clean_env ⇒ Object
Clear environment variables except specified by env
114 115 |
# File 'lib/tty/command/cmd.rb', line 114 def with_clean_env end |