Class: CemWinSpec::WinExec::LocalCmd

Inherits:
BaseCmd
  • Object
show all
Defined in:
lib/cem_win_spec/win_exec/cmd/local_cmd.rb

Overview

Class for executing local shell commands

Constant Summary

Constants inherited from BaseCmd

BaseCmd::COMMAND_SEPARATOR, BaseCmd::PUPPET_VER_TO_RUBY_VER, BaseCmd::TOOL_DIR_BY_RUBY_VER

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Attributes inherited from BaseCmd

#env_vars, #puppet_version, #working_dir

Instance Method Summary collapse

Methods inherited from BaseCmd

#command, #ruby_version

Methods included from Logging

#current_log_format, current_log_format, current_log_level, #current_log_level, included, log_setup!, #log_setup!, logger, #logger, new_log_formatter, #new_log_formatter, new_log_level, #new_log_level

Constructor Details

#initialize(working_dir = nil, puppet_version: nil, **env_vars) ⇒ LocalCmd

Returns a new instance of LocalCmd.



12
13
14
15
16
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 12

def initialize(working_dir = nil, puppet_version: nil, **env_vars)
  @ran_in_thread = false
  @thread_results = {}
  super(working_dir, puppet_version: puppet_version, **env_vars)
end

Instance Attribute Details

#thread_resultsObject (readonly)

Returns the value of attribute thread_results.



10
11
12
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 10

def thread_results
  @thread_results
end

Instance Method Details

#any_threads?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 26

def any_threads?
  @ran_in_thread && thread_group.list.any?
end

#available?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 18

def available?
  true
end

#bg_run(cmd, *_args, **kwargs) ⇒ Object

Spawn a new process and detach it from the current process This is useful for running commands that should not block the current process and that you don’t need stdout/stderr from.

Parameters:

  • cmd (String)

    The command to execute



38
39
40
41
42
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 38

def bg_run(cmd, *_args, **kwargs)
  cmd = command(cmd, **kwargs)
  log_command(cmd)
  Process.detach(spawn())
end

#join_threadsObject



30
31
32
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 30

def join_threads
  thread_group.list.each(&:join)
end

#ran_in_thread?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 22

def ran_in_thread?
  @ran_in_thread
end

#run(cmd, *_args, **kwargs) ⇒ Object



64
65
66
67
68
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 64

def run(cmd, *_args, **kwargs)
  cmd = command(cmd, **kwargs)
  log_command(cmd)
  Open3.capture3(cmd)
end

#thread_run(cmd, *_args, **kwargs) ⇒ Array

Execute a command in a new thread. This works by creating a new thread in a thread group and then executing the command in that thread. The thread group is used to keep track of all the threads that have been created and to join / exit / kill them all at once. The command results are stored in a hash with the command as the key and the results as the value. The results are stored as an array of [stdout, stderr, status] where status is a Process::Status object.

Parameters:

  • cmd (String)

    The command to execute

Returns:

  • (Array)

    An array of [:threaded, cmd]



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cem_win_spec/win_exec/cmd/local_cmd.rb', line 52

def thread_run(cmd, *_args, **kwargs)
  @ran_in_thread = true
  th = Thread.new do
    cmd = command(cmd, **kwargs)
    log_command(cmd)
    so, se, st = Open3.capture3(cmd)
    @thread_results[cmd] = [so, se, st]
  end
  thread_group.add th
  :threaded
end