From a92b94cccb81a36349131b4f2d98a566fddbef9f Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Sat, 22 Sep 2018 21:24:44 +0200 Subject: [PATCH] Primitive adduser tool added. --- shard.yml | 2 ++ src/adduser.cr | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/adduser.cr diff --git a/shard.yml b/shard.yml index f39318c..4527c78 100644 --- a/shard.yml +++ b/shard.yml @@ -11,6 +11,8 @@ description: | targets: authd: main: src/main.cr + authd-adduser: + main: src/adduser.cr crystal: 0.26 diff --git a/src/adduser.cr b/src/adduser.cr new file mode 100644 index 0000000..9384dbc --- /dev/null +++ b/src/adduser.cr @@ -0,0 +1,89 @@ + +require "option_parser" + +require "./user.cr" + +user_name : String? = nil +user_password : String? = nil +user_perms = Array(String).new + +database_url = "postgres:localhost" +database_name = "authd" +database_user = "nico" +database_password = "nico-nico-nii" + +OptionParser.parse! do |parser| + parser.banner = "usage: #{ARGV[0]} [arguments]" + + parser.on "-a url", "--database url", "URL of authd’s database." do |url| + database_url = url + end + parser.on "-U name", "--database-user name", "Name of the database’s user." do |name| + database_user = name + end + parser.on "-P password", "--database-pasword password", "Password of the database’s user." do |password| + database_password = password + end + parser.on "-N name", "--database-name name", "Name of the database." do |name| + database_name = name + end + + parser.on "-u name", "--username name", "Name of the user to add." do |name| + user_name = name + end + parser.on "-p password", "--password password", "Password of the user." do |password| + user_password = password + end + parser.on "-x perm", "--permission", "Add a permission token to the user." do |token| + user_perms << token + end + + parser.on("-h", "--help", "Show this help") do + puts parser + exit 0 + end + + parser.invalid_option do |flag| + puts "error: #{flag} is not a valid option." + puts parser + + exit 1 + end +end + +module DataBase + extend Crecto::Repo +end + +DataBase.config do |conf| + conf.adapter = Crecto::Adapters::Postgres + conf.hostname = database_url + conf.username = database_user + conf.password = database_password + conf.database = database_name +end + +if user_name.nil? + STDERR.puts "No username specified." + exit 1 +end +if user_password.nil? + STDERR.puts "No user password specified." + exit 1 +end + +user = User.new +user.username = user_name +user.password = user_password +user.perms = user_perms +changeset = DataBase.insert user + +if changeset.valid? + puts "User added successfully." +else + changeset.errors.each do |e| + p e + end +end + +