auto-choose binary, implement add.sh script

This commit is contained in:
Yuri Zamyatin 2024-07-16 03:57:24 +04:00
parent 261d4a7b1e
commit 08c7ac1a63
2 changed files with 71 additions and 3 deletions

66
scripts/add.sh Normal file
View File

@ -0,0 +1,66 @@
#!/bin/bash
DB_PATH=$(realpath "$1")
print_help() {
echo -e "add.sh - convenience script to quickly add some things without details. Usage:\n"
echo -e "tx\t<amt> <asset type name> <asset code> <storage name> <category name> [<datetime>]"
echo -e "bal\t<amt> <asset type name> <asset code> <storage name>"
echo -e "rate\t<amt> <asset type name> <asset code>\n"
}
num_arg() {
ARG="${1//[^A-Za-z0-9-\.]/}"
if [ -z "$ARG" ]; then
echo NULL
else
echo "$ARG"
fi
}
text_arg() {
ARG="${1//\'/\'\'}"
if [ -z "$ARG" ]; then
echo NULL
else
echo "'$ARG'"
fi
}
make_query() {
TARGET="$1"
shift 1
case "$TARGET" in
"tx")
echo "insert into latest_fin_transactions(amount,asset_type,asset_code,storage,category,datetime) values($(num_arg "$1"),$(text_arg "$2"),$(text_arg "$3"),$(text_arg "$4"),$(text_arg "$5"),$(text_arg "$6"));";;
"bal")
echo "insert into current_balances(balance,asset_type,asset_code,storage) values($(num_arg "$1"),$(text_arg "$2"),$(text_arg "$3"),$(text_arg "$4"));";;
"rate")
echo "insert into current_fin_asset_rates(rate,asset_type,asset) values($(num_arg "$1"),$(text_arg "$2"),$(text_arg "$3"))";;
esac
}
print_help
MIME=$(file -b --mime-type "$DB_PATH")
if [ "$MIME" = "application/vnd.sqlite3" ]; then
BIN_PATH=sqlite3
else
BIN_PATH=sqlcipher
read -srp "Password: " PASS; echo '';
fi
while read -rp '> ' ARGS_STR; do
eval "ARGS=($ARGS_STR)"
SQL=$(make_query "${ARGS[@]}")
if [ -z "$SQL" ]; then
echo -e "Error: unknown command\n"
print_help
continue
fi
$BIN_PATH "$DB_PATH" <<EOF
.output '/dev/null'
pragma key='$PASS';
pragma foreign_keys=ON;
.output 'stdout'
$SQL
EOF
done

View File

@ -1,4 +1,5 @@
#!/bin/bash
set -e
DB_PATH=$(realpath "$1")
DUMP_PATH=$(realpath "$2")
@ -21,11 +22,12 @@ order by
SQL
)
read -s -p "Password (if any): " PASS; echo '';
if [ -z "$PASS" ]; then
MIME=$(file -b --mime-type "$DB_PATH")
if [ "$MIME" = "application/vnd.sqlite3" ]; then
BIN_PATH=sqlite3
else
BIN_PATH=sqlcipher
read -srp "Password: " PASS; echo '';
fi
# dump
@ -39,6 +41,6 @@ EOF
# test
TEST_DB_PATH="$DUMP_PATH.test.db"
rm -f "$TEST_DB_PATH"
cat "$DUMP_PATH" | sqlite3 "$TEST_DB_PATH"
sqlite3 "$TEST_DB_PATH" < "$DUMP_PATH"
echo 'pragma foreign_key_check;' | sqlite3 "$TEST_DB_PATH"
rm -f "$TEST_DB_PATH"