Module: TunnelRb::CLI

Defined in:
lib/tunnel_rb/cli.rb

Class Method Summary collapse

Class Method Details

.env_bool(name, default) ⇒ Object



15
16
17
18
19
20
# File 'lib/tunnel_rb/cli.rb', line 15

def env_bool(name, default)
  value = ENV[name]
  return default if value.nil? || value.empty?

  ["1", "true", "yes"].include?(value.downcase)
end

.parse_options(argv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tunnel_rb/cli.rb', line 22

def parse_options(argv)
  options = {
    server_host: ENV.fetch("SERVER_HOST", "server.tunnel-rb.dev"),
    server_port: Integer(ENV.fetch("SERVER_PORT", "7777")),
    local_host: ENV.fetch("LOCAL_HOST", "localhost"),
    local_port: ENV["LOCAL_PORT"] ? Integer(ENV["LOCAL_PORT"]) : nil,
    tls: env_bool("RELAY_TLS", true),
    tls_ca: ENV["RELAY_TLS_CA"],
    tls_verify: env_bool("RELAY_TLS_VERIFY", true)
  }

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: tunnel [LOCAL_PORT] [options]"
    opts.on("--server-host HOST", "Server host (default: #{options[:server_host]})") { |v| options[:server_host] = v }
    opts.on("--server-port PORT", Integer, "Server port (default: #{options[:server_port]})") { |v| options[:server_port] = v }
    opts.on("--local-host HOST", "Local host (default: #{options[:local_host]})") { |v| options[:local_host] = v }
    opts.on("--[no-]tls", "Connect to the server over TLS (default: on; use --no-tls for local/testing)") { |v| options[:tls] = v }
    opts.on("--[no-]tls-verify", "Verify the server cert against the system CA store (default: on)") { |v| options[:tls_verify] = v }
    opts.on("--tls-ca PATH", "CA cert/bundle to verify the server instead of the system CA store") { |v| options[:tls_ca] = v }
    opts.on("-h", "--help", "Show this help") do
      puts opts
      exit
    end
  end
  parser.parse!(argv)

  options[:local_port] = Integer(argv[0]) if argv[0]

  if options[:local_port].nil?
    warn "Error: local port is required (positional argument or LOCAL_PORT env var)"
    warn parser.help
    exit 1
  end

  options
end

.run(argv = ARGV) ⇒ Object



11
12
13
# File 'lib/tunnel_rb/cli.rb', line 11

def run(argv = ARGV)
  Client.new(**parse_options(argv)).start
end