Class: SwarmClusterCliOpe::ShellCommandResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
LoggerConcern
Defined in:
lib/swarm_cluster_cli_ope/shell_command_response.rb

Overview

Identifica una risposta dalla shell

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerConcern

#logger

Constructor Details

#initialize(result) ⇒ ShellCommandResponse

Returns a new instance of ShellCommandResponse.

Parameters:

  • result (Hash)

    composto da: stdout: [String], stderr: [String], pid: [Integer], status: [Process::Status]



18
19
20
# File 'lib/swarm_cluster_cli_ope/shell_command_response.rb', line 18

def initialize(result)
  @raw_result = result
end

Instance Attribute Details

#raw_resultString

Returns:

  • (String)


11
12
13
# File 'lib/swarm_cluster_cli_ope/shell_command_response.rb', line 11

def raw_result
  @raw_result
end

Instance Method Details

#failed?Boolean

Controlla se il valore di status è diverso da 0

Returns:

  • (Boolean)


56
57
58
# File 'lib/swarm_cluster_cli_ope/shell_command_response.rb', line 56

def failed?
  raw_result[:status].exitstatus.to_i != 0
end

#result(object_class: OpenStruct, single: false) ⇒ Array<object_class>, Object

## # Ritorna una versione stampabile del risultato def to_s

raw_result[:stdout]

end

Risultato, essendo sempre composto da una lista di righe in formato json, ritorniamo un array di json

Parameters:

  • object_class (Object) (defaults to: OpenStruct)

Returns:

  • (Array<object_class>, Object)


32
33
34
35
36
37
38
39
40
41
# File 'lib/swarm_cluster_cli_ope/shell_command_response.rb', line 32

def result(object_class: OpenStruct, single: false)
  #tento prima di estrapolare direttamente da json e sucessivamente come array
  if single
    # questo per k8s, dato che abbiamo come risposta un json vero
    object_class.new(JSON.parse(raw_result[:stdout]))
  else
    # questo nel caso siamo in swarm che ci ritorna un array di json
    raw_result[:stdout].split("\n").collect { |s| object_class.new(JSON.parse(s)) }
  end
end

#single_obj(object_class: OpenStruct) ⇒ Object

Parameters:

  • object_class (Class<OpenStruct>) (defaults to: OpenStruct)

Returns:

  • (Object)


45
46
47
# File 'lib/swarm_cluster_cli_ope/shell_command_response.rb', line 45

def single_obj(object_class: OpenStruct)
  result(object_class: object_class, single: true)
end

#stderrObject

Ritorna l’errore della shell



69
70
71
# File 'lib/swarm_cluster_cli_ope/shell_command_response.rb', line 69

def stderr
  raw_result[:stderr]
end

#success?TrueClass, FalseClass

Inverso di :failed?

Returns:

  • (TrueClass, FalseClass)


63
64
65
# File 'lib/swarm_cluster_cli_ope/shell_command_response.rb', line 63

def success?
  !failed?
end