39 lines
908 B
Crystal
39 lines
908 B
Crystal
|
|
||
|
class NetworkDCLIParser
|
||
|
def self.pack_args (argv : Array(String))
|
||
|
last_flag = nil : String?
|
||
|
|
||
|
argv.chunks do |x|
|
||
|
if x[0..1] == "--"
|
||
|
last_flag = x
|
||
|
end
|
||
|
|
||
|
last_flag
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def self.parse_rules (argv : Array(String), rules : RuleSet, redirections : RedirectionSet)
|
||
|
args = NetworkDCLIParser.pack_args argv
|
||
|
|
||
|
args.each do |flag, parameters|
|
||
|
# puts "flag: #{flag}, params: #{parameters.join(' ')}"
|
||
|
if flag == "--allow" || flag == "--deny"
|
||
|
parameters[3] # will crash if non-existant
|
||
|
|
||
|
rules << Rule.from_args parameters
|
||
|
elsif flag == "--redirect"
|
||
|
if parameters.size == 3
|
||
|
redirections << Redirection.new parameters[1], parameters[2]
|
||
|
elsif parameters.size == 5
|
||
|
raise "--redirect with 4 parameters not implemented, yet"
|
||
|
else
|
||
|
raise "--redirect <service> <newservice> [<origin-url> <dest-url>]"
|
||
|
end
|
||
|
else
|
||
|
raise "oh no"
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|