Class: TBM::ConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/TBM/config_parser.rb

Constant Summary collapse

CONFIG_FILE =

The configuration file before expansion

'~/.tbm'
EXPANDED_CONFIG_FILE =

The configuration file used for parsing config.

File.expand_path( CONFIG_FILE )
GATEWAY_PATTERN =

Pattern for Gateway Server with Optional Username

/^([^@]+)(@([^@]+))?$/
HOSTPORT_PATTERN =

Pattern for a tunnel with a remote host followed by a port (example.com:3333)

/^([a-zA-Z0-9\.\-]+):(\d{1,5})$/
PORTHOSTPORT_PATTERN =

Pattern for a tunnel with a remote host and local and remote ports (1234:example.com:4321)

/^(\d{1,5}):([a-zA-Z0-9\.\-]+):(\d{1,5})$/
PORTPORT_PATTERN =

Pattern for a tunnel with a local port and a gateway port (1234:4321)

/^(\d{1,5}):(\d{1,5})$/
PORT_PATTERN =

Pattern for a tunnel with a single port to be used client/server to forward to the gateway (1234)

/^\d{1,5}$/
TARGET_NAME_PATTERN =

Pattern for a target with aliases defined in the target name

/^
  ([A-Za-z0-9\.\-\[\]\#]+)                 # name
  (\s*\(\s*                                # optional parenthesized aliases
  ([A-Za-z0-9\.\-\[\]\#]+\s*               # first alias
  (,\s*[A-Za-z0-9\.\-\[\]\#]+\s*)*)            # repeating alias pattern with commas
\))?\s*$/x

Class Method Summary collapse

Class Method Details

.parseConfig

Parses the tunnel boring machine configuration to get a list of targets which can be invoked to bore tunnels.

Returns:

  • (Config)

    the parsed configuration for TBM



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/TBM/config_parser.rb', line 41

def self.parse
  config = Config.new
  if File.file? EXPANDED_CONFIG_FILE
    config_data = YAML.load_file( EXPANDED_CONFIG_FILE )
    case config_data
    when Hash
      parse_gateways( config_data, config ) if config_data.is_a? Hash
    else
      config.errors << "Cannot parse TBM configuration of type: #{config_data.class}"
    end
  else
    config.errors << "No configuration file found. Specify your tunnels in YAML form in: #{CONFIG_FILE}"
  end
  return config
rescue Psych::SyntaxError => pse
  config.errors << "TBM config is invalid YAML: #{pse}"
  return config
end