From 6d5ca0f46c55519aff239684639c5278d3bd2425 Mon Sep 17 00:00:00 2001 From: cbdev Date: Sat, 7 May 2022 15:32:29 +0200 Subject: Extend README, implement cargohold-cli --- backend/cargohold-cli | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 backend/cargohold-cli (limited to 'backend') diff --git a/backend/cargohold-cli b/backend/cargohold-cli new file mode 100755 index 0000000..99c7caa --- /dev/null +++ b/backend/cargohold-cli @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +DATABASE=cargohold.db3 + +usage(){ + printf "This utility can be used to manage the cargohold database from the command line\n\n" + printf "Cargohold database used: %s\n\n" "$DATABASE" + printf "Usage:\n" + printf "\t%s adduser []\n" "$0" + printf "\tExample: %s adduser foo 10000\n" "$0" + printf "\tThis will add user 'foo' with a storage limit of 10K bytes\n\n" + printf "\t%s addalias []\n" "$0" + printf "\tExample: %s addalias foo mydirectory c send-me-files\n" "$0" + printf "\tThis will make the directory 'mydirectory' under the fileroot (and optionally userdir) available for uploads ('c') under the alias 'send-me-files'\n\n" + printf "\t%s list\n" "$0" + printf "\tList all users and configured aliases\n" +} + +# adduser [] +adduser(){ + if [ -z "$1" ]; then + usage + exit 1 + fi + limit=${2:-0} + + printf "Adding user %s\n" "$1" + sqlite3 "$DATABASE" "PRAGMA foreign_keys = ON; INSERT INTO users (name, storage) VALUES ('$1', $limit);" +} + +# addalias [] +addalias(){ + if [ "$#" -lt 3 ]; then + usage + exit 1 + fi + alias=${4:-$(xxd -g 0 -l 16 -p /dev/urandom)} + + printf "Adding alias %s pointing to %s (ACL %s, User %s)\n" "$alias" "$2" "$3" "$1" + sqlite3 "$DATABASE" "PRAGMA foreign_keys = ON; INSERT INTO aliases (alias, user, real, access) VALUES ('$alias', '$1', '$2', '$3');" +} + +list(){ + printf "Listing all users\n" + sqlite3 -column -header "$DATABASE" "SELECT name, storage FROM users;" + + printf "\nListing all aliases\n" + sqlite3 -column "$DATABASE" "SELECT user || ': ' || alias || ' (' || access || ') -> ' || real FROM aliases;" +} + +if [ "$#" -lt 1 ]; then + usage + exit 0 +fi + +case "$1" in + "adduser") + shift + adduser $@ + ;; + "addalias") + shift + addalias $@ + ;; + "list") + list + ;; + *) + usage + exit 1 + ;; +esac -- cgit v1.2.3