82 lines
1.9 KiB
Crystal
82 lines
1.9 KiB
Crystal
require "commander"
|
|
require "fancyline"
|
|
|
|
require "./db"
|
|
|
|
module API
|
|
module CLI
|
|
extend self
|
|
|
|
private FANCY = Fancyline.new
|
|
|
|
FANCY.actions.set Fancyline::Key::Control::CtrlC do
|
|
exit
|
|
end
|
|
|
|
private def input(prompt : String) : String
|
|
x = FANCY.readline(prompt)
|
|
|
|
if x
|
|
x.chomp.strip
|
|
else
|
|
""
|
|
end
|
|
end
|
|
|
|
cli = Commander::Command.new do |cmd|
|
|
cmd.use = "api"
|
|
cmd.long = "Mentorenwahl API CLI"
|
|
|
|
cmd.run do
|
|
API.run
|
|
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" => 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 !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: #{Auth.create_user_jwt(user_id: user.id.not_nil!)}"
|
|
puts "---"
|
|
end
|
|
end
|
|
end
|
|
|
|
Commander.run(cli, ARGV)
|
|
end
|
|
end
|