dhcp: wrap main routine in class

This commit is contained in:
Jörg Thalheim 2015-01-15 08:38:30 +01:00
parent 6ce5a542f2
commit b540709a35
1 changed files with 52 additions and 41 deletions

View File

@ -37,12 +37,35 @@ GLOBAL_OPTIONS = OptionParser.new do |opts|
Available subcommands: Available subcommands:
add [options] NAME MACADDRESS: add dhcp lease add [options] NAME MACADDRESS: add dhcp lease
remove [options] NAME: remove dhcp static lease remove [options] NAME: remove dhcp static lease
regenerate: regenerate dhcp configuration
See 'dhcp COMMAND --help' for more information on a specific command. See 'dhcp COMMAND --help' for more information on a specific command.
HELP HELP
end end
def add_command(registry, args) class Application
def run(args)
GLOBAL_OPTIONS.order!(args)
@registry = DhcpRegistry.new
case command = args.shift
when "add"
add_command
when "remove"
remove_command
when nil
puts(GLOBAL_OPTIONS.help())
exit(0)
when "regenerate" # fall through
else
die "unknown subcommand #{command}"
end
registry.save
registry.update_leases
end
private
def add_command(args)
ipv4, ipv6 = nil, nil ipv4, ipv6 = nil, nil
parser = OptionParser.new do |opts| parser = OptionParser.new do |opts|
opts.banner = "Usage: dhcp add [options] NAME MACADDRESS" opts.banner = "Usage: dhcp add [options] NAME MACADDRESS"
@ -52,37 +75,25 @@ def add_command(registry, args)
opts.on("-6", "--ipv6 ADDRESS", "set fixed ipv6 address") do |address| opts.on("-6", "--ipv6 ADDRESS", "set fixed ipv6 address") do |address|
ipv6 = address ipv6 = address
end end
end.order! end.order!(args)
if ARGV.size < 2 if ARGV.size < 2
$stderr.puts "no enough arguments" $stderr.puts "no enough arguments"
die(parser.help) die(parser.help)
end end
name, macaddress = args name, macaddress = args
registry.add_lease(name, macaddress, ipv4, ipv6) @registry.add_lease(name, macaddress, ipv4, ipv6)
end end
def remove_command(registry, args) def remove_command(args)
parser = OptionParser.new do |opts| parser = OptionParser.new do |opts|
opts.banner = "Usage: dhcp remove NAME" opts.banner = "Usage: dhcp remove NAME"
end.order! end.order!(args)
if args.empty? if args.empty?
$stderr.puts "no enough arguments" $stderr.puts "no enough arguments"
die(parser.help) die(parser.help)
end end
registry.remove_lease(args.first) @registry.remove_lease(args.first)
end
end end
GLOBAL_OPTIONS.order! Application.new.run(ARGV)
registry = DhcpRegistry.new
case command = ARGV.shift
when "add"
add_command(registry, ARGV)
when "remove"
remove_command(registry, ARGV)
when nil # just update
else
die "unknown subcommand #{command}"
end
registry.save
registry.update_leases