class SwarmClusterCliOpe::ShellCommandExecution

Attributes

cmd[RW]

@return [Array<String>] comando da eseguire

Public Class Methods

new(cmd) click to toggle source

@param [Array<String>,String] cmd

# 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

Public Instance Methods

add(*append_command) click to toggle source

@return [SwarmClusterCliOpe::ShellCommandExecution] @param [*String] append_command

# 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) click to toggle source

Esegue il comando e ritorna STDOUT, altrimenti se va in errore esegue un raise @return [ShellCommandResponse] @param [FalseClass] allow_failure -> se impostato a true, ritorniamo risultato anche quando fallisce

# 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_command() click to toggle source

Stampa il comando @return [String]

# File lib/swarm_cluster_cli_ope/shell_command_execution.rb, line 62
def string_command
  @cmd.join(' ')
end