Class: PDK::CLI::Exec::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/cli/exec/command.rb

Direct Known Subclasses

InteractiveCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*argv) ⇒ Command

Returns a new instance of Command.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pdk/cli/exec/command.rb', line 22

def initialize(*argv)
  @argv = argv

  @process = ChildProcess.build(*@argv)
  @process.leader = true

  @stdout = Tempfile.new('stdout').tap { |io| io.sync = true }
  @stderr = Tempfile.new('stderr').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

#argvObject (readonly)

Returns the value of attribute argv.



16
17
18
# File 'lib/pdk/cli/exec/command.rb', line 16

def argv
  @argv
end

#contextObject

Returns the value of attribute context.



17
18
19
# File 'lib/pdk/cli/exec/command.rb', line 17

def context
  @context
end

#environmentObject

Returns the value of attribute environment.



19
20
21
# File 'lib/pdk/cli/exec/command.rb', line 19

def environment
  @environment
end

#exec_group=(value) ⇒ Object (writeonly)

Sets the attribute exec_group

Parameters:

  • value

    the value to set the attribute exec_group to.



20
21
22
# File 'lib/pdk/cli/exec/command.rb', line 20

def exec_group=(value)
  @exec_group = value
end

#timeoutObject

Returns the value of attribute timeout.



18
19
20
# File 'lib/pdk/cli/exec/command.rb', line 18

def timeout
  @timeout
end

Instance Method Details

#add_spinner(message, opts = {}) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/pdk/cli/exec/command.rb', line 60

def add_spinner(message, opts = {})
  return unless PDK::CLI::Util.interactive?
  @success_message = opts.delete(:success)
  @failure_message = opts.delete(:failure)

  @spinner = TTY::Spinner.new("[:spinner] #{message}", opts.merge(PDK::CLI::Util.spinner_opts_for_platform))
end

#execute!Object



72
73
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
# File 'lib/pdk/cli/exec/command.rb', line 72

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)
    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



52
53
54
55
56
57
58
# File 'lib/pdk/cli/exec/command.rb', line 52

def register_spinner(spinner, opts = {})
  return unless PDK::CLI::Util.interactive?
  @success_message = opts.delete(:success)
  @failure_message = opts.delete(:failure)

  @spinner = spinner
end

#update_environment(additional_env) ⇒ Object



68
69
70
# File 'lib/pdk/cli/exec/command.rb', line 68

def update_environment(additional_env)
  @environment.merge!(additional_env)
end