Class: Portless::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/portless/cli.rb

Overview

Hand-rolled command dispatch (no Thor/optparse ceremony, like portless's cli.ts). rb-portless run <cmd> is the main path; the rest manage the proxy, CA trust, hosts file, and diagnostics.

Constant Summary collapse

COMMANDS =
%w[run proxy trust hosts list doctor clean prune alias get service].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



14
15
16
# File 'lib/portless/cli.rb', line 14

def initialize(argv)
  @argv = argv.dup
end

Class Method Details

.start(argv = ARGV) ⇒ Object



10
11
12
# File 'lib/portless/cli.rb', line 10

def self.start(argv = ARGV)
  new(argv).run
end

Instance Method Details

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/portless/cli.rb', line 18

def run
  first = @argv.first
  return print_version if %w[--version -v].include?(first)
  return print_help if @argv.empty? || %w[--help -h].include?(first)

  if COMMANDS.include?(first)
    return command_help(first) if %w[--help -h].include?(@argv[1])

    send("cmd_#{first}", rest(first))
  elsif first.start_with?("--")
    cmd_run(@argv)        # leading flags (incl. --name) → run mode
  else
    cmd_named(@argv)      # `rb-portless <name> <command…>` shorthand
  end
rescue Portless::NonInteractiveError => e
  warn error_line(e.message)
  exit 2
rescue Portless::Error => e
  warn error_line(e.message)
  exit 1
end