2020-12-10 17:29:26 +01:00
|
|
|
require "option_parser"
|
|
|
|
|
|
|
|
|
|
|
|
class OptionParser
|
|
|
|
def to_s(io : IO)
|
|
|
|
if banner = @banner
|
|
|
|
io << banner
|
|
|
|
io << "\n\n"
|
|
|
|
end
|
|
|
|
@flags.join io, "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def parsing_cli(authd_config : Baguette::Configuration::Auth)
|
|
|
|
|
|
|
|
# frequently used functions
|
|
|
|
opt_authd_login = -> (parser : OptionParser, authd_config : Baguette::Configuration::Auth) {
|
|
|
|
parser.on "-l LOGIN", "--login LOGIN", "Authd user login." do |login|
|
|
|
|
authd_config.login = login
|
|
|
|
Baguette::Log.info "User login for authd: #{authd_config.login}."
|
|
|
|
end
|
|
|
|
parser.on "-p PASSWORD", "--password PASSWORD", "Authd user password." do |password|
|
|
|
|
authd_config.pass = password
|
|
|
|
Baguette::Log.info "User password for authd: #{authd_config.pass}."
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
opt_help = -> (parser : OptionParser) {
|
|
|
|
parser.on "-h", "--help", "Prints command usage." do
|
|
|
|
puts "usage: #{PROGRAM_NAME} command -h"
|
|
|
|
puts
|
|
|
|
puts parser
|
|
|
|
|
|
|
|
case Context.command
|
2023-05-06 23:18:59 +02:00
|
|
|
when /admin-maintenance/
|
2020-12-10 17:29:26 +01:00
|
|
|
Baguette::Log.warning "should provide subjects to request"
|
|
|
|
Baguette::Log.warning "as in:"
|
2023-05-06 23:18:59 +02:00
|
|
|
DNSManager::Request::Maintenance::Subject.names.each do |n|
|
2020-12-10 17:29:26 +01:00
|
|
|
Baguette::Log.warning "- #{n}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# Unrecognized parameters are used to create commands with multiple arguments.
|
|
|
|
# Example: user add _login email phone_
|
|
|
|
# Here, login, email and phone are unrecognized arguments.
|
|
|
|
# Still, the "user add" command expect them.
|
|
|
|
unrecognized_args_to_context_args = -> (parser : OptionParser,
|
|
|
|
nexact : Int32?,
|
|
|
|
at_least : Int32?) {
|
|
|
|
|
|
|
|
# With the right args, these will be interpreted as serialized data.
|
|
|
|
parser.unknown_args do |args|
|
|
|
|
|
|
|
|
# either we test with the exact expected number of arguments or the least.
|
|
|
|
if exact = nexact
|
|
|
|
if args.size != exact
|
|
|
|
Baguette::Log.error "#{parser}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
elsif least = at_least
|
|
|
|
if args.size < least
|
|
|
|
Baguette::Log.error "#{parser}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Baguette::Log.error "Number of parameters not even provided!"
|
|
|
|
Baguette::Log.error "#{parser}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
args.each do |arg|
|
|
|
|
Baguette::Log.debug "Unrecognized argument: #{arg} (adding to Context.args)"
|
|
|
|
if Context.args.nil?
|
|
|
|
Context.args = Array(String).new
|
|
|
|
end
|
|
|
|
Context.args.not_nil! << arg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
parser = OptionParser.new do |parser|
|
|
|
|
parser.banner = "Welcome on the DNSManager CLI administration."
|
|
|
|
|
|
|
|
# Admin section.
|
|
|
|
parser.on "admin", "Admin operations." do
|
2023-05-07 04:09:44 +02:00
|
|
|
parser.banner = "Admin operations (requires secret via -k)."
|
2020-12-10 17:29:26 +01:00
|
|
|
|
|
|
|
# Maintenance.
|
2023-05-06 23:18:59 +02:00
|
|
|
parser.on("maintenance", "Maintenance operation of the website.") do
|
2023-05-07 04:09:44 +02:00
|
|
|
Baguette::Log.info "maintenance operation of the website."
|
2023-05-06 23:18:59 +02:00
|
|
|
Context.command = "admin-maintenance"
|
|
|
|
parser.banner = "COMMAND: admin maintenance subject [value]"
|
2020-12-10 17:29:26 +01:00
|
|
|
unrecognized_args_to_context_args.call parser, nil, 1
|
|
|
|
end
|
|
|
|
|
2024-02-24 06:54:59 +01:00
|
|
|
# Generate a zone file.
|
|
|
|
parser.on("genzone", "Generate a zone file.") do
|
|
|
|
Baguette::Log.info "generate a zone file on the server."
|
|
|
|
Context.command = "admin-generate-zonefile"
|
|
|
|
parser.banner = "COMMAND: admin genzone domain [domain…]"
|
|
|
|
unrecognized_args_to_context_args.call parser, nil, 1
|
|
|
|
end
|
|
|
|
|
2020-12-10 17:29:26 +01:00
|
|
|
end
|
|
|
|
|
2023-05-07 04:09:44 +02:00
|
|
|
# User section.
|
|
|
|
parser.on "user", "Simple user operations." do
|
|
|
|
parser.banner = "Operations as a simple user."
|
2023-06-27 13:00:26 +02:00
|
|
|
|
|
|
|
# Domain.
|
|
|
|
parser.on("domain", "Domain operations.") do
|
|
|
|
parser.on("add", "Add a new domain.") do
|
|
|
|
Baguette::Log.info "add domain."
|
|
|
|
Context.command = "user-domain-add"
|
|
|
|
parser.banner = "COMMAND: user domain add domain [domain...]"
|
|
|
|
unrecognized_args_to_context_args.call parser, nil, 1
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on("del", "Delete a domain.") do
|
|
|
|
Baguette::Log.info "del domain."
|
|
|
|
Context.command = "user-domain-del"
|
|
|
|
parser.banner = "COMMAND: user domain del domain [domain...]"
|
|
|
|
unrecognized_args_to_context_args.call parser, nil, 1
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on("list", "List all domains.") do
|
|
|
|
Baguette::Log.info "list domains."
|
|
|
|
Context.command = "user-domain-list"
|
|
|
|
parser.banner = "COMMAND: user domain list"
|
|
|
|
unrecognized_args_to_context_args.call parser, 0, nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-07 04:09:44 +02:00
|
|
|
# Zone.
|
|
|
|
parser.on("zone", "Zone operations.") do
|
|
|
|
parser.on("add", "Add new zone.") do
|
|
|
|
Baguette::Log.info "add zone."
|
|
|
|
Context.command = "user-zone-add"
|
|
|
|
parser.banner = "COMMAND: user zone add <file> [<file>...]"
|
|
|
|
unrecognized_args_to_context_args.call parser, nil, 1
|
|
|
|
end
|
|
|
|
|
2023-06-27 13:00:26 +02:00
|
|
|
parser.on("del", "Delete a zone (alias to domain del).") do
|
2023-05-07 04:09:44 +02:00
|
|
|
Baguette::Log.info "del zone."
|
2023-06-27 13:00:26 +02:00
|
|
|
Context.command = "user-domain-del"
|
2023-05-07 04:09:44 +02:00
|
|
|
parser.banner = "COMMAND: user zone del domain [domain...]"
|
|
|
|
unrecognized_args_to_context_args.call parser, nil, 1
|
|
|
|
end
|
2023-05-07 20:50:56 +02:00
|
|
|
|
2023-05-07 21:05:53 +02:00
|
|
|
parser.on("get", "Get a zone.") do
|
|
|
|
Baguette::Log.info "get zone."
|
|
|
|
Context.command = "user-zone-get"
|
|
|
|
parser.banner = "COMMAND: user zone get domain [domain...]"
|
|
|
|
unrecognized_args_to_context_args.call parser, nil, 1
|
|
|
|
end
|
|
|
|
|
2023-06-27 13:00:26 +02:00
|
|
|
parser.on("list", "List all domains (alias to domain list).") do
|
|
|
|
Baguette::Log.info "list domains."
|
2023-05-07 20:50:56 +02:00
|
|
|
Context.command = "user-domain-list"
|
|
|
|
parser.banner = "COMMAND: user zone list"
|
|
|
|
unrecognized_args_to_context_args.call parser, 0, nil
|
|
|
|
end
|
2023-05-07 04:09:44 +02:00
|
|
|
end
|
|
|
|
|
2023-05-08 17:34:50 +02:00
|
|
|
parser.on("rr", "Zone Resource Record operations.") do
|
|
|
|
parser.on("add", "Add new RR.") do
|
|
|
|
Baguette::Log.info "Add RR."
|
|
|
|
parser.banner = "COMMAND: user rr add [A|AAAA|CNAME|MX|SRV|TXT|NS]"
|
|
|
|
parser.on("A", "Add new A RR.") do
|
|
|
|
Baguette::Log.info "add new A RR."
|
|
|
|
Context.command = "user-rr-add-a"
|
|
|
|
parser.banner = "COMMAND: user rr add A <domain> <NAME> <TTL> <TARGET>"
|
|
|
|
unrecognized_args_to_context_args.call parser, 4, nil
|
|
|
|
end
|
|
|
|
end
|
2023-05-08 19:07:20 +02:00
|
|
|
|
|
|
|
parser.on("update", "Update a RR.") do
|
|
|
|
Baguette::Log.info "Update a RR."
|
|
|
|
parser.banner = "COMMAND: user rr update [A|AAAA|CNAME|MX|SRV|TXT|NS|SOA]"
|
|
|
|
parser.on("A", "Update an A RR.") do
|
|
|
|
Baguette::Log.info "update an A RR."
|
|
|
|
Context.command = "user-rr-update-a"
|
|
|
|
parser.banner = "COMMAND: user rr update A <domain> <rrid> <NAME> <TTL> <TARGET>"
|
|
|
|
unrecognized_args_to_context_args.call parser, 5, nil
|
|
|
|
end
|
|
|
|
end
|
2023-05-08 19:23:36 +02:00
|
|
|
|
|
|
|
parser.on("del", "Delete a RR.") do
|
|
|
|
Baguette::Log.info "delete a RR."
|
|
|
|
parser.banner = "COMMAND: user rr del <domain> <rrid>"
|
|
|
|
Context.command = "user-rr-del"
|
|
|
|
unrecognized_args_to_context_args.call parser, 2, nil
|
|
|
|
end
|
2023-05-08 17:34:50 +02:00
|
|
|
end
|
|
|
|
|
2023-05-07 04:09:44 +02:00
|
|
|
end
|
|
|
|
|
2020-12-10 17:29:26 +01:00
|
|
|
opt_help.call parser
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.parse
|
|
|
|
end
|