Module: Canals::Cli::Helpers

Included in:
Application, Session
Defined in:
lib/canals/cli/helpers.rb

Instance Method Summary collapse

Instance Method Details

#check_completionObject



55
56
57
58
59
60
61
62
# File 'lib/canals/cli/helpers.rb', line 55

def check_completion
  if Canals.config[:completion_version]
    if Canals.config[:completion_version] != Canals::VERSION
      Canals::Tools::Completion.update_completion
      say "Bash completion script upgraded, use `source #{Canals::Tools::Completion.cmp_file}` to reload it", :red
    end
  end
end

#checkmark(bool) ⇒ Object

transform boolean into ✓ / ✗



65
66
67
# File 'lib/canals/cli/helpers.rb', line 65

def checkmark(bool)
  bool ? "\u2713".encode('utf-8') : "\u2717".encode('utf-8')
end

#startup_checksObject



51
52
53
# File 'lib/canals/cli/helpers.rb', line 51

def startup_checks
  check_completion
end

#trestart(tunnel_opts) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/canals/cli/helpers.rb', line 33

def trestart(tunnel_opts)
  if tunnel_opts.instance_of? String
    tunnel_opts = tunnel_options(tunnel_opts)
  end
  tstop(tunnel_opts)
  tstart(tunnel_opts)
end

#tstart(tunnel_opts, silent: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/canals/cli/helpers.rb', line 17

def tstart(tunnel_opts, silent: false)
  if tunnel_opts.instance_of? String
    tunnel_opts = tunnel_options(tunnel_opts)
  end
  pid = Canals.start(tunnel_opts)
  say "Created tunnel #{tunnel_opts.name.inspect} with pid #{pid}. You can access it using '#{tunnel_opts.bind_address}:#{tunnel_opts.local_port}'" unless silent
  pid
rescue Canals::Exception => e
  if tunnel_opts.instance_of? String
    tunnel_opts = tunnel_options(tunnel_opts)
  end
  isalive = Canals.isalive? tunnel_opts
  say "Unable to create tunnel #{tunnel_opts.name.inspect}#{isalive ? ', A tunnel for ' + tunnel_opts.name.inspect + ' Already exists.' : ''}", :red
  0
end

#tstop(tunnel_opts, remove_from_session: true, silent: false) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/canals/cli/helpers.rb', line 9

def tstop(tunnel_opts, remove_from_session: true, silent: false)
  if tunnel_opts.instance_of? String
    tunnel_opts = tunnel_options(tunnel_opts)
  end
  Canals.stop(tunnel_opts, remove_from_session: remove_from_session)
  say "Tunnel #{tunnel_opts.name.inspect} stopped." unless silent
end

#tunnel_options(name) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/canals/cli/helpers.rb', line 41

def tunnel_options(name)
  if (Canals.session.has?(name))
    Canals.session.get_obj(name)
  elsif (Canals.repository.has?(name))
    Canals.repository.get(name)
  else
    raise Thor::Error.new "Unable to find tunnel #{name.inspect}."
  end
end