aboutsummaryrefslogtreecommitdiff
path: root/usergen
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-03-16 01:53:33 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-03-16 01:55:22 +0100
commitf6fd20fdc4def2406614eebb958ef15f6085bb0e (patch)
tree0c115b0458e24b51644b87b9187682f6d1e8eb15 /usergen
parent27b8a6971861cb4ead0eb07965bb4873f591ad24 (diff)
downloadslcl-f6fd20fdc4def2406614eebb958ef15f6085bb0e.tar.gz
Make usergen a bit more useful
So far, usergen printed a JSON object over standard output that had to be manually copied into db.json. Now, this step is done automatically, thanks to jq(1). OTOH, user directory is now also created by usergen.
Diffstat (limited to 'usergen')
-rwxr-xr-xusergen46
1 files changed, 39 insertions, 7 deletions
diff --git a/usergen b/usergen
index ec63fd3..42c18fe 100755
--- a/usergen
+++ b/usergen
@@ -2,8 +2,29 @@
set -e
+usage()
+{
+ echo "$0 <dir>"
+}
+
+if [ $# != 1 ]; then
+ usage >&2
+ exit 1
+fi
+
+DIR=$1
+
echo Username: >&2
read -r USER
+
+DB="$DIR/db.json"
+
+if jq '.users[].name' "$DB" | grep -q $USER
+then
+ echo User $USER already in $DB >&2
+ exit 1
+fi
+
echo Password: >&2
read -r PWD
echo "Quota, in MiB (leave empty for unlimited quota):" >&2
@@ -23,12 +44,23 @@ do
done
echo >&2
-cat <<-EOF
+TMP=$(mktemp)
+
+cleanup()
{
- "name": "$USER",
- "password": "$PWD",
- "salt": "$SALT",
- "key": "$KEY",
- "quota": "$QUOTA"
+ rm -f $TMP
}
-EOF
+
+trap cleanup EXIT
+
+jq ".users += [
+{
+ \"name\": \"$USER\",
+ \"password\": \"$PWD\",
+ \"salt\": \"$SALT\",
+ \"key\": \"$KEY\",
+ \"quota\": \"$QUOTA\"
+}]" "$DB" > $TMP
+
+mv $TMP "$DB"
+mkdir "$DIR/user/$USER"