40 lines
993 B
Bash
40 lines
993 B
Bash
#!/bin/bash
|
|
|
|
if [ -z "$1" ]; then
|
|
echo "Use: $0 <var_name> [value]"
|
|
exit 1
|
|
fi
|
|
|
|
VARIABLE_NAME=$1
|
|
ENV_FILE="/data/secrets/$SERVER_DOMAIN/$SERVER_DOMAIN.env"
|
|
|
|
# Password gen: 20chars,0-9,a-z
|
|
generate_random_password() {
|
|
#tr -dc 'a-z0-9' </dev/urandom | head -c 20
|
|
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
|
|
|
|
# Creating .env file if it doesn't exist
|
|
if [ ! -f "$ENV_FILE" ]; then
|
|
# Creating directories if they don't exist
|
|
mkdir -p "$(dirname "$ENV_FILE")"
|
|
touch "$ENV_FILE"
|
|
fi
|
|
|
|
# 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
|
|
|
|
echo "Variable $VARIABLE_NAME successfully updated/added to $ENV_FILE" |