mentorenwahl/backend/src/backend/cli.cr

105 lines
2.3 KiB
Crystal

require "commander"
module 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 VERSION
end
end
cmd.commands.add do |c|
c.use = "authors"
c.short = "Prints authors"
c.long = c.short
c.run do
puts AUTHORS.join("\n")
end
end
cmd.commands.add do |c|
c.use = "licenses"
c.short = "Prints licenses of projects used by this programs"
c.long = c.short
c.run do
puts LICENSES
end
end
cmd.commands.add do |c|
c.use = "run"
c.short = "Run the backend"
c.long = c.short
c.run do
Runner.new.run
end
end
cmd.commands.add do |c|
c.use = "schema"
c.short = "Prints out GraphQL schema"
c.long = c.short
c.run do
puts Api::Schema::SCHEMA.document.to_s
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.flags.add do |f|
f.name = "yes"
f.short = "-y"
f.long = "--yes"
f.default = false
f.description = "Answer yes to all questions"
end
c.run do |opts, args|
username = args[0]
role = Db::UserRole.from_string(args[1].underscore)
unless opts.bool["yes"]
print "Register '#{username}' as '#{role.to_api}'#{opts.bool["admin"] ? " with admin privileges" : nil}? [y/N] "
abort unless gets(chomp: true).not_nil!.strip.downcase == "y"
end
user = Db::User.create!(username: username, role: role.to_s, admin: opts.bool["admin"])
case role.to_api
in Api::Schema::UserRole::Student
Db::Student.create!(user_id: user.id)
in Api::Schema::UserRole::Teacher
# Db::Teacher.create!(user_id: user.id)
end
Worker::Jobs::CacheLdapUserJob.new(user.id).enqueue
puts "Done!"
end
end
end
end