class TrainSH::Session

Attributes

backend[R]
connection[R]
env[RW]
exitcode[RW]
host[R]
ping[RW]
pwd[RW]

Public Class Methods

new(url = nil) click to toggle source
# File lib/trainsh/session.rb, line 93
def initialize(url = nil)
  connect(url) unless url.nil?
end

Public Instance Methods

connect(url) click to toggle source
# 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() click to toggle source
# File lib/trainsh/session.rb, line 117
def disconnect
  puts "Closing session #{url}"

  connection.close
end
reconnect() click to toggle source
# File lib/trainsh/session.rb, line 123
def reconnect
  disconnect

  connect(url)
end
run(command, skip_affixes: false) click to toggle source
# 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() click to toggle source
# File lib/trainsh/session.rb, line 156
def run_idle
  @ping = ::Benchmark.measure { run('#', skip_affixes: true) }.real * 1000
end
url() click to toggle source

Redact password information

# File lib/trainsh/session.rb, line 130
def url
  Addressable::URI.parse(@url).omit(:password).to_s
end

Private Instance Methods

env_get() click to toggle source

TODO: Preserve Windows environment variables

# File lib/trainsh/session.rb, line 181
def env_get
  platform.windows? ? '' : 'export'
end
env_set() click to toggle source

TODO: Preserve Windows environment variables

# File lib/trainsh/session.rb, line 186
def env_set
  platform.windows? ? '' : env
end
exitcode_get() click to toggle source
# File lib/trainsh/session.rb, line 162
def exitcode_get
  "echo $#{TrainSH::EXITCODE_VAR}"
end
host_get() click to toggle source
# File lib/trainsh/session.rb, line 166
def host_get
  'hostname'
end
pwd_get() click to toggle source
# File lib/trainsh/session.rb, line 170
def pwd_get
  platform.windows? ? '(Get-Location).Path' : 'pwd'
end
pwd_set(path = pwd) click to toggle source
# File lib/trainsh/session.rb, line 174
def pwd_set(path = pwd)
  return '' if path.nil?

  platform.windows? ? "Set-Location #{path}" : "cd #{path}"
end