2222# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323# SOFTWARE.
2424
25- VERSION=" 0.9.1 "
25+ VERSION=" 0.9.2a "
2626USAGE=" bash-env-json [--help] [--shellfns <comma-separated-function-names>] [path]"
2727
2828shopt -s extglob
2929
30+ # external dependencies are ideally invoked via full paths,
31+ # so make it straightforward for the Nix flake to substitute for these
32+ function _env() { env " $@ " ; }
33+ function _jq() { jq " $@ " ; }
34+ function _mktemp() { mktemp " $@ " ; }
35+ function _rm() { rm " $@ " ; }
36+ function _sed() { sed " $@ " ; }
37+ function _touch() { touch " $@ " ; }
38+
3039function capture() {
3140 local -n _capture_env=" $1 "
3241 local -n _capture_shellvars=" $2 "
@@ -37,12 +46,12 @@ function capture() {
3746 # environment variables
3847 while IFS=' =' read -r -d ' ' _name _value; do
3948 _capture_env[" $_name " ]=" ${_value} "
40- done < <( env -0)
49+ done < <( _env -0)
4150
4251 # shellvars
4352 for _name in $(
4453 set -o posix
45- set | sed -n -e ' /^[a-zA-Z_][a-zA-Z_0-9]*=/s/=.*$//p'
54+ set | _sed -n -e ' /^[a-zA-Z_][a-zA-Z_0-9]*=/s/=.*$//p'
4655 set +o posix
4756 ) ; do
4857 if test -v " $_name " -a ! " ${_capture_env[$_name]+EXISTS} " -a ! " ${_inhibit_shellvars[$_name]+EXISTS} " ; then
@@ -52,9 +61,9 @@ function capture() {
5261}
5362
5463function emit_value() {
55- # jq -R produces nothing on empty input, but we want ""
64+ # ` jq -R` produces nothing on empty input, but we want ""
5665 if test -n " $1 " ; then
57- echo -n " $1 " | jq -R
66+ echo -n " $1 " | _jq -R
5867 else
5968 echo -n ' ""'
6069 fi
@@ -82,17 +91,17 @@ function emit_error() {
8291}
8392
8493function emit_error_exit() {
85- emit_error " $@ " | jq
94+ emit_error " $@ " | _jq
8695 exit 1
8796}
8897
8998function emit_help_exit() {
90- emit_meta ' {' ' }' usage | jq
99+ emit_meta ' {' ' }' usage | _jq
91100 exit 0
92101}
93102
94103function emit_version_exit() {
95- emit_meta ' {' ' }' | jq
104+ emit_meta ' {' ' }' | _jq
96105 exit 0
97106}
98107
@@ -136,11 +145,11 @@ function eval_or_source() {
136145 local _source _path _error_file
137146 _path=" $1 "
138147
139- _error_file=$( mktemp -u)
140- touch " $_error_file "
148+ _error_file=$( _mktemp -u)
149+ _touch " $_error_file "
141150 # shellcheck disable=SC2094
142151 exec 3< " $_error_file " 4> " $_error_file "
143- rm -f " $_error_file "
152+ _rm -f " $_error_file "
144153
145154 if test -n " $_path " ; then
146155 # source from file if specified
@@ -161,19 +170,19 @@ function eval_or_source() {
161170 if ! eval " $_source " > /dev/null 2>&4 ; then
162171 exec 4>& -
163172 # discard error location, because it is this file not the one sourced
164- emit_error_exit " $( sed -e ' s/^.*line\s*[0-9]*:\s*//' < & 3) "
173+ emit_error_exit " $( _sed -e ' s/^.*line\s*[0-9]*:\s*//' < & 3) "
165174 fi
166175 fi
167176}
168177
169178function invoke_safely() {
170179 local _fn=" $1 "
171180
172- _error_file=$( mktemp -u)
173- touch " $_error_file "
181+ _error_file=$( _mktemp -u)
182+ _touch " $_error_file "
174183 # shellcheck disable=SC2094
175184 exec 3< " $_error_file " 4> " $_error_file "
176- rm -f " $_error_file "
185+ _rm -f " $_error_file "
177186
178187 " $_fn " > /dev/null 2>&4 || {
179188 exec 4>& -
@@ -233,14 +242,14 @@ function main() {
233242 capture _env_current _shellvars_current
234243
235244 # accumulate result in a file until we know we are error-free
236- _result_file=$( mktemp -u)
237- touch " $_result_file "
245+ _result_file=$( _mktemp -u)
246+ _touch " $_result_file "
238247 # shellcheck disable=SC2094
239248 exec 5< " $_result_file " 6> " $_result_file "
240- rm -f " $_result_file "
249+ _rm -f " $_result_file "
241250
242- emit " {" env _env_previous _env_current >&6
243- emit " ," shellvars _shellvars_previous _shellvars_current >&6
251+ emit " {" " env" _env_previous _env_current >&6
252+ emit " ," " shellvars" _shellvars_previous _shellvars_current >&6
244253
245254 test " ${# _shellfn_names[@]} " -gt 0 && {
246255 echo " ,\" fn\" :{" >&6
@@ -253,8 +262,8 @@ function main() {
253262 capture _env_current _shellvars_current
254263
255264 echo " $_fn_comma \" $_fn \" :" >&6
256- emit " {" env _env_previous _env_current >&6
257- emit " ," shellvars _shellvars_previous _shellvars_current >&6
265+ emit " {" " env" _env_previous _env_current >&6
266+ emit " ," " shellvars" _shellvars_previous _shellvars_current >&6
258267 echo " }" >&6
259268 _fn_comma=" ,"
260269 done
@@ -266,7 +275,7 @@ function main() {
266275 emit_meta ' ,' ' }' >&6
267276 exec 6>& -
268277
269- jq < & 5
278+ _jq < & 5
270279}
271280
272281function bad_usage() {
0 commit comments