Skip to content

Commit cc446e1

Browse files
committed
make start-client CLI more erganomic with named args and help
1 parent f54cd73 commit cc446e1

File tree

1 file changed

+68
-13
lines changed

1 file changed

+68
-13
lines changed

nix/tools/run-client.sh.in

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,81 @@
33

44
[ ! -z "$DEBUG" ] && set -x
55

6+
# Default values
7+
PSQL_VERSION="15"
8+
MIGRATION_FILE=""
9+
PORTNO="@PGSQL_DEFAULT_PORT@"
10+
11+
# Function to display help
12+
print_help() {
13+
echo "Usage: nix run .#start-client -- [options]"
14+
echo
15+
echo "Options:"
16+
echo " -v, --version [15|16|orioledb-16] Specify the PostgreSQL version to use (required)"
17+
echo " -f, --file FILE Provide a custom migration script"
18+
echo " -h, --help Show this help message"
19+
echo
20+
echo "Description:"
21+
echo " This script wraps 'psql' and allows you to specify the PostgreSQL version to use."
22+
echo " If no migration file is provided, it runs the default Supabase migrations."
23+
echo
24+
echo "Examples:"
25+
echo " nix run .#start-client"
26+
echo " nix run .#start-client -- --version 15"
27+
echo " nix run .#start-client -- --version 16 --file custom_migration.sql"
28+
echo " nix run .#start-client -- --version 16 --port 5433"
29+
}
30+
31+
# Parse arguments
32+
while [[ "$#" -gt 0 ]]; do
33+
case "$1" in
34+
-v|--version)
35+
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
36+
PSQL_VERSION="$2"
37+
shift 2
38+
else
39+
echo "Error: --version requires an argument (15, 16, or orioledb-16)"
40+
exit 1
41+
fi
42+
;;
43+
-f|--file)
44+
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
45+
MIGRATION_FILE="$2"
46+
shift 2
47+
else
48+
echo "Error: --file requires a filename"
49+
exit 1
50+
fi
51+
;;
52+
-h|--help)
53+
print_help
54+
exit 0
55+
;;
56+
*)
57+
echo "Unknown option: $1"
58+
print_help
59+
exit 1
60+
;;
61+
esac
62+
done
63+
64+
# Check if version is provided
65+
if [[ -z "$PSQL_VERSION" ]]; then
66+
echo "Error: PostgreSQL version is required."
67+
print_help
68+
exit 1
69+
fi
70+
671
# Determine PostgreSQL version
7-
if [ "$1" == "15" ]; then
72+
if [ "$PSQL_VERSION" == "15" ]; then
873
echo "Starting client for PSQL 15"
974
PSQL15=@PSQL15_BINDIR@
1075
BINDIR="$PSQL15"
11-
elif [ "$1" == "16" ]; then
76+
elif [ "$PSQL_VERSION" == "16" ]; then
1277
echo "Starting client for PSQL 16"
1378
PSQL16=@PSQL16_BINDIR@
1479
BINDIR="$PSQL16"
15-
elif [ "$1" == "orioledb-16" ]; then
80+
elif [ "$PSQL_VERSION" == "orioledb-16" ]; then
1681
echo "Starting client for PSQL ORIOLEDB 16"
1782
PSQLORIOLEDB16=@PSQLORIOLEDB16_BINDIR@
1883
BINDIR="$PSQLORIOLEDB16"
@@ -21,20 +86,10 @@ else
2186
exit 1
2287
fi
2388

24-
shift 1
25-
2689
#vars for migration.sh
2790
export PATH=$BINDIR/bin:$PATH
2891
export POSTGRES_DB=postgres
2992
export POSTGRES_HOST=localhost
30-
export POSTGRES_PORT=@PGSQL_DEFAULT_PORT@
31-
32-
# Optional second argument: path to custom migration script
33-
if [ -n "$1" ]; then
34-
MIGRATION_FILE="$1"
35-
else
36-
MIGRATION_FILE=""
37-
fi
3893

3994
PORTNO="${2:-@PGSQL_DEFAULT_PORT@}"
4095
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@

0 commit comments

Comments
 (0)