refactor: extract env loading into shared utils.sh helper

This commit is contained in:
Esteban Vincent
2026-05-19 12:24:34 +02:00
committed by ForgeCode
parent 1f507fdb38
commit e94617967f
3 changed files with 47 additions and 35 deletions
+2 -16
View File
@@ -15,23 +15,9 @@
USER_ID="$1"
ENV_FILE="$HOME/Raycast/envs/get_graph_user.env"
if [[ ! -f "$ENV_FILE" ]]; then
echo "❌ .env not found at $ENV_FILE"
exit 1
fi
set -o allexport
# shellcheck source=/dev/null
source "$ENV_FILE"
set +o allexport
for var in AZURE_TENANT_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET; do
if [[ -z "${!var}" ]]; then
echo "❌ Missing $var in .env"
exit 1
fi
done
source "$HOME/Raycast/utils.sh"
load_env AZURE_TENANT_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET || exit 1
TOKEN_RESPONSE=$(curl -s -w "\n%{http_code}" \
-X POST "https://login.microsoftonline.com/${AZURE_TENANT_ID}/oauth2/v2.0/token" \
+2 -19
View File
@@ -13,26 +13,9 @@
# @raycast.description Generate a meme via imgflip webhook and copy image to clipboard
# @raycast.author Esteban Vincent
ENV_FILE="$HOME/Raycast/envs/imgflip-meme.env"
if [[ ! -f "$ENV_FILE" ]]; then
echo "❌ .env not found at $ENV_FILE"
exit 1
fi
set -o allexport
# shellcheck source=/dev/null
source "$ENV_FILE"
set +o allexport
# Re-read literally to preserve $ in value, strip surrounding quotes
N8N_WEBHOOK_IMGFLIP_API_KEY=$(grep -m1 '^N8N_WEBHOOK_IMGFLIP_API_KEY=' "$ENV_FILE" | cut -d= -f2- | sed "s/^['\"]//;s/['\"]$//")
for var in N8N_WEBHOOK_BASE_URL N8N_WEBHOOK_IMGFLIP_API_KEY; do
if [[ -z "${!var}" ]]; then
echo "❌ Missing $var in .env"
exit 1
fi
done
source "$HOME/Raycast/utils.sh"
load_env N8N_WEBHOOK_BASE_URL N8N_WEBHOOK_IMGFLIP_API_KEY || exit 1
QUERY="$1"
TMPFILE=$(mktemp /tmp/meme_XXXXXX.jpg)
+43
View File
@@ -0,0 +1,43 @@
#!/bin/bash
# Helper: source this file, then call load_env
# Usage: source "$HOME/Raycast/utils/load_env.sh"
# load_env VAR1 VAR2 VAR3 || exit 1
# Side-effect: sets LOADED_ENV_FILE to the resolved path
load_env() {
local caller
caller="$(basename "${BASH_SOURCE[1]}" .sh)"
LOADED_ENV_FILE="$HOME/Raycast/envs/${caller}.env"
if [[ ! -f "$LOADED_ENV_FILE" ]]; then
echo "❌ .env not found at $LOADED_ENV_FILE"
return 1
fi
local line key value
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip empty lines and comments
[[ "$line" =~ ^[[:space:]]*$ ]] && continue
[[ "$line" =~ ^[[:space:]]*# ]] && continue
# Strip optional leading 'export '
line="${line#export }"
key="${line%%=*}"
value="${line#*=}"
# Strip surrounding quotes (preserves all chars inside, including $)
if [[ "$value" == \"*\" ]]; then
value="${value#\"}"
value="${value%\"}"
elif [[ "$value" == \'*\' ]]; then
value="${value#\'}"
value="${value%\'}"
fi
export "$key=$value"
done < "$LOADED_ENV_FILE"
for var in "$@"; do
if [[ -z "${!var}" ]]; then
echo "❌ Missing $var in .env"
return 1
fi
done
}