docker / libcompose

*Unmaintained/Deprecated* An experimental go library providing Compose-like functionality
https://godoc.org/github.com/docker/libcompose
Apache License 2.0
584 stars 191 forks source link

Make defaults work with nested env variables ie: ${FOO:-${HOME}/bar} #496

Open vito-c opened 7 years ago

vito-c commented 7 years ago

Signed-off-by: GodFather vito.blog@gmail.com

jwreagor commented 7 years ago

👍 It would be great to get this in since I've seen this fail on some of my compose files.

vito-c commented 7 years ago

@vdemeester how's it looking on this one? :)

vito-c commented 7 years ago

It feels like they actually have a bug in their code. The default string is not interpolated correctly. If you try something like this echo ${ABC:-${HOME}} you'll notice it will output your homedir.

---
version: '3'
services:
    test.env:
        container_name: test.env
        image: alpine
        environment:
            FOO: ${ABC:-foo}
        command: env

output:

Recreating test.env ...
Recreating test.env ... done
Attaching to test.env
test.env    | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test.env    | HOSTNAME=0e6eab0bcf66
test.env    | FOO=foo
test.env    | no_proxy=*.local, 169.254/16
test.env    | HOME=/root
test.env exited with code 0

and

---
version: '3'
services:
    test.env:
        container_name: test.env
        image: alpine
        environment:
            FOO: ${ABC:-${HOME}}
        command: env

output:

Recreating test.env ...
Recreating test.env ... done
Attaching to test.env
test.env    | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test.env    | HOSTNAME=6fa2fe19685b
test.env    | FOO=${HOME}
test.env    | no_proxy=*.local, 169.254/16
test.env    | HOME=/root
test.env exited with code 0
vdemeester commented 7 years ago

Yeah, that's why I would like to make this behavior optional (so ppl can opt-in or opt-out of the behavior to get the same as docker-compose.)

vito-c commented 7 years ago

sweet how would I go about adding the optional flag?

vdemeester commented 7 years ago

@vito-c I'm not sure yet. I would probably make Interpolate configurable (https://github.com/vito-c/libcompose/blob/628498254cf9da3ab00dad397b64a17d11d65140/config/interpolation.go#L184) that would be disabled by default (on libcompose binary).