Skip to content

Commit 713c255

Browse files
committed
update: safename function to parse environment variable.
1 parent 60498ed commit 713c255

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

.foam/templates/user.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ foam_template:
1919
```
2020
2121
```zsh env-invoked
22-
set_current_user ${FOAM_TITLE}
22+
set_current_user ${FOAM_TITLE/@(\S*)$//}
2323
```
2424

2525
#### information

.vscode/env.zsh

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ function cut_lines_from_markdown_codes() {
5151
done
5252
}
5353

54+
function safe_name() {
55+
local name=$1
56+
if [[ -z $name ]]; then
57+
echo "Usage: safe_name <name>"
58+
return 1
59+
fi
60+
echo "$name" | tr '@$.-' '____' # replace . and - with _ to avoid env var issues
61+
}
62+
5463
function update_host_to_env() {
5564
if [[ -x "$(command -v yq)" && -d "${PROJECT_FOLDER}/hosts" ]]; then
5665
for ur in $(ls -1 ${PROJECT_FOLDER}/hosts); do
@@ -59,7 +68,7 @@ function update_host_to_env() {
5968
local host_data=$(cut_lines_from_markdown_codes "$file" "yaml host")
6069

6170
local hostname=$(echo "$host_data" | yq '.[0].hostname' -r)
62-
local _var=$(echo "$hostname" | sed -e "s/\./_/g" | sed -e "s/-/_/g" | sed -e 's/\$/_/g') # replace . and - with _ to avoid env var issues
71+
local _var=$(safe_name "$hostname" ) # replace . and - with _ to avoid env var issues
6372

6473
local ip=$(echo "$host_data" | yq '.[0].ip' -r)
6574
local is_dc=$(echo "$host_data" | yq '.[0].is_dc' -r)
@@ -94,7 +103,7 @@ function set_current_host() {
94103
return 1
95104
fi
96105

97-
export CURRENT_HOST=$(echo "$1" | sed -e "s/\./_/g" | sed -e "s/-/_/g" | sed -e 's/\$/_/g') # replace . and - with _ to avoid env var issues
106+
export CURRENT_HOST=$(safe_name "$1") # replace . and - with _ to avoid env var issues
98107
export CURRENT_IP=$(eval echo '$IP_'$CURRENT_HOST) # alias for IP_dc01 or IP_dc02
99108
export CURRENT_HOSTNAME=$(eval echo '$HOST_'$CURRENT_HOST) # alias for HOST_dc01 or HOST_dc02
100109

@@ -119,9 +128,11 @@ function update_user_cred_to_env() {
119128
local usercred=$(cut_lines_from_markdown_codes "$file" "yaml credentials")
120129

121130
local user=$(echo "$usercred" | yq '.[0].user' -r)
122-
local _var=$(echo "$user" | sed -e "s/\./_/g" | sed -e "s/-/_/g" | sed -e 's/\$/_/g') # replace . and - with _ to avoid env var issues
131+
local _var=$(safe_name "$user") # replace . and - with _ to avoid env var issues
123132
local pass=$(echo "$usercred" | yq '.[0].password' -r)
124133
local nt_hash=$(echo "$usercred" | yq '.[0].nt_hash' -r)
134+
local login=$(echo "$usercred" | yq '.[0].login' -r)
135+
local LOGIN_${_var}=$login
125136
export USER_${_var}=$user
126137
export PASS_${_var}=$pass
127138
export NT_HASH_${_var}=$nt_hash
@@ -141,17 +152,19 @@ function set_current_user() {
141152
env | egrep '^USER_' | sed -e 's/USER_//g' | awk '{printf "- " $1 "\n"}' | sed -e 's/=/: /g' | sort
142153
return 1
143154
fi
144-
export CURRENT=$(echo "$1" | sed -e "s/\./_/g" | sed -e "s/-/_/g" | sed -e 's/\$/_/g') # replace . and - with _ to avoid env var issues
155+
export CURRENT=$(safe_name "$1" ) # replace . and - with _ to avoid env var issues
145156
export CURRENT_USER=$(eval echo '$USER_'$CURRENT) # alias for USER_A or USER_B
146157
export CURRENT_PASS=$(eval echo '$PASS_'$CURRENT) # alias for PASS_A or PASS_B
147158
export CURRENT_NT_HASH=$(eval echo '$NT_HASH_'$CURRENT) # alias for NT_HASH_A or NT_HASH_B
148-
159+
export CURRENT_LOGIN=$(eval echo '$LOGIN_'$CURRENT) # alias for LOGIN_A or LOGIN_B
160+
149161
# defined variables if u need
150162
export USER=${CURRENT_USER}
151163
export USERNAME=${CURRENT_USER}
152164
export PASS=${CURRENT_PASS}
153165
export PASSWORD=${CURRENT_PASS} # alias for PASS
154166
export NT_HASH=${CURRENT_NT_HASH} # alias for NT_HASH_A
167+
export LOGIN=${CURRENT_LOGIN} # alias for LOGIN_A
155168
}
156169
# set_current_user
157170

0 commit comments

Comments
 (0)