Class: TrainSH::Session
- Inherits:
-
Object
- Object
- TrainSH::Session
- Extended by:
- Forwardable
- Defined in:
- lib/trainsh/session.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#env ⇒ Object
Returns the value of attribute env.
-
#exitcode ⇒ Object
Returns the value of attribute exitcode.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#ping ⇒ Object
Returns the value of attribute ping.
-
#pwd ⇒ Object
Returns the value of attribute pwd.
Instance Method Summary collapse
- #connect(url) ⇒ Object
- #disconnect ⇒ Object
-
#initialize(url = nil) ⇒ Session
constructor
A new instance of Session.
- #reconnect ⇒ Object
- #run(command, skip_affixes: false) ⇒ Object
- #run_idle ⇒ Object
-
#url ⇒ Object
Redact password information.
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
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
89 90 91 |
# File 'lib/trainsh/session.rb', line 89 def backend @backend end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
89 90 91 |
# File 'lib/trainsh/session.rb', line 89 def connection @connection end |
#env ⇒ Object
Returns the value of attribute env.
91 92 93 |
# File 'lib/trainsh/session.rb', line 91 def env @env end |
#exitcode ⇒ Object
Returns the value of attribute exitcode.
91 92 93 |
# File 'lib/trainsh/session.rb', line 91 def exitcode @exitcode end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
89 90 91 |
# File 'lib/trainsh/session.rb', line 89 def host @host end |
#ping ⇒ Object
Returns the value of attribute ping.
91 92 93 |
# File 'lib/trainsh/session.rb', line 91 def ping @ping end |
#pwd ⇒ Object
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 |
#disconnect ⇒ Object
117 118 119 120 121 |
# File 'lib/trainsh/session.rb', line 117 def disconnect puts "Closing session #{url}" connection.close end |
#reconnect ⇒ Object
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_idle ⇒ Object
156 157 158 |
# File 'lib/trainsh/session.rb', line 156 def run_idle @ping = ::Benchmark.measure { run('#', skip_affixes: true) }.real * 1000 end |
#url ⇒ Object
Redact password information
130 131 132 |
# File 'lib/trainsh/session.rb', line 130 def url Addressable::URI.parse(@url).omit(:password).to_s end |