Class: SwarmClusterCliOpe::ShellCommandExecution

Inherits:
Object
  • Object
show all
Includes:
LoggerConcern
Defined in:
lib/swarm_cluster_cli_ope/shell_command_execution.rb

Defined Under Namespace

Classes: Failure

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerConcern

#logger

Constructor Details

#initialize(cmd) ⇒ ShellCommandExecution



12
13
14
15
# File 'lib/swarm_cluster_cli_ope/shell_command_execution.rb', line 12

def initialize(cmd)
  cmd = cmd.split(" ") if cmd.is_a? String
  @cmd = cmd
end

Instance Attribute Details

#cmdArray<String>



9
10
11
# File 'lib/swarm_cluster_cli_ope/shell_command_execution.rb', line 9

def cmd
  @cmd
end

Instance Method Details

#add(*append_command) ⇒ SwarmClusterCliOpe::ShellCommandExecution



19
20
21
22
# File 'lib/swarm_cluster_cli_ope/shell_command_execution.rb', line 19

def add(*append_command)
  @cmd.append(append_command)
  self
end

#execute(allow_failure: false) ⇒ ShellCommandResponse

Esegue il comando e ritorna STDOUT, altrimenti se va in errore esegue un raise



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/swarm_cluster_cli_ope/shell_command_execution.rb', line 34

def execute(allow_failure: false)
  result = {
    stdout: nil,
    stderr: nil,
    pid: nil,
    status: nil
  }
  logger.info { "SHELL: #{string_command}" }
  result[:status] = Open4::popen4(string_command) do |pid, stdin, stdout, stderr|
    stdin.close

    result[:stdout] = stdout.read.strip
    result[:stderr] = stderr.read.strip
    result[:pid] = pid
  end

  unless allow_failure
    raise Failure.new(cmd, result[:stderr]) if (result[:status] && result[:status].exitstatus != 0)
  end

  logger.debug { "SHELL_RESPONSE: #{JSON.pretty_generate(result)}" }

  ShellCommandResponse.new(result)
end

#string_commandString

Stampa il comando



62
63
64
# File 'lib/swarm_cluster_cli_ope/shell_command_execution.rb', line 62

def string_command
  @cmd.join(' ')
end