mentorenwahl/docker/backend/src/cli/backend.cr
Dominic Grimm 1da3141e82
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Updated CLI
2022-02-09 17:10:16 +01:00

84 lines
1.6 KiB
Crystal

require "commander"
require "../backend"
cli = Commander::Command.new do |cmd|
cmd.use = "backend"
cmd.short = "Mentorenwahl backend CLI"
cmd.run do
puts cmd.help
end
cmd.commands.add do |c|
c.use = "version"
c.short = "Prints version"
c.long = c.short
c.run do
puts Backend::VERSION
end
end
cmd.commands.add do |c|
c.use = "authors"
c.short = "Prints authors"
c.long = c.short
c.run do
puts Backend::AUTHORS.join("\n")
end
end
cmd.commands.add do |c|
c.use = "license"
c.short = "Prints license"
c.long = c.short
c.run do
puts Backend::LICENSE
end
end
cmd.commands.add do |c|
c.use = "run"
c.short = "Run the backend"
c.long = c.short
c.run do
Backend.run
end
end
cmd.commands.add do |c|
c.use = "register <username> <role>"
c.short = "Seeds the database with required data"
c.long = c.short
c.flags.add do |f|
f.name = "admin"
f.long = "--admin"
f.default = false
f.description = "Register as admin"
end
c.run do |opts, args|
username = args[0]
role = Backend::Db::UserRole.parse(args[1].downcase)
print "Register '#{username}' as '#{role}'#{opts.bool["admin"] ? " with admin privileges" : nil}? [y/N] "
abort unless (gets(chomp: true) || "").strip.downcase == "y"
user = Backend::Db::User.create!(username: username, role: role.to_s, admin: opts.bool["admin"])
puts "Done!"
puts "---"
puts "User: #{user.id}"
puts "Role: #{user.role}"
puts "Admin: #{user.admin}"
puts "---"
end
end
end
Commander.run(cli, ARGV)