Class: EchSpec::CLI

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

Instance Method Summary collapse

Instance Method Details

#parse_options(argv = ARGV) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
83
# File 'lib/echspec/cli.rb', line 5

def parse_options(argv = ARGV)
  op = OptionParser.new

  # default value
  fpath = nil
  port = 443
  force_compliant = true
  verbose = false
  sections = nil

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

  op.on(
    '-p',
    '--port VALUE',
    "server port number                (default #{port})"
  ) do |v|
    port = v
  end

  op.on(
    '-n',
    '--not-force-compliant-hpke',
    'not force compliant ECHConfig HPKE cipher suite'
  ) do
    force_compliant = false
  end

  op.on(
    '-v',
    '--verbose',
    'verbose mode; prints message stack if raised an error'
  ) do
    verbose = true
  end

  op.on(
    '-s',
    '--sections SECTIONS',
    'sections to test; by the default, test all sections'
  ) do |v|
    sections = v.split(',')
  end

  op.banner = 'Usage: echspec [OPTIONS] <HOSTNAME>'
  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

  unknowns = sections.nil? ? [] : sections - Spec.sections
  unless unknowns.empty?
    warn "** #{unknowns} are unknown sections"
    exit 1
  end

  if args.length != 1
    warn op
    warn '** <HOSTNAME> argument is not specified'
    exit 1
  end
  hostname = args[0]

  [fpath, port, force_compliant, verbose, hostname, sections]
end

#runObject

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength



87
88
89
90
91
92
93
94
95
# File 'lib/echspec/cli.rb', line 87

def run
  fpath, port, force_compliant, verbose, hostname, sections = parse_options

  if sections.nil?
    Spec.run(fpath, port, hostname, force_compliant, verbose)
  else
    Spec.run_only(fpath, port, hostname, sections, verbose)
  end
end