Skip to content

Commit bf51fd2

Browse files
committed
Expose target cat-env
1 parent 96396ee commit bf51fd2

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

Targets.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,20 @@ doco-target::jq-name() {
167167
REPLY=("${1//::::/::}")
168168
}
169169

170-
__get-compose-env() {
171-
CLEAR_FILTERS
172-
APPLY '.services[$svc].environment // {} | to_entries' svc="$1"
170+
doco-target::cat-env() {
171+
this is-service || fail "$TARGET_NAME is not a service" || return
172+
compose-config || return; CLEAR_FILTERS
173+
APPLY '.services[$svc].environment // {} | to_entries' svc="$TARGET_NAME"
173174
FILTER 'map( "\(.key)=\(.value / "$$" | map (. + "$") | add | .[:-1] | @sh)\n" )'
174175
FILTER '.+[""] | add | .[:-1]'
175-
RUN_JQ -r -j <<<"$COMPOSED_JSON"
176+
RUN_JQ -r <<<"$COMPOSED_JSON"
176177
}
177178

178179
doco-target::get-env() {
179-
this is-service || fail "$TARGET_NAME is not a service" || return
180180
# Fetch parsed env values from docker-compose config
181181
local name=${TARGET_VAR}__env; local -n vals=$name
182182
if ! array-exists "$name"; then
183-
compose-config && eval "vals=($(__get-compose-env "$TARGET_NAME"))" || return
183+
REPLY=$(this cat-env) || return; eval "vals=($REPLY)"
184184
fi
185185
REPLY=("${vals[@]}")
186186
}

bin/doco

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,20 +700,20 @@ doco-target::jq-name() {
700700
REPLY=("${1//::::/::}")
701701
}
702702
703-
__get-compose-env() {
704-
CLEAR_FILTERS
705-
APPLY '.services[$svc].environment // {} | to_entries' svc="$1"
703+
doco-target::cat-env() {
704+
this is-service || fail "$TARGET_NAME is not a service" || return
705+
compose-config || return; CLEAR_FILTERS
706+
APPLY '.services[$svc].environment // {} | to_entries' svc="$TARGET_NAME"
706707
FILTER 'map( "\(.key)=\(.value / "$$" | map (. + "$") | add | .[:-1] | @sh)\n" )'
707708
FILTER '.+[""] | add | .[:-1]'
708-
RUN_JQ -r -j <<<"$COMPOSED_JSON"
709+
RUN_JQ -r <<<"$COMPOSED_JSON"
709710
}
710711
711712
doco-target::get-env() {
712-
this is-service || fail "$TARGET_NAME is not a service" || return
713713
# Fetch parsed env values from docker-compose config
714714
local name=${TARGET_VAR}__env; local -n vals=$name
715715
if ! array-exists "$name"; then
716-
compose-config && eval "vals=($(__get-compose-env "$TARGET_NAME"))" || return
716+
REPLY=$(this cat-env) || return; eval "vals=($REPLY)"
717717
fi
718718
REPLY=("${vals[@]}")
719719
}

specs/Targets.cram.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Groups can be made read-only; services are always read-only. Any attempt to cha
194194

195195
### Target Environments
196196

197-
Service targets can fetch their environments as an array of `key=val` strings using `get-env`, or run a command with local variables set from them (using `with-env`). docker-compose's escaping of `$` is handled correctly, as are multi-line values.
197+
Service targets can fetch their environments as an array of `key=val` strings using `get-env`, or run a command with local variables set from them (using `with-env`). docker-compose's escaping of `$` is handled correctly, as are multi-line values. (You can also dump them to stdout with `cat-env`.)
198198

199199
~~~sh
200200
# Only services can get-env
@@ -210,7 +210,7 @@ Service targets can fetch their environments as an array of `key=val` strings us
210210
# Environment is loaded from compose-config, so let's mock that and
211211
# pretend we already have the configuration
212212

213-
$ compose-config() { echo "calling compose-config"; }
213+
$ compose-config() { echo "calling compose-config" >&2; }
214214
$ COMPOSED_JSON='{"services": {"svc2": {"environment": { "X": "y$$z", "Q": "r\ns" }}}}'
215215

216216
# aService has no environment, so we get nothing.
@@ -223,6 +223,15 @@ Service targets can fetch their environments as an array of `key=val` strings us
223223

224224
# svc2 has two variables, one with `$$` and the other with a linefeed
225225

226+
$ target svc2 cat-env
227+
calling compose-config
228+
X='y$z'
229+
Q='r
230+
s'
231+
232+
# But they are correctly parsed into k=v strings by get-env:
233+
# (cat-env doesn't cache, so compose-config will be called again)
234+
226235
$ target svc2 get-env; printf '%q\n' "${REPLY[@]}"
227236
calling compose-config
228237
X=y\$z

0 commit comments

Comments
 (0)