Skip to content

Commit

Permalink
Parse 'envs' file as key=value pairs, while resolving the 'value'
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed May 17, 2024
1 parent eb82197 commit feeeed1
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions bin/ucsf-vpn
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
### * UCSF Managing Your Passwords:
### - https://it.ucsf.edu/services/managing-your-passwords
###
### Version: 5.7.0-9024
### Version: 5.7.0-9025
### Copyright: Henrik Bengtsson (2016-2024)
### License: GPL (>= 2.1) [https://www.gnu.org/licenses/gpl.html]
### Source: https://github.com/HenrikBengtsson/ucsf-vpn
Expand Down Expand Up @@ -1549,7 +1549,7 @@ function openconnect_stop() {


# -------------------------------------------------------------------------
# XDG and cache utility functions
# XDG config utility functions
# -------------------------------------------------------------------------
function xdg_config_path() {
local path
Expand All @@ -1575,19 +1575,49 @@ function pii_cleanup() {
}


# Function to safely parse and set environment variables for file
source_envs() {
local file line key value

file="$(xdg_config_path)/envs"

## Nothing to do?
if [[ ! -f "${file}" ]]; then
return 0
fi

while IFS= read -r line; do
# Skip empty lines and lines starting with #
[[ -z "$line" ]] && continue
[[ "$line" =~ ^[[:blank:]]*# ]] && continue

# Assert that line specifies a key=value pair
if [[ ! "$line" =~ ^[a-zA-Z_][a-zA-Z0-9_]*=.*$ ]]; then
merror "Syntax error in ${file}: ${line}"
fi

# Split the line into key and value parts
key="${line%%=*}"
value="${line#*=}"

# Resolve any variables and expressions in the value part
eval "value=${value}"

# Assign the resolved value to environment variable specified by the key
export "$key=$value"
done < "${file}"
}


# -------------------------------------------------------------------------
# MAIN
# -------------------------------------------------------------------------
pid_file="$(xdg_config_path)/openconnect.pid"
envs_file="$(xdg_config_path)/envs"
ip_route_novpn_file="$(xdg_config_path)/ip-route.novpn.out"
ip_route_vpn_file="$(xdg_config_path)/ip-route.vpn.out"
pii_file=$(make_pii_file)

if [[ -f "${envs_file}" ]]; then
# shellcheck disable=SC1090
source "${envs_file}"
fi
source_envs

## Actions
action=
Expand Down

0 comments on commit feeeed1

Please sign in to comment.