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.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #add_spinner(message, opts = {}) ⇒ Object
- #execute! ⇒ Object
-
#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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pdk/cli/exec/command.rb', line 15 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 |
#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
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pdk/cli/exec/command.rb', line 58 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! ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 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 |
# File 'lib/pdk/cli/exec/command.rb', line 74 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
48 49 50 51 52 53 54 55 56 |
# File 'lib/pdk/cli/exec/command.rb', line 48 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
70 71 72 |
# File 'lib/pdk/cli/exec/command.rb', line 70 def update_environment(additional_env) @environment.merge!(additional_env) end |