Module: Wokku::Commands::Shell

Defined in:
lib/wokku/commands/shell.rb

Class Method Summary collapse

Class Method Details

.derive_cable_url(api_url) ⇒ Object



60
61
62
63
64
65
# File 'lib/wokku/commands/shell.rb', line 60

def derive_cable_url(api_url)
  u = URI(api_url)
  scheme = u.scheme == "https" ? "wss" : "ws"
  port = (u.port && ![80, 443].include?(u.port)) ? ":#{u.port}" : ""
  "#{scheme}://#{u.host}#{port}/cable"
end

.run!(mode:, target_kind:, target:, argv:, force_tty:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wokku/commands/shell.rb', line 41

def run!(mode:, target_kind:, target:, argv:, force_tty:)
  resource_path = target_kind == :app ? "/apps/#{target}" : "/databases/#{target}"
  resource = api(:get, resource_path)
  server_id = resource["server_id"] or abort "could not resolve server for #{target}"

  token = Wokku::Config.api_token or abort "not logged in — run `wokku auth:login`"
  cable_url = derive_cable_url(Wokku::Config.api_url)

  cable = Wokku::CableClient.new(url: cable_url, token: token)
  cable.subscribe(channel: "TerminalChannel", params: { server_id: server_id })

  tty = force_tty.nil? ? $stdout.tty? : force_tty
  session = Wokku::PtySession.new(cable: cable, tty: tty)
  session.start!(mode: mode, target: target, argv: argv)
  session.run
  cable.close
  exit(session.exit_code || 0)
end