gitea-init/env-gen-install.sh

42 lines
1011 B
Bash
Raw Permalink Normal View History

2024-08-22 21:03:07 +05:00
#!/bin/bash
if [ -z "$1" ]; then
echo "Use: $0 <var_name> [value]"
exit 1
fi
VARIABLE_NAME=$1
ENV_FILE="/data/gitea-init/install.env"
trap 'echo -e "\033[31menv-gen-install.sh: Something went wrong\033[0m"; exit 1' ERR
set -e
# Password gen: 20chars,0-9,a-z
generate_random_password() {
pwgen -s 20 1
}
# If the second parameter is specified, use it as the value of the variable
if [ -n "$2" ]; then
VALUE=$2
else
VALUE=$(generate_random_password)
fi
echo "Creating dir and .env file if it doesn't exist..."
if [ ! -f "$ENV_FILE" ]; then
mkdir -p "$(dirname "$ENV_FILE")"
touch "$ENV_FILE"
fi
echo "Update or add a variable to the .env file..."
if grep -q "^$VARIABLE_NAME=" "$ENV_FILE"; then
# The variable exists, update its value
sed -i "s/^$VARIABLE_NAME=.*/$VARIABLE_NAME=$VALUE/" "$ENV_FILE"
else
# The variable does not exist, add it to the file
echo "$VARIABLE_NAME=$VALUE" >> "$ENV_FILE"
fi
trap - ERR
echo "Variable $VARIABLE_NAME successfully updated/added to $ENV_FILE"