Class: EchSpec::CLI::Resolve

Inherits:
Object
  • Object
show all
Includes:
EchConfigPrinter
Defined in:
lib/echspec/cli/resolve.rb

Instance Method Summary collapse

Methods included from EchConfigPrinter

#evaluate_widths, #field_pairs, #print_ech_configs, #print_field

Instance Method Details

#execute(argv) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/echspec/cli/resolve.rb', line 5

def execute(argv)
  fpath, well_known, hostname = parse_options(argv)

  result = if !fpath.nil?
             Spec::Spec9.parse_pem(File.read(fpath))
           elsif well_known
             Spec::Spec9.well_known_origin_svcb(hostname)
           else
             Spec::Spec9.resolve_ech_configs(hostname)
           end

  case result
  in Ok(ech_configs)
    print_ech_configs(ech_configs)
  in Err(details, _)
    warn "** #{details}"
    exit 1
  end
end

#parse_options(argv) ⇒ Object

rubocop: disable Metrics/MethodLength



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/echspec/cli/resolve.rb', line 26

def parse_options(argv)
  op = OptionParser.new
  fpath = nil
  well_known = false

  op.on(
    '-f',
    '--file FILE',
    'path to ECHConfigs PEM file       (default resolve ECHConfigs via DNS)'
  ) do |v|
    fpath = v
  end

  op.on(
    '-w',
    '--well-known',
    'resolve ECHConfigs via well-known URI, instead of DNS'
  ) do
    well_known = true
  end

  op.banner = <<~USAGE
    Usage: echspec resolve [OPTIONS...] [{HOSTNAME}]

    Resolve ECHConfigs for a hostname and print fields.
    {HOSTNAME} is required unless -f is specified.

    Examples:

      $ echspec resolve localhost
      $ echspec resolve -w localhost
      $ echspec resolve -f echconfigs.pem

    Options:
  USAGE

  begin
    args = op.parse(argv)
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
    warn op
    warn "** #{e.message}"
    exit 1
  end

  if !fpath.nil? && !File.exist?(fpath)
    warn '** {FILE} is not found'
    exit 1
  end

  if fpath.nil? && args.length != 1
    warn op
    warn '** {HOSTNAME} argument is not specified'
    exit 1
  end

  [fpath, well_known, args[0]]
end