Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions commands/backups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
script_dir="$(cd "$(dirname "$0")" && pwd)"

# Parse arguments using common.sh function
source ${script_dir}/common.sh
# shellcheck source-path=SCRIPTDIR
source "${script_dir}/common.sh"
parse_environment_args "$@"
init_environment

# Parse backup command from remaining arguments
if [ ${#remaining_args[@]} -eq 0 ]; then
if [[ ${#remaining_args[@]} -eq 0 ]]; then
echo "Error: Please provide a backup command."
echo "Usage: $0 [--local|--production] <create|list|restore> [backup_name]"
exit 1
fi

BACKUP_CMD="${remaining_args[0]}"

if [[ $BACKUP_CMD = "create" ]]; then
cmd="backup"
elif [[ $BACKUP_CMD = "list" ]]; then
cmd="backups"
elif [[ $BACKUP_CMD = "restore" ]]; then
if [ ${#remaining_args[@]} -lt 2 ]; then
# Build the postgres command as an array so subcommand and backup name
# reach the container as separate argv entries (a quoted single string
# would arrive as one argument, which the postgres entrypoint can't exec).
POSTGRES_CMD=()
if [[ "$BACKUP_CMD" == "create" ]]; then
POSTGRES_CMD=(backup)
elif [[ "$BACKUP_CMD" == "list" ]]; then
POSTGRES_CMD=(backups)
elif [[ "$BACKUP_CMD" == "restore" ]]; then
if [[ ${#remaining_args[@]} -lt 2 ]]; then
echo "Error: Please provide backup name for restore."
echo "Usage: $0 [--local|--production] restore <backup_name>"
exit 1
Expand All @@ -30,11 +35,11 @@ elif [[ $BACKUP_CMD = "restore" ]]; then
$COMPOSE_BASE_CMD stop django
echo "Ensuring postgres service is running for restore..."
$COMPOSE_BASE_CMD up -d postgres
cmd="restore ${remaining_args[1]}"
POSTGRES_CMD=(restore "${remaining_args[1]}")
else
echo "Invalid command $BACKUP_CMD"
echo "Usage: $0 [--local|--production] <create|list|restore> [backup_name]"
exit 1
fi

$COMPOSE_BASE_CMD run --rm -it postgres $cmd
$COMPOSE_BASE_CMD run --rm postgres "${POSTGRES_CMD[@]}"
28 changes: 16 additions & 12 deletions commands/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Function to show usage information
show_usage() {
local script_name="${1:-$0}"
local script_name="$0"
echo "Usage: $script_name [OPTIONS] [COMMAND_ARGS...]"
echo ""
echo "Environment Options:"
Expand All @@ -12,6 +12,7 @@ show_usage() {
echo " --help, -h Show this help message"
echo ""
echo "If no environment is specified, auto-detection will be used based on available .envs directories."
return 0
}

# Function to parse environment arguments and return remaining arguments
Expand All @@ -32,6 +33,11 @@ parse_environment_args() {
shift
;;
--env)
if [[ -z "${2:-}" ]]; then
echo "Error: --env requires a value." >&2
show_usage >&2
exit 1
fi
temp_env="$2"
shift 2
;;
Expand All @@ -53,16 +59,15 @@ parse_environment_args() {

# Function to initialize the environment after parsing
init_environment() {
# Skip if already initialized
if [ -n "$COMPOSE_BASE_CMD" ]; then
if [[ -n "${COMPOSE_BASE_CMD:-}" ]]; then
return 0
fi

# Auto-detect environment if not specified
if [ -z "$CODA_ENV" ]; then
if [ -d "$PWD/.envs/.local" ]; then
if [[ -z "$CODA_ENV" ]]; then
if [[ -d "$PWD/.envs/.local" ]]; then
CODA_ENV="local"
elif [ -d "$PWD/.envs/.production" ]; then
elif [[ -d "$PWD/.envs/.production" ]]; then
CODA_ENV="production"
else
echo "Error: Cannot determine environment. Please specify --local or --production, or ensure environment files exist."
Expand All @@ -73,31 +78,30 @@ init_environment() {
fi

# Validate environment
if [ "$CODA_ENV" != "local" ] && [ "$CODA_ENV" != "production" ]; then
if [[ "$CODA_ENV" != "local" && "$CODA_ENV" != "production" ]]; then
echo "Error: Environment must be 'local' or 'production'. Current value: $CODA_ENV"
echo ""
show_usage
exit 1
fi

# Set up compose command based on environment
if [ "$CODA_ENV" = "local" ]; then
if [[ "$CODA_ENV" == "local" ]]; then
export COMPOSE_FILE="compose.local.yml"
export ENV_DIR="$PWD/.envs/.local"
export COMPOSE_BASE_CMD="docker compose -f $COMPOSE_FILE --env-file $ENV_DIR/django.env --env-file $ENV_DIR/postgres.env"
export POSTGRES_DATA_VOLUME="$(basename $PWD)_coda_local_postgres_data"
else
export COMPOSE_FILE="compose.production.yml"
export ENV_DIR="$PWD/.envs/.production"
export COMPOSE_BASE_CMD="docker compose -f $COMPOSE_FILE --env-file $ENV_DIR/coda.env --env-file $ENV_DIR/postgres.env"
export POSTGRES_DATA_VOLUME="$(basename $PWD)_production_postgres_data"
fi

echo "Using environment: $CODA_ENV (compose file: $COMPOSE_FILE)"
}

# Main execution when sourced with arguments (for simple scripts)
if [ $# -gt 0 ]; then
# Standalone execution: resolve and report the environment.
# When sourced (BASH_SOURCE != $0) this file only defines functions.
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
parse_environment_args "$@"
init_environment
fi
5 changes: 4 additions & 1 deletion commands/create-superuser.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash

script_dir="$(cd "$(dirname "$0")" && pwd)"
source ${script_dir}/common.sh "$@"
# shellcheck source-path=SCRIPTDIR
source "${script_dir}/common.sh"
parse_environment_args "$@"
init_environment

$COMPOSE_BASE_CMD exec django pdm run manage.py createsuperuser
3 changes: 2 additions & 1 deletion commands/fix-collation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
script_dir="$(cd "$(dirname "$0")" && pwd)"

# Parse arguments using common.sh function
source ${script_dir}/common.sh
# shellcheck source-path=SCRIPTDIR
source "${script_dir}/common.sh"
parse_environment_args "$@"
init_environment

Expand Down
41 changes: 41 additions & 0 deletions commands/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Import a local data file through a Django management command.
#
# Usage: import.sh [--local|--production] <manage-command> <file>

script_dir="$(cd "$(dirname "$0")" && pwd)"

# shellcheck source-path=SCRIPTDIR
source "${script_dir}/common.sh"
parse_environment_args "$@"
init_environment

usage() {
echo "Usage: $0 [--local|--production] <manage-command> <file>" >&2
return 0
}

if [[ ${#remaining_args[@]} -ne 2 ]]; then
usage
exit 1
fi

# The first argument is passed to manage.py verbatim (e.g.
# import_invoices, import_fundingrequests) — new import commands need no
# change here. The regex only rejects paths/flags that can't be commands.
MANAGE_CMD="${remaining_args[0]}"
if ! [[ "$MANAGE_CMD" =~ ^[a-z][a-z0-9_]*$ ]]; then
echo "Error: first argument must be a manage.py command name, e.g. import_fundingrequests." >&2
usage
exit 1
fi

if [[ ! -f "${remaining_args[1]}" ]]; then
echo "Error: file not found: ${remaining_args[1]}" >&2
exit 1
fi
MOUNT_DIR="$(cd "$(dirname "${remaining_args[1]}")" && pwd)" || exit 1
FILE_NAME="$(basename "${remaining_args[1]}")"

$COMPOSE_BASE_CMD run --rm -v "$MOUNT_DIR:/imports" django pdm run manage.py "$MANAGE_CMD" "/imports/$FILE_NAME"
21 changes: 0 additions & 21 deletions commands/import_invoices.sh

This file was deleted.

21 changes: 0 additions & 21 deletions commands/import_requests.sh

This file was deleted.

24 changes: 19 additions & 5 deletions commands/start-coda.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
#!/bin/bash

script_dir="$(cd "$(dirname "$0")" && pwd)"
source ${script_dir}/common.sh "$@"
# shellcheck source-path=SCRIPTDIR
source "${script_dir}/common.sh"
parse_environment_args "$@"
init_environment

# echo owner/repo for GitHub URLs of any scheme (scp-style, ssh://, https://);
# nonzero for anything that doesn't parse, so _get_repo falls through.
_gh_repo() {
local remote_url="$1"
local repo_slug
repo_slug=$(printf '%s' "$remote_url" | sed 's/.*github.com[:\/]//;s/\.git$//')
[[ "$repo_slug" == */* && "$repo_slug" != *:* ]] || return 1
echo "$repo_slug"
}

_get_repo() {
local remote
remote=$(git rev-parse --abbrev-ref @{upstream} 2>/dev/null | cut -d/ -f1)
local remote url
remote=$(git rev-parse --abbrev-ref "@{upstream}" 2>/dev/null | cut -d/ -f1)
if [[ -n "$remote" ]]; then
git remote get-url "$remote" 2>/dev/null | sed 's/.*github.com[:\/]//' | sed 's/\.git$//' && return
url=$(git remote get-url "$remote" 2>/dev/null) && _gh_repo "$url" && return
fi
git remote get-url origin 2>/dev/null | sed 's/.*github.com[:\/]//' | sed 's/\.git$//' || echo "coda-oa/coda"
url=$(git remote get-url origin 2>/dev/null) && _gh_repo "$url" && return
echo "coda-oa/coda"
}

start_coda() {
Expand Down
5 changes: 4 additions & 1 deletion commands/stop-coda.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/bin/bash

script_dir="$(cd "$(dirname "$0")" && pwd)"
source ${script_dir}/common.sh "$@"
# shellcheck source-path=SCRIPTDIR
source "${script_dir}/common.sh"
parse_environment_args "$@"
init_environment

stop_coda() {
$COMPOSE_BASE_CMD down
Expand Down
Loading