mentorenwahl/docker/backend/src/backend/cli.cr
Dominic Grimm 5bc10f8aaf
Some checks failed
continuous-integration/drone/push Build is failing
Added worker
2022-01-23 09:12:57 +01:00

93 lines
2.2 KiB
Crystal

require "commander"
require "fancyline"
require "./db"
module Backend
module CLI
extend self
private FANCY = Fancyline.new
private def input(prompt : String) : String
x = FANCY.readline(prompt)
if x
x.chomp.strip
else
""
end
end
def run : Nil
FANCY.actions.set Fancyline::Key::Control::CtrlC do
exit
end
cli = Commander::Command.new do |cmd|
cmd.use = "backend"
cmd.long = "Mentorenwahl backend CLI"
cmd.run do
puts cmd.help
end
cmd.commands.add do |c|
c.use = "run"
c.long = "Run the backend"
c.run do
Backend.run
end
end
cmd.commands.add do |c|
c.use = "seed"
c.long = "Seeds the database with required data"
c.run do
puts "Seeding database with admin user..."
data = {
"firstname" => input("Firstname: "),
"lastname" => input("Lastname: "),
"email" => input("Email: "),
"password" => API::Auth.hash_password(input("Password: ")),
"role" => Db::UserRole::Admin.to_s,
}
password_confirmation = input("Password confirmation: ")
if data.values.any?(&.empty?)
abort "Values can't be empty!"
elsif !API::Auth.verify_password?(password_confirmation, data["password"])
abort "Passwords do not match!"
end
puts "---"
data.each { |k, v| puts "#{k.capitalize}: #{v}" }
puts "---"
unless input("Are you sure? (y/N) ").downcase == "y"
abort "Aborted!"
end
puts "Seeding database with admin user..."
user = Db::User.create!(data)
admin = Db::Admin.create!(user_id: user.id)
puts "Done!"
puts "---"
puts "User id: #{user.id}"
puts "Admin id: #{admin.id}"
puts "Token: #{API::Auth.create_user_jwt(user_id: user.id.not_nil!)}"
puts "---"
end
end
end
Commander.run(cli, ARGV)
end
end
end