From 60a714fa82373d170e43f510e09e1eb09289aecc Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Mon, 10 Jan 2022 14:47:08 +0100 Subject: [PATCH] Updated cli --- docker/backend/Dockerfile | 7 ++- docker/backend/shard.yml | 2 +- docker/backend/src/app.cr | 94 ------------------------------ docker/backend/src/mw/cli.cr | 97 +++++++++++++++++++++++++++++++ docker/backend/src/mw/licenses.cr | 5 -- 5 files changed, 102 insertions(+), 103 deletions(-) delete mode 100644 docker/backend/src/app.cr create mode 100644 docker/backend/src/mw/cli.cr delete mode 100644 docker/backend/src/mw/licenses.cr diff --git a/docker/backend/Dockerfile b/docker/backend/Dockerfile index a7633c5..eed1ef4 100644 --- a/docker/backend/Dockerfile +++ b/docker/backend/Dockerfile @@ -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" ] diff --git a/docker/backend/shard.yml b/docker/backend/shard.yml index 5176573..12498d0 100644 --- a/docker/backend/shard.yml +++ b/docker/backend/shard.yml @@ -6,7 +6,7 @@ authors: targets: mw: - main: src/app.cr + main: src/mw.cr crystal: 1.3.0 diff --git a/docker/backend/src/app.cr b/docker/backend/src/app.cr deleted file mode 100644 index 49d6c90..0000000 --- a/docker/backend/src/app.cr +++ /dev/null @@ -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) diff --git a/docker/backend/src/mw/cli.cr b/docker/backend/src/mw/cli.cr new file mode 100644 index 0000000..266c040 --- /dev/null +++ b/docker/backend/src/mw/cli.cr @@ -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 diff --git a/docker/backend/src/mw/licenses.cr b/docker/backend/src/mw/licenses.cr deleted file mode 100644 index 523196d..0000000 --- a/docker/backend/src/mw/licenses.cr +++ /dev/null @@ -1,5 +0,0 @@ -require "compiled_license" - -module MW - LICENSES = CompiledLicense::LICENSES -end