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.



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

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_addrObject

Returns the value of attribute blit_addr.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def blit_addr
  @blit_addr
end

#blit_portObject

Returns the value of attribute blit_port.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def blit_port
  @blit_port
end

#blit_protoObject

Returns the value of attribute blit_proto.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def blit_proto
  @blit_proto
end

#local_addrObject

Returns the value of attribute local_addr.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def local_addr
  @local_addr
end

#local_portObject

Returns the value of attribute local_port.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def local_port
  @local_port
end

#plug_optsObject

Returns the value of attribute plug_opts.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def plug_opts
  @plug_opts
end

#target_addrObject

Returns the value of attribute target_addr.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def target_addr
  @target_addr
end

#target_portObject

Returns the value of attribute target_port.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def target_port
  @target_port
end

#transportObject

Returns the value of attribute transport.



17
18
19
# File 'lib/rbkb/plug/cli.rb', line 17

def transport
  @transport
end

Instance Method Details

#make_parserObject



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

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 }

  return arg
end

#parse_target_argumentObject



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

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
  return m
end