From f157c61914f9a4da32b4add1920f766dbb66774c Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Mon, 13 Apr 2026 15:56:12 +0530 Subject: saving previously selected option for vpn-connect script --- modules/scripts/vpn-connect.sh | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'modules/scripts/vpn-connect.sh') diff --git a/modules/scripts/vpn-connect.sh b/modules/scripts/vpn-connect.sh index f325e17..87cfdcc 100644 --- a/modules/scripts/vpn-connect.sh +++ b/modules/scripts/vpn-connect.sh @@ -2,17 +2,41 @@ CONF_DIR="$HOME/.config/openvpn" CREDS_DIR="$CONF_DIR/credentials" SERVERS_DIR="$CONF_DIR/servers" +CACHE_FILE="$HOME/.cache/openvpn/last_connection" -# Create directories if they don't exist -mkdir -p "$CREDS_DIR" "$SERVERS_DIR" +# Create directories +mkdir -p "$CREDS_DIR" "$SERVERS_DIR" "$(dirname "$CACHE_FILE")" -# Select the credential file -CRED=$(find "$CREDS_DIR" -type f | fzf --prompt="Select Credentials > ") -[ -z "$CRED" ] && exit 1 +# Logic to force a refresh (bypass cache) +REFRESH=false +if [[ "$1" == "--refresh" || "$1" == "-r" ]]; then + REFRESH=true +fi -# Select the VPN server config -SERVER=$(find "$SERVERS_DIR" -type f \( -name "*.conf" -o -name "*.ovpn" \) | fzf --prompt="Select VPN Server > ") -[ -z "$SERVER" ] && exit 1 +# Try to load from cache +if [[ "$REFRESH" == false && -f "$CACHE_FILE" ]]; then + source "$CACHE_FILE" + # Validate cached files still exist + if [[ ! -f "$CRED" || ! -f "$SERVER" ]]; then + echo "Cached config not found, falling back to selection..." + REFRESH=true + fi +else + REFRESH=true +fi + +# If we need a refresh, run fzf +if [[ "$REFRESH" == true ]]; then + CRED=$(find "$CREDS_DIR" -type f | fzf --prompt="Select Credentials > ") + [ -z "$CRED" ] && exit 1 + + SERVER=$(find "$SERVERS_DIR" -type f \( -name "*.conf" -o -name "*.ovpn" \) | fzf --prompt="Select VPN Server > ") + [ -z "$SERVER" ] && exit 1 + + # Save to cache + echo "CRED=\"$CRED\"" > "$CACHE_FILE" + echo "SERVER=\"$SERVER\"" >> "$CACHE_FILE" +fi echo "Connecting to $SERVER using $CRED..." -- cgit v1.2.3