dhcp: wrap main routine in class
This commit is contained in:
parent
6ce5a542f2
commit
b540709a35
53
scripts/dhcp
53
scripts/dhcp
@ -37,12 +37,35 @@ GLOBAL_OPTIONS = OptionParser.new do |opts|
|
||||
Available subcommands:
|
||||
add [options] NAME MACADDRESS: add dhcp lease
|
||||
remove [options] NAME: remove dhcp static lease
|
||||
regenerate: regenerate dhcp configuration
|
||||
|
||||
See 'dhcp COMMAND --help' for more information on a specific command.
|
||||
HELP
|
||||
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
|
||||
parser = OptionParser.new do |opts|
|
||||
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|
|
||||
ipv6 = address
|
||||
end
|
||||
end.order!
|
||||
end.order!(args)
|
||||
if ARGV.size < 2
|
||||
$stderr.puts "no enough arguments"
|
||||
die(parser.help)
|
||||
end
|
||||
name, macaddress = args
|
||||
registry.add_lease(name, macaddress, ipv4, ipv6)
|
||||
end
|
||||
@registry.add_lease(name, macaddress, ipv4, ipv6)
|
||||
end
|
||||
|
||||
def remove_command(registry, args)
|
||||
def remove_command(args)
|
||||
parser = OptionParser.new do |opts|
|
||||
opts.banner = "Usage: dhcp remove NAME"
|
||||
end.order!
|
||||
end.order!(args)
|
||||
if args.empty?
|
||||
$stderr.puts "no enough arguments"
|
||||
die(parser.help)
|
||||
end
|
||||
registry.remove_lease(args.first)
|
||||
@registry.remove_lease(args.first)
|
||||
end
|
||||
end
|
||||
|
||||
GLOBAL_OPTIONS.order!
|
||||
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
|
||||
Application.new.run(ARGV)
|
||||
|
Loading…
Reference in New Issue
Block a user