Updated cli

This commit is contained in:
Dominic Grimm 2022-01-10 14:47:08 +01:00
parent fc0babff13
commit 60a714fa82
5 changed files with 102 additions and 103 deletions

View file

@ -28,8 +28,9 @@ COPY ./scripts ./scripts
RUN . ./scripts/build.sh ${BUILD_ENV}
FROM scratch as runner
COPY --from=micrate-builder /app/bin/micrate .
COPY --from=builder /app/bin/mw .
WORKDIR /
COPY --from=micrate-builder /app/bin/micrate ./bin/micrate
COPY --from=builder /app/bin/mw ./bin/mw
COPY ./db ./db
EXPOSE 8080
CMD [ "/mw" ]
CMD [ "mw" ]

View file

@ -6,7 +6,7 @@ authors:
targets:
mw:
main: src/app.cr
main: src/mw.cr
crystal: 1.3.0

View file

@ -1,94 +0,0 @@
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)

View file

@ -0,0 +1,97 @@
require "commander"
require "compiled_license"
require "./db"
module MW
module Cli
extend self
private 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 CompiledLicense::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..."
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

View file

@ -1,5 +0,0 @@
require "compiled_license"
module MW
LICENSES = CompiledLicense::LICENSES
end