Class: Above::CLI

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

Overview

CLI handled by optparse

Instance Method Summary collapse

Instance Method Details

#certs(opts) ⇒ Object



36
37
38
39
40
# File 'lib/above/cli.rb', line 36

def certs(opts)
  opts.on("-c", "--cert DOMAINS", "Make a certificate for DOMAINS (comma seperated list)") do |opt|
    @options[:domain] = opt
  end
end

#create_certObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/above/cli.rb', line 42

def create_cert
  if @options.key?(:domain)
    if !@options.key?(:dir)
      puts "Directory argument missing"
      exit
    end

    domain_arr = @options[:domain].split(",")
    dir = @options[:dir]
    cert = Above::Certs.new
    cert.create(domain_arr:, dir:)
    cert.dane(domain_arr:, dir:)
  end
end

#dir(opts) ⇒ Object



57
58
59
60
61
# File 'lib/above/cli.rb', line 57

def dir(opts)
  opts.on("-d", "--dir DIRECTORY", "Make a certificate in, or look for config in DIRECTORY") do |opt|
    @options[:dir] = opt
  end
end

#help(opts) ⇒ Object



63
64
65
66
67
68
# File 'lib/above/cli.rb', line 63

def help(opts)
  opts.on("-h", "--help", "This help message") do
    puts opts
    exit
  end
end

#server(opts) ⇒ Object



70
71
72
73
74
# File 'lib/above/cli.rb', line 70

def server(opts)
  opts.on("-s", "--start", "Start the server, if a config directory isn't found one will be created") do |opt|
    @options[:server] = opt
  end
end

#start(*_args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/above/cli.rb', line 12

def start(*_args)
  @options = {}
  OptionParser.new do |opts|
    opts.banner = <<~BANNER
      Above
      A Gemini Protocol server

      Commands:
    BANNER
    certs(opts)
    dir(opts)
    server(opts)
    help(opts)

    version(opts)
  end.parse!

  create_cert
  start_server
rescue OptionParser::InvalidOption
  puts "Invalid option"
  # Don't have opts here to show help
end

#start_serverObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/above/cli.rb', line 76

def start_server
  return unless @options[:server]
  dir = @options[:dir]
  Above::Config.init_dir unless dir
  domains = Above::Config.read_config(dir:)
  domains.each_pair do |server_name, server_hash|
    puts "Starting #{server_name}"
    server = Above::Server.new(config: server_hash)
    server.start
  end
end

#version(opts) ⇒ Object



88
89
90
91
92
93
# File 'lib/above/cli.rb', line 88

def version(opts)
  opts.on("-v", "--version", "The version") do
    puts Above::VERSION
    exit
  end
end