Class: PDK::CLI::Exec::Command
- Inherits:
-
Object
- Object
- PDK::CLI::Exec::Command
- Defined in:
- lib/pdk/cli/exec/command.rb
Direct Known Subclasses
Constant Summary collapse
- TEMPFILE_MODE =
File::RDWR | File::BINARY | File::CREAT | File::TRUNC
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#context ⇒ Object
Returns the value of attribute context.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#exec_group ⇒ Object
writeonly
Sets the attribute exec_group.
-
#spinner ⇒ TTY::Spinner?
readonly
private
The spinner for this command.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #add_spinner(message, opts = {}) ⇒ Object
-
#execute! ⇒ Hash[Symbol => Object]
The result from executing the command :stdout => String : The result of STDOUT :stderr => String : The result of STDERR :exit_code => Integer : The exit code from the command :duration => Float : Number seconds it took to execute.
-
#initialize(*argv) ⇒ Command
constructor
A new instance of Command.
- #register_spinner(spinner, opts = {}) ⇒ Object
- #update_environment(additional_env) ⇒ Object
Constructor Details
#initialize(*argv) ⇒ Command
Returns a new instance of Command.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pdk/cli/exec/command.rb', line 23 def initialize(*argv) require 'childprocess' require 'tempfile' @argv = argv @process = ChildProcess.build(*@argv) @process.leader = true @stdout = Tempfile.new('stdout', mode: TEMPFILE_MODE).tap { |io| io.sync = true } @stderr = Tempfile.new('stderr', mode: TEMPFILE_MODE).tap { |io| io.sync = true } @process.io.stdout = @stdout @process.io.stderr = @stderr # Default to running things in the system context. @context = :system # Extra environment vars to add to base set. @environment = {} # Register the ExecGroup when running in parallel @exec_group = nil end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
7 8 9 |
# File 'lib/pdk/cli/exec/command.rb', line 7 def argv @argv end |
#context ⇒ Object
Returns the value of attribute context.
8 9 10 |
# File 'lib/pdk/cli/exec/command.rb', line 8 def context @context end |
#environment ⇒ Object
Returns the value of attribute environment.
10 11 12 |
# File 'lib/pdk/cli/exec/command.rb', line 10 def environment @environment end |
#exec_group=(value) ⇒ Object (writeonly)
Sets the attribute exec_group
11 12 13 |
# File 'lib/pdk/cli/exec/command.rb', line 11 def exec_group=(value) @exec_group = value end |
#spinner ⇒ TTY::Spinner? (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.
The spinner for this command. This should only be used for testing
19 20 21 |
# File 'lib/pdk/cli/exec/command.rb', line 19 def spinner @spinner end |
#timeout ⇒ Object
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/pdk/cli/exec/command.rb', line 9 def timeout @timeout end |
Instance Method Details
#add_spinner(message, opts = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/pdk/cli/exec/command.rb', line 66 def add_spinner(, opts = {}) require 'pdk/cli/util' return unless PDK::CLI::Util.interactive? @success_message = opts.delete(:success) @failure_message = opts.delete(:failure) require 'pdk/cli/util/spinner' @spinner = TTY::Spinner.new("[:spinner] #{}", opts.merge(PDK::CLI::Util.spinner_opts_for_platform)) end |
#execute! ⇒ Hash[Symbol => Object]
Returns The result from executing the command :stdout => String : The result of STDOUT :stderr => String : The result of STDERR :exit_code => Integer : The exit code from the command :duration => Float : Number seconds it took to execute.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/pdk/cli/exec/command.rb', line 87 def execute! # Start spinning if configured. @spinner.auto_spin if @spinner # Set env for child process resolved_env_for_command.each { |k, v| @process.environment[k] = v } if [:module, :pwd].include?(context) require 'pdk/util' mod_root = PDK::Util.module_root unless mod_root @spinner.error if @spinner raise PDK::CLI::FatalError, _('Current working directory is not part of a module. (No metadata.json was found.)') end if Dir.pwd == mod_root || context == :pwd run_process_in_clean_env! else Dir.chdir(mod_root) do run_process_in_clean_env! end end else run_process! end # Stop spinning when done (if configured). stop_spinner @stdout.rewind @stderr.rewind process_data = { stdout: @stdout.read, stderr: @stderr.read, exit_code: @process.exit_code, duration: @duration, } PDK.logger.debug _('STDOUT: %{output}') % { output: process_data[:stdout].empty? ? 'N/A' : "\n#{process_data[:stdout]}", } PDK.logger.debug _('STDERR: %{output}') % { output: process_data[:stderr].empty? ? 'N/A' : "\n#{process_data[:stderr]}", } process_data ensure @stdout.close @stderr.close end |
#register_spinner(spinner, opts = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/pdk/cli/exec/command.rb', line 56 def register_spinner(spinner, opts = {}) require 'pdk/cli/util' return unless PDK::CLI::Util.interactive? @success_message = opts.delete(:success) @failure_message = opts.delete(:failure) @spinner = spinner end |
#update_environment(additional_env) ⇒ Object
78 79 80 |
# File 'lib/pdk/cli/exec/command.rb', line 78 def update_environment(additional_env) @environment.merge!(additional_env) end |