From e94617967f6f945440085ca36a2eb8633089ad5d Mon Sep 17 00:00:00 2001 From: Esteban Vincent Date: Tue, 19 May 2026 12:24:34 +0200 Subject: [PATCH] refactor: extract env loading into shared utils.sh helper --- scripts/get_graph_user.sh | 18 ++-------------- scripts/imgflip-meme.sh | 21 ++----------------- utils.sh | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 utils.sh diff --git a/scripts/get_graph_user.sh b/scripts/get_graph_user.sh index c34d4d8..61526fa 100755 --- a/scripts/get_graph_user.sh +++ b/scripts/get_graph_user.sh @@ -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" \ diff --git a/scripts/imgflip-meme.sh b/scripts/imgflip-meme.sh index a808e78..84c2276 100755 --- a/scripts/imgflip-meme.sh +++ b/scripts/imgflip-meme.sh @@ -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) diff --git a/utils.sh b/utils.sh new file mode 100644 index 0000000..29e49c3 --- /dev/null +++ b/utils.sh @@ -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 +}