mentorenwahl/docker/backend/src/cli/backend.cr

100 lines
2.4 KiB
Crystal

# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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)