mentorenwahl/docker/backend/src/app.cr
2022-01-08 13:29:22 +01:00

95 lines
2.1 KiB
Crystal

require "commander"
require "./mw.cr"
def input(prompt : String) : String
print prompt
(gets || "").chomp.strip
end
cli = Commander::Command.new do |cmd|
cmd.use = "mw"
cmd.long = "Mentorenwahl"
cmd.run do
MW.run
end
cmd.commands.add do |c|
c.use = "version"
c.long = "Prints the current version"
c.run do
puts MW::VERSION
end
end
cmd.commands.add do |c|
c.use = "authors"
c.long = "Prints the authors"
c.run do
puts MW::AUTHORS.join(",\n")
end
end
cmd.commands.add do |c|
c.use = "licenses"
c.long = "Prints the licenses of libraries used"
c.run do
puts MW::LICENSES
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..."
# firstname = input "Firstname: "
# lastname = input "Lastname: "
# email = input "Email: "
# password = input "Password: "
# password_confirmation = input "Password confirmation: "
data = {
"firstname" => input("Firstname: "),
"lastname" => input("Lastname: "),
"email" => input("Email: "),
"password" => MW::Auth.hash_password(input("Password: ")),
"role" => MW::Db::UserRole::Admin.to_s,
}
password_confirmation = input("Password confirmation: ")
if data.values.any?(&.empty?)
abort "Values can't be empty!"
elsif !MW::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) ") == "y"
abort "Aborted!"
end
puts "Seeding database with admin user..."
user = MW::Db::User.create!(data)
admin = MW::Db::Admin.create!(user_id: user.id)
puts "Done!"
puts "---"
puts "User id: #{user.id}"
puts "Admin id: #{admin.id}"
puts "Token: #{MW::Auth.create_user_jwt(user_id: user.id.not_nil!)}"
puts "---"
end
end
end
Commander.run(cli, ARGV)