Class: TrainSH::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Session

Returns a new instance of Session.



93
94
95
# File 'lib/trainsh/session.rb', line 93

def initialize(url = nil)
  connect(url) unless url.nil?
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



89
90
91
# File 'lib/trainsh/session.rb', line 89

def backend
  @backend
end

#connectionObject (readonly)

Returns the value of attribute connection.



89
90
91
# File 'lib/trainsh/session.rb', line 89

def connection
  @connection
end

#envObject

Returns the value of attribute env.



91
92
93
# File 'lib/trainsh/session.rb', line 91

def env
  @env
end

#exitcodeObject

Returns the value of attribute exitcode.



91
92
93
# File 'lib/trainsh/session.rb', line 91

def exitcode
  @exitcode
end

#hostObject (readonly)

Returns the value of attribute host.



89
90
91
# File 'lib/trainsh/session.rb', line 89

def host
  @host
end

#pingObject

Returns the value of attribute ping.



91
92
93
# File 'lib/trainsh/session.rb', line 91

def ping
  @ping
end

#pwdObject

Returns the value of attribute pwd.



91
92
93
# File 'lib/trainsh/session.rb', line 91

def pwd
  @pwd
end

Instance Method Details

#connect(url) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/trainsh/session.rb', line 97

def connect(url)
  @url = url

  data = Train.unpack_target_from_uri(url)

  # TODO: Wire up with "messy" parameter
  data[:cleanup] = false

  backend = Train.create(data[:backend], data)
  return false unless backend

  @backend = data[:backend]
  @host = data[:host]

  @connection = backend.connection
  connection.wait_until_ready

  at_exit { disconnect }
end

#disconnectObject



117
118
119
120
121
# File 'lib/trainsh/session.rb', line 117

def disconnect
  puts "Closing session #{url}"

  connection.close
end

#reconnectObject



123
124
125
126
127
# File 'lib/trainsh/session.rb', line 123

def reconnect
  disconnect

  connect(url)
end

#run(command, skip_affixes: false) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/trainsh/session.rb', line 134

def run(command, skip_affixes: false)
  command = Command.new(command, @connection)

  # Save exit code
  command.postfix(exitcode_get) { |output| @exitcode = output.to_i }

  # Request UTF-8 instead of UTF-16
  # command.prefix("$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'") if platform.windows?

  unless skip_affixes
    command.prefix(pwd_set)
    command.postfix(pwd_get) { |output| @pwd = output }
    command.prefix(env_set)
    command.postfix(env_get) { |output| @env = output }
  end

  # Discovery tasks
  command.prefix(host_get) { |output| @host = output } if host.nil? || host == 'unknown'

  command.run
end

#run_idleObject



156
157
158
# File 'lib/trainsh/session.rb', line 156

def run_idle
  @ping = ::Benchmark.measure { run('#', skip_affixes: true) }.real * 1000
end

#urlObject

Redact password information



130
131
132
# File 'lib/trainsh/session.rb', line 130

def url
  Addressable::URI.parse(@url).omit(:password).to_s
end