2020-10-11 15:51:26 +02:00
|
|
|
require "option_parser"
|
|
|
|
|
2020-10-11 20:52:04 +02:00
|
|
|
opt_authd_admin = -> (parser : OptionParser) {
|
|
|
|
parser.on "-k file", "--key-file file", "Read the authd shared key from a file." do |file|
|
|
|
|
Context.shared_key = File.read(file).chomp
|
|
|
|
Baguette::Log.info "Key for admin operations: #{Context.shared_key}."
|
2020-10-11 15:51:26 +02:00
|
|
|
end
|
2020-10-11 20:52:04 +02:00
|
|
|
}
|
2020-10-11 15:51:26 +02:00
|
|
|
|
2020-10-13 02:35:33 +02:00
|
|
|
# frequently used functions
|
|
|
|
opt_authd_login = -> (parser : OptionParser) {
|
|
|
|
parser.on "-l LOGIN", "--login LOGIN", "Authd user login." do |login|
|
|
|
|
Context.authd_login = login
|
|
|
|
Baguette::Log.info "User login for authd: #{Context.authd_login}."
|
|
|
|
end
|
|
|
|
parser.on "-p PASSWORD", "--password PASSWORD", "Authd user password." do |password|
|
|
|
|
Context.authd_pass = password
|
|
|
|
Baguette::Log.info "User password for authd: #{Context.authd_pass}."
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
opt_help = -> (parser : OptionParser) {
|
|
|
|
parser.on "help", "Prints this help message." do
|
|
|
|
puts parser
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
opt_profile = -> (parser : OptionParser) {
|
|
|
|
parser.on "-P file", "--profile file", "Read the user profile from a file." do |file|
|
|
|
|
Context.user_profile = JSON.parse(File.read file).as_h
|
|
|
|
Baguette::Log.info "Reading the user profile: #{Context.user_profile}."
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
opt_phone = -> (parser : OptionParser) {
|
|
|
|
parser.on "-n phone", "--phone-number num", "Phone number." do |phone|
|
|
|
|
Context.phone = phone
|
|
|
|
Baguette::Log.info "Reading the user phone number: #{Context.phone}."
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
opt_email = -> (parser : OptionParser) {
|
|
|
|
parser.on "-e email", "--email address", "Email address." do |email|
|
|
|
|
Context.email = email
|
|
|
|
Baguette::Log.info "Reading the user email address: #{Context.email}."
|
|
|
|
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, n_expected_args : Int32) {
|
|
|
|
# With the right args, these will be interpreted as serialized data.
|
|
|
|
parser.unknown_args do |args|
|
|
|
|
if args.size != n_expected_args
|
|
|
|
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
|
|
|
|
}
|
2020-10-11 15:51:26 +02:00
|
|
|
|
2020-10-11 20:52:04 +02:00
|
|
|
parser = OptionParser.new do |parser|
|
|
|
|
parser.banner = "usage: #{PROGRAM_NAME} command help"
|
|
|
|
parser.on "-v verbosity", "--verbosity v", "Verbosity. From 0 to 4 (debug)." do |v|
|
|
|
|
Baguette::Context.verbosity = v.to_i
|
|
|
|
Baguette::Log.info "verbosity = #{v}"
|
2020-10-11 15:51:26 +02:00
|
|
|
end
|
2020-10-11 20:52:04 +02:00
|
|
|
parser.on "-h", "--help", "Prints this help message." do
|
|
|
|
puts "usage: #{PROGRAM_NAME} command help"
|
|
|
|
puts parser
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
2020-10-13 02:35:33 +02:00
|
|
|
parser.on "user", "Operations on users." do
|
2020-10-11 20:52:04 +02:00
|
|
|
parser.banner = "Usage: user [add | mod | delete]"
|
2020-10-13 02:35:33 +02:00
|
|
|
|
|
|
|
parser.on "add", "Adding a user to the DB." do
|
|
|
|
parser.banner = "usage: user add login email phone [-P profile] [opt]"
|
|
|
|
Baguette::Log.info "Adding a user to the DB."
|
2020-10-11 20:52:04 +02:00
|
|
|
Context.command = "user-add"
|
|
|
|
opt_authd_admin.call parser
|
2020-10-13 02:35:33 +02:00
|
|
|
opt_profile.call parser
|
|
|
|
opt_email.call parser
|
|
|
|
opt_phone.call parser
|
|
|
|
opt_help.call parser
|
|
|
|
# login email phone
|
|
|
|
unrecognized_args_to_context_args.call parser, 3
|
2020-10-11 20:52:04 +02:00
|
|
|
end
|
|
|
|
|
2020-10-13 02:35:33 +02:00
|
|
|
parser.on "mod", "Modify a user account." do
|
|
|
|
parser.banner = "Usage: user mod userid [-e email|-n phone|-P profile] [opt]"
|
|
|
|
Baguette::Log.info "Modify a user account."
|
2020-10-11 20:52:04 +02:00
|
|
|
Context.command = "user-mod"
|
|
|
|
opt_authd_admin.call parser
|
2020-10-13 02:35:33 +02:00
|
|
|
opt_email.call parser
|
|
|
|
opt_phone.call parser
|
|
|
|
opt_profile.call parser
|
|
|
|
opt_help.call parser
|
|
|
|
# userid
|
|
|
|
unrecognized_args_to_context_args.call parser, 1
|
2020-10-11 20:52:04 +02:00
|
|
|
end
|
2020-10-11 15:51:26 +02:00
|
|
|
|
2020-10-11 20:52:04 +02:00
|
|
|
parser.on "delete", "Remove user." do
|
2020-10-13 05:33:49 +02:00
|
|
|
parser.banner = "Usage: user delete userid [opt]"
|
2020-10-11 20:52:04 +02:00
|
|
|
Baguette::Log.info "Remove user."
|
2020-10-13 06:21:58 +02:00
|
|
|
Context.command = "user-delete"
|
2020-10-13 02:35:33 +02:00
|
|
|
# You can either be the owner of the account, or an admin.
|
|
|
|
opt_authd_login.call parser
|
2020-10-11 20:52:04 +02:00
|
|
|
opt_authd_admin.call parser
|
2020-10-13 02:35:33 +02:00
|
|
|
opt_help.call parser
|
2020-10-13 05:33:49 +02:00
|
|
|
# userid
|
|
|
|
unrecognized_args_to_context_args.call parser, 1
|
2020-10-13 02:35:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Do not require to be admin.
|
|
|
|
parser.on "register", "Register a user (requires activation)." do
|
|
|
|
parser.banner = "Usage: user register login email phone [-P profile] [opt]"
|
|
|
|
Baguette::Log.info "Register a user (requires activation)."
|
|
|
|
Context.command = "user-registration"
|
|
|
|
# These options shouldn't be used here,
|
|
|
|
# email and phone parameters are mandatory.
|
|
|
|
# opt_email.call parser
|
|
|
|
# opt_phone.call parser
|
|
|
|
opt_profile.call parser
|
|
|
|
opt_help.call parser
|
|
|
|
# login email phone
|
|
|
|
unrecognized_args_to_context_args.call parser, 3
|
2020-10-11 20:52:04 +02:00
|
|
|
end
|
2020-10-11 15:51:26 +02:00
|
|
|
end
|
|
|
|
|
2020-10-13 02:35:33 +02:00
|
|
|
parser.on "permission", "Permissions management." do
|
2020-10-11 20:52:04 +02:00
|
|
|
parser.banner = "Usage: permissions [check | set]"
|
|
|
|
parser.on "set", "Set permissions." do
|
2020-10-13 02:35:33 +02:00
|
|
|
parser.banner = <<-END
|
|
|
|
usage: permission set user application resource permission
|
|
|
|
example: permission set 1002 my-application chat read
|
|
|
|
|
|
|
|
permission list: none read edit admin
|
|
|
|
END
|
2020-10-11 20:52:04 +02:00
|
|
|
Baguette::Log.info "Set permissions."
|
2020-10-13 02:35:33 +02:00
|
|
|
Context.command = "permission-set"
|
2020-10-11 20:52:04 +02:00
|
|
|
opt_authd_admin.call parser
|
2020-10-13 02:35:33 +02:00
|
|
|
opt_help.call parser
|
|
|
|
# userid application resource permission
|
|
|
|
unrecognized_args_to_context_args.call parser, 4
|
2020-10-11 20:52:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "check", "Check permissions." do
|
2020-10-13 02:35:33 +02:00
|
|
|
parser.banner = <<-END
|
|
|
|
usage: permission check user application resource
|
|
|
|
example: permission check 1002 my-application chat
|
|
|
|
|
|
|
|
permission list: none read edit admin
|
|
|
|
END
|
2020-10-11 20:52:04 +02:00
|
|
|
Baguette::Log.info "Check permissions."
|
2020-10-13 02:35:33 +02:00
|
|
|
Context.command = "permission-check"
|
2020-10-11 20:52:04 +02:00
|
|
|
opt_authd_admin.call parser
|
2020-10-13 02:35:33 +02:00
|
|
|
opt_help.call parser
|
|
|
|
# userid application resource
|
|
|
|
unrecognized_args_to_context_args.call parser, 3
|
2020-10-11 20:52:04 +02:00
|
|
|
end
|
2020-10-11 15:51:26 +02:00
|
|
|
end
|
|
|
|
|
2020-10-11 20:52:04 +02:00
|
|
|
parser.unknown_args do |args|
|
|
|
|
if args.size > 0
|
|
|
|
Baguette::Log.warning "Unknown args: #{args}"
|
|
|
|
end
|
2020-10-11 15:51:26 +02:00
|
|
|
end
|
2020-10-11 20:52:04 +02:00
|
|
|
|
|
|
|
# parser.on "-X user-password", "--user-password pass", "Read the new user password." do |pass|
|
|
|
|
# password = pass
|
|
|
|
# end
|
2020-10-13 02:35:33 +02:00
|
|
|
|
2020-10-11 15:51:26 +02:00
|
|
|
end
|
|
|
|
|
2020-10-11 20:52:04 +02:00
|
|
|
|
|
|
|
parser.parse
|