Class: TrainSH::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/trainsh/session.rb

Constant Summary collapse

MAGIC_STRING =

Used for command separation, randomly generated

'mVDK6afaqa6fb7kcMqTpR2aoUFbYsRt889G4eGoI'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, connection) ⇒ Command

Returns a new instance of Command.



13
14
15
16
17
18
19
# File 'lib/trainsh/session.rb', line 13

def initialize(command, connection)
  @command = command
  @connection = connection

  @prefixes = []
  @postfixes = []
end

Instance Attribute Details

#connection=(value) ⇒ Object (writeonly)

Sets the attribute connection

Parameters:

  • value

    the value to set the attribute connection to.



11
12
13
# File 'lib/trainsh/session.rb', line 11

def connection=(value)
  @connection = value
end

Instance Method Details

#aggregate_commandsObject



71
72
73
74
75
76
77
78
79
# File 'lib/trainsh/session.rb', line 71

def aggregate_commands
  separator = "\necho #{MAGIC_STRING}\n"

  commands = @prefixes.reverse.map { |ary| ary[:command] }
  commands << "#{@command}\n#{save_exit_code}"
  commands.concat(@postfixes.map { |ary| ary[:command] })

  commands.join(separator)
end

#parse(result) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/trainsh/session.rb', line 63

def parse(result)
  result.stdout
        .gsub(/\r\n/, "\n")
        .gsub(/ *$/, '')
        .split(MAGIC_STRING)
        .map(&:strip)
end

#postfix(postfix_command, &block) ⇒ Object



28
29
30
31
32
33
# File 'lib/trainsh/session.rb', line 28

def postfix(postfix_command, &block)
  @postfixes << {
    command: postfix_command,
    block: block
  }
end

#prefix(prefix_command, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/trainsh/session.rb', line 21

def prefix(prefix_command, &block)
  @prefixes << {
    command: prefix_command,
    block: block
  }
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/trainsh/session.rb', line 35

def run
  result = @connection.run_command aggregate_commands
  stdouts = parse(result)

  prefixes_stdout = stdouts.first(@prefixes.count).reverse
  @prefixes.each_with_index do |prefix, idx|
    next if prefix[:block].nil?

    prefix[:block].call prefixes_stdout[idx]
  end
  @prefixes.count.times { stdouts.shift } unless @prefixes.empty?

  postfixes_stdout = stdouts.last(@postfixes.count)
  @postfixes.each_with_index do |postfix, idx|
    next if postfix[:block].nil?

    postfix[:block].call postfixes_stdout[idx]
  end
  @postfixes.count.times { stdouts.pop } unless @postfixes.empty?

  raise 'Pre-/Postfix command processing ended up with more than one remaining stdout' if stdouts.count > 1

  result.stdout = stdouts.first
  # result.stderr = "" # TODO
  result.exit_status = 0 # TODO
  result
end

#save_exit_codeObject



81
82
83
# File 'lib/trainsh/session.rb', line 81

def save_exit_code
  @connection.platform.windows? ? "$#{TrainSH::EXITCODE_VAR}=$LastExitCode" : "export #{TrainSH::EXITCODE_VAR}=$?"
end