-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00-env-functions.plugin.bash
More file actions
207 lines (194 loc) · 6.85 KB
/
00-env-functions.plugin.bash
File metadata and controls
207 lines (194 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#--- FUNCTION ----------------------------------------------------------------
# NAME: _add2env
# DESCRIPTION: Adds new value to env variable. The first parameter must be set,
# rest is optional. If they are omitted then it'll be used PATH
# as variable and : as separator.
# It works with Bash and Ksh as well. But it does not work with Zsh yet.
# SYNOPSIS: _add2env options
# USAGE: _add2env value
# _add2env variable value
# _add2env variable value separator
# _add2env variable=value
# RETURNS: 0 if success, 1 otherwise
#-------------------------------------------------------------------------------
unset _add2env
function _add2env { #{{{
if [ $# -lt 1 ] || [ $# -gt 3 ]; then
printf '%s\n' 'Wrong number of arguments, must be 1, 2 or 3: [value], [variable value], [variable value separator]' >&2
return 1
fi
: ${DEBUG_DEVEL=0}
typeset value variable separator
typeset modification=post
if [[ $1 =~ = ]]; then
if [[ $1 =~ \+= ]]; then
# post-append to variable
# _add2env VARIABLE+=value
# default behaviour
modification=post
value=${1#*+=}
variable=${1%+=*}
separator=${2:-:}
elif [[ $1 =~ =\+ ]]; then
# pre-append to variable
# _add2env VARIABLE=+value
modification=pre
value=${1#*=+}
variable=${1%=+*}
separator=${2:-:}
else
# _add2env VARIABLE=value
modification=assign
value=${1#*=}
variable=${1%=*}
separator=''
fi
else
# Checking number of arguments
if [ $# -eq 1 ]; then
value=${1}
variable=PATH
separator=:
elif [ $# -eq 2 ]; then
variable=$1
value=$2
separator=:
else
variable=$1
value=$2
separator=$3
fi
fi
# echo "modification: $modification"
# echo "variable: $variable"
# echo "value: $value"
# echo "separator: $separator"
# return
# evaluate tylde
if [[ $value =~ '~' ]]; then
value=$(eval echo "$value")
fi
if (( DEBUG_DEVEL )); then
echo "modification: $modification, variable: $variable, separator $separator, value: $value" >&2
fi
typeset valueOfVariable=$(eval echo "$(printf "$%s" "$variable")")
if [ -z "$valueOfVariable" ]; then
# simple assign
if (( DEBUG_DEVEL )); then
echo eval "$variable=$value" >&2
fi
eval "$variable=${value// /\\ }"
# shellcheck disable=SC2163
export "$variable"
if (( DEBUG_DEVEL )); then
echo export "$variable"
fi
else
if [[ "$modification" = assign ]]; then
if (( DEBUG_DEVEL )); then
echo eval "$variable=$value"
fi
eval "$variable=${value// /\\ }"
else
case "$valueOfVariable" in ${value}${separator}* | *${separator}${value}${separator}* | *${separator}${value} | $value )
: echo already set
;;
*)
case $modification in
post)
if (( DEBUG_DEVEL )); then
echo eval "$variable=${valueOfVariable}${separator}${value}"
fi
eval "$variable=${valueOfVariable// /\\ }${separator// /\\ }${value}"
;;
pre)
if (( DEBUG_DEVEL )); then
echo eval "$variable=${value}${separator// /\\ }${valueOfVariable// /\\ }"
fi
eval "$variable=${value}${separator}${valueOfVariable}"
;;
esac
# shellcheck disable=SC2163
export "$variable"
if (( DEBUG_DEVEL )); then
echo export "$variable"
fi
;;
esac
fi
fi
return 0
} #}}}
#-------------------------------------------------------------------------------
# DESCRIPTION: Removes value from env variable. The first parameter must be set,
# rest is optional. If they are omitted then it'll be used PATH
# as variable and : as separator.
# It works with Bash and Ksh as well. But it does not work with Zsh yet.
# SYNOPSIS: _rm4env values
# USAGE: _rm4env value
# _rm4env variable value
# _rm4env variable variable separator
# _rm4env variable=value
# REQUIREMENTS: perl
# RETURNS: 0 if success, 1 otherwise
#-------------------------------------------------------------------------------
unset _rm4env
function _rm4env { #{{{
if [ $# -lt 1 ] || [ $# -gt 3 ]; then
printf '%s\n' 'Wrong number of arguments, must be 1, 2 or 3: [value], [variable value], [variable value separator]' >&2
return 1
fi
: ${DEBUG_DEVEL=0}
typeset value variable separator
typeset modification=remove
if [ $# -eq 1 ]; then
value=${1}
variable=PATH
separator=:
elif [ $# -eq 2 ]; then
variable=$1
value=$2
separator=:
else
variable=$1
value=$2
separator=$3
fi
# _rm4env VARIABLE=value
if [[ $# -eq 1 && $value =~ = ]]; then
modification=assign
variable=${value%=*}
value=${value#*=}
separator=${2:-:}
fi
if (( DEBUG_DEVEL )); then
echo "modification: $modification, variable: $variable, separator $separator, value: $value" >&2
fi
if [[ "$modification" == assign ]]; then
if (( DEBUG_DEVEL )); then
echo eval "$variable=$value" >&2
fi
eval "$variable=$value"
# shellcheck disable=SC2163
export "$variable"
if (( DEBUG_DEVEL )); then
echo export "$variable"
fi
else
typeset valueOfVariable=$(eval echo "$(printf "$%s" "$variable")")
case $valueOfVariable in
${value}${separator}*|*${separator}${value}${separator}*|*${separator}${value}|$value)
typeset newValue=$(echo "${valueOfVariable}" | perl -p -e "s#${separator}${value}|^${value}${separator}?##g")
if (( DEBUG_DEVEL )); then
echo eval "$variable=$newValue" >&2
fi
eval "$variable=$newValue"
# shellcheck disable=SC2163
export "$variable"
if (( DEBUG_DEVEL )); then
echo export "$variable"
fi
;;
esac
fi
} #}}}