-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.sh
executable file
·218 lines (178 loc) · 4.86 KB
/
env.sh
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
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
if ! type -f bpkg-package &>/dev/null; then
echo "error: bpkg-package not found, aborting"
exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/package/package.sh
source "$(which bpkg-package)"
fi
if ! type -f bpkg-realpath &>/dev/null; then
echo "error: bpkg-realpath not found, aborting"
exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/realpath/realpath.sh
source "$(which bpkg-realpath)"
fi
# List of bash source files
declare -a BPKG_PACKAGE_SOURCES=()
# Include config rc file if found
declare BPKG_USER_CONFIG_FILE="$HOME/.bpkgrc"
# Include config rc file if found
declare BPKG_LOCAL_CONFIG_FILE="$(pwd)/.bpkgrc"
## meta
export BPKG_DATE="$(date)"
export BPKG_HOME="${BPKG_HOME:-$HOME}"
export BPKG_INDEX
export BPKG_FORCE_ACTIONS
## os
export BPKG_OS="$(uname)"
export BPKG_CWD="$(pwd)"
export BPKG_BIN="${BPKG_BIN:-$(which bpkg)}"
export BPKG_USER="${BPKG_USER:-$USER}"
## git
export BPKG_REMOTES
export BPKG_GIT_REMOTE
export BPKG_GIT_REMOTES
## package
export BPKG_PACKAGE_USER
export BPKG_PACKAGE_NAME="$(bpkg_package name 2>/dev/null)"
export BPKG_PACKAGE_REPO="$(bpkg_package repo 2>/dev/null)"
export BPKG_PACKAGE_PATH="$(bpkg_package_path)"
export BPKG_PACKAGE_ROOT="$(bpkg_realpath "$(dirname "$BPKG_PACKAGE_PATH")")"
export BPKG_PACKAGE_DEPS="$BPKG_PACKAGE_ROOT/deps"
export BPKG_PACKAGE_VERSION="$(bpkg_package version 2>/dev/null)"
export BPKG_PACKAGE_DESCRIPTION="$(bpkg_package description 2>/dev/null)"
export BPKG_PACKAGE_DEFAULT_USER="${BPKG_PACKAGE_DEFAULT_USER:-bpkg}"
## remote
# shellcheck disable=SC2178
export BPKG_REMOTE
export BPKG_REMOTES
export BPKG_REMOTE_RAW_PATH
if test -f "$BPKG_PACKAGE_PATH"; then
BPKG_PACKAGE_SOURCES+=($(find "$BPKG_PACKAGE_ROOT" -name '*.sh' 2>/dev/null))
BPKG_PACKAGE_SOURCES+=($(find "$BPKG_PACKAGE_ROOT" -name '*.zsh' 2>/dev/null))
BPKG_PACKAGE_SOURCES+=($(find "$BPKG_PACKAGE_ROOT" -name '*.bash' 2>/dev/null))
export BPKG_PACKAGE_SOURCES
fi
if test -d "$BPKG_PACKAGE_DEPS/bin"; then
export PATH="$BPKG_PACKAGE_DEPS/bin:$PATH"
fi
## output usage
usage () {
cat <<USAGE
usage: bpkg-env [-h|--help]
or: bpkg-env <key|pattern> [--value]
example:
$ bpkg-env BPKG_PACKAGE*
BPKG_PACKAGE_DEPS="deps"
BPKG_PACKAGE_NAME="bpkg"
BPKG_PACKAGE_DESCRIPTION="Lightweight bash package manager"
BPKG_PACKAGE_REPO="bpkg/bpkg"
BPKG_PACKAGE_USER="bpkg"
BPKG_PACKAGE_VERSION="1.1.0"
USAGE
}
bpkg_env () {
local key=""
local should_emit_value=0
for opt in "$@"; do
case "$opt" in
-h|--help)
usage
return 0
;;
--value)
should_emit_value=1
shift
;;
-*)
bpkg_error "Unknown option: \`$opt'"
return 2
;;
*)
key="$1"
shift
;;
esac
done
{
printenv
echo BPKG_PACKAGE_SOURCES=\""${BPKG_PACKAGE_SOURCES[*]}"\"
} | \
grep '^\s*BPKG_' | \
sed 's/^\s*//g' | \
sort | \
{
local did_emit=0
while read -r line; do
local kv=("" "")
local j=0
for (( i = 0; i < ${#line}; ++i )); do
if [ "${line:$i:1}" = "=" ] && (( j == 0 )); then
(( j++ ))
continue
fi
kv[$j]+=${line:$i:1}
done
if [ -n "$key" ]; then
regex="^${key/'*'/'.*'}$"
if ! [[ "${kv[0]}" =~ $regex ]]; then
continue
fi
fi
if [ -z "${kv[1]}" ]; then
continue
fi
if [ "${kv[1]:0:1}" = "$" ] || [ "${kv[1]:1:1}" = "$" ]; then
continue
fi
if [ "${kv[1]:0:1}" = ";" ]; then
continue
fi
if [ "${kv[1]}" = '"";' ]; then
continue
fi
if (( should_emit_value == 0 )); then
printf '%s=' "${kv[0]}"
fi
if [ "${kv[1]:0:1}" != '"' ]; then
printf '"'
fi
printf '%s' "${kv[1]}"
local l=${#kv[1]}
local lm1=$(( l - 1 ))
local lm2=$(( l - 2 ))
if [ "${kv[1]:$lm1:1}" != '"' ] && [ "${kv[1]:$lm2:1}" != '"' ] && [ "${kv[1]:$lm1:1}" != ';' ] ; then
printf '"'
fi
# shellcheck disable=SC2030,SC2031
did_emit=1
echo
done
if (( did_emit == 1 )); then
return 0
fi
return 1
}
return $?
}
if [[ -f "$BPKG_USER_CONFIG_FILE" ]] && [ -z "$BPKG_USER_CONFIG_FILE_LOADED" ]; then
export BPKG_USER_CONFIG_FILE
export BPKG_USER_CONFIG_FILE_LOADED="1"
# shellcheck disable=SC1090
source "$BPKG_USER_CONFIG_FILE_LOADED"
fi
if [[ -f "$BPKG_LOCAL_CONFIG_FILE" ]] && [ -z "$BPKG_LOCAL_CONFIG_FILE_LOADED" ]; then
export BPKG_LOCAL_CONFIG_FILE
export BPKG_LOCAL_CONFIG_FILE_LOADED="1"
# shellcheck disable=SC1090
source "$BPKG_LOCAL_CONFIG_FILE"
fi
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f bpkg_env
else
bpkg_env "$@"
exit $?
fi