Class: Rbkb::Cli::PlugCli

Inherits:
Executable show all
Defined in:
lib/rbkb/plug/cli.rb

Overview

Rbkb::Cli::Executable is an abstract class for creating command line executables using the Ruby Black Bag framework.

Direct Known Subclasses

Telson

Constant Summary collapse

RX_HOST_AND_PORT =
/^([\w._-]+):(\d+)$/
RX_PORT_OPT_ADDR =
/^(?:([\w._-]+):)?(\d+)$/

Instance Attribute Summary collapse

Attributes inherited from Executable

#argv, #exit_status, #oparse, #opts, #stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from Executable

#bail, #bail_args, #exit, #go, #parse, run

Constructor Details

#initialize(*args) ⇒ PlugCli

Returns a new instance of PlugCli.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rbkb/plug/cli.rb', line 19

def initialize(*args)
  super(*args) do |this|
    this.blit_addr ||= Plug::Blit::DEFAULT_IPADDR
    this.blit_port ||= Plug::Blit::DEFAULT_PORT
    this.transport ||= :TCP
    this.plug_opts ||= {}
    yield this if block_given?
  end

  # TODO: Plug::UI obviously need fixing.
  # TODO It shouldn't be driven by constants for configuration
  Plug::UI::LOGCFG[:verbose] = true
  Plug::UI::LOGCFG[:dump] = :hex
  Plug::UI::LOGCFG[:out] = @stderr
end

Instance Attribute Details

#blit_addr ⇒ Object

Returns the value of attribute blit_addr.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def blit_addr
  @blit_addr
end

#blit_port ⇒ Object

Returns the value of attribute blit_port.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def blit_port
  @blit_port
end

#blit_proto ⇒ Object

Returns the value of attribute blit_proto.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def blit_proto
  @blit_proto
end

#local_addr ⇒ Object

Returns the value of attribute local_addr.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def local_addr
  @local_addr
end

#local_port ⇒ Object

Returns the value of attribute local_port.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def local_port
  @local_port
end

#plug_opts ⇒ Object

Returns the value of attribute plug_opts.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def plug_opts
  @plug_opts
end

#target_addr ⇒ Object

Returns the value of attribute target_addr.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def target_addr
  @target_addr
end

#target_port ⇒ Object

Returns the value of attribute target_port.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def target_port
  @target_port
end

#transport ⇒ Object

Returns the value of attribute transport.



15
16
17
# File 'lib/rbkb/plug/cli.rb', line 15

def transport
  @transport
end

Instance Method Details

#make_parser ⇒ Object



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
# File 'lib/rbkb/plug/cli.rb', line 35

def make_parser
  arg = super()
  arg.banner << ' host:port'

  arg.on('-o', '--output=FILE', 'Output to file') do |o|
    Plug::UI::LOGCFG[:out] = File.open(o, 'w') # XXX
  end

  arg.on('-q', '--quiet', 'Turn off verbose logging') do
    Plug::UI::LOGCFG[:verbose] = false # XXX
  end

  arg.on('-d', '--dump-format=hex/raw',
         'Output conversations in hexdump or raw') do |d|
    if m = /^(hex|raw)$/i.match(d)
      Plug::UI::LOGCFG[:dump] = m[1].downcase.to_sym # XXX
    else
      bail "Invalid dump format: #{d.inspect}"
    end
  end

  arg.on('-b', '--blit=ADDR:PORT', 'Where to listen for blit') do |b|
    unless m = RX_PORT_OPT_ADDR.match(b)
      bail('Invalid blit address/port')
    end
    @blit_port = m[2].to_i
    @blit_addr = m[1] if m[1]
  end

  arg.on('-u', '--udp', 'UDP mode') { @transport = :UDP }

  arg.on('-S', '--start-tls', 'Initiate TLS') { |_s| @plug_opts[:tls] = true }

  arg
end

#parse_target_argument ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/rbkb/plug/cli.rb', line 71

def parse_target_argument
  unless (m = RX_HOST_AND_PORT.match(tgt = @argv.shift))
    bail "Invalid target: #{tgt}\n  Hint: use -h"
  end
  @target_addr = m[1]
  @target_port = m[2].to_i
  m
end