Skip to content

Commit 0c481dc

Browse files
committed
add support for raw env_file format
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 39d0f64 commit 0c481dc

7 files changed

Lines changed: 67 additions & 6 deletions

File tree

cmd/compose/compose.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24+
"io"
2425
"os"
2526
"os/signal"
2627
"path/filepath"
@@ -37,6 +38,7 @@ import (
3738
dockercli "github.com/docker/cli/cli"
3839
"github.com/docker/cli/cli-plugins/manager"
3940
"github.com/docker/cli/cli/command"
41+
"github.com/docker/cli/pkg/kvfile"
4042
"github.com/docker/compose/v2/cmd/formatter"
4143
"github.com/docker/compose/v2/internal/desktop"
4244
"github.com/docker/compose/v2/internal/experimental"
@@ -70,6 +72,26 @@ const (
7072
ComposeMenu = "COMPOSE_MENU"
7173
)
7274

75+
// rawEnv load a dot env file using docker/cli key=value parser, without attempt to interpolate or evaluate values
76+
func rawEnv(r io.Reader, filename string, lookup func(key string) (string, bool)) (map[string]string, error) {
77+
lines, err := kvfile.ParseFromReader(r, lookup)
78+
if err != nil {
79+
return nil, fmt.Errorf("failed to parse env_file %s: %w", filename, err)
80+
}
81+
vars := types.Mapping{}
82+
for _, line := range lines {
83+
key, value, _ := strings.Cut(line, "=")
84+
vars[key] = value
85+
}
86+
return vars, nil
87+
}
88+
89+
func init() {
90+
// compose evaluates env file values for interpolation
91+
// `raw` format allows to load env_file with the same parser used by docker run --env-file
92+
dotenv.RegisterFormat("raw", rawEnv)
93+
}
94+
7395
type Backend interface {
7496
api.Service
7597

cmd/formatter/colors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"strconv"
2222
"sync"
2323

24-
"github.com/docker/compose/v2/pkg/api"
24+
"github.com/docker/cli/cli/command"
2525
)
2626

2727
var names = []string{
@@ -59,7 +59,7 @@ const (
5959
)
6060

6161
// SetANSIMode configure formatter for colored output on ANSI-compliant console
62-
func SetANSIMode(streams api.Streams, ansi string) {
62+
func SetANSIMode(streams command.Streams, ansi string) {
6363
if !useAnsi(streams, ansi) {
6464
nextColor = func() colorFunc {
6565
return monochrome
@@ -68,7 +68,7 @@ func SetANSIMode(streams api.Streams, ansi string) {
6868
}
6969
}
7070

71-
func useAnsi(streams api.Streams, ansi string) bool {
71+
func useAnsi(streams command.Streams, ansi string) bool {
7272
switch ansi {
7373
case Always:
7474
return true

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/davecgh/go-spew v1.1.1
1414
github.com/distribution/reference v0.6.0
1515
github.com/docker/buildx v0.17.1
16-
github.com/docker/cli v27.3.1+incompatible
16+
github.com/docker/cli v27.3.2-0.20241008150905-cb3048fbebb1+incompatible
1717
github.com/docker/cli-docs-tool v0.8.0
1818
github.com/docker/docker v27.3.1+incompatible
1919
github.com/docker/go-connections v0.5.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5Qvfr
128128
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
129129
github.com/docker/buildx v0.17.1 h1:9ob2jGp4+W9PxWw68GsoNFp+eYFc7eUoRL9VljLCSM4=
130130
github.com/docker/buildx v0.17.1/go.mod h1:kJOhOhS47LRvrLFRulFiO5SE6VJf54yYMn7DzjgO5W0=
131-
github.com/docker/cli v27.3.1+incompatible h1:qEGdFBF3Xu6SCvCYhc7CzaQTlBmqDuzxPDpigSyeKQQ=
132-
github.com/docker/cli v27.3.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
131+
github.com/docker/cli v27.3.2-0.20241008150905-cb3048fbebb1+incompatible h1:fJ3SzYiebfWoas3qOJpmRsNrDL2w1XIpPFywSLFhhfk=
132+
github.com/docker/cli v27.3.2-0.20241008150905-cb3048fbebb1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
133133
github.com/docker/cli-docs-tool v0.8.0 h1:YcDWl7rQJC3lJ7WVZRwSs3bc9nka97QLWfyJQli8yJU=
134134
github.com/docker/cli-docs-tool v0.8.0/go.mod h1:8TQQ3E7mOXoYUs811LiPdUnAhXrcVsBIrW21a5pUbdk=
135135
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=

pkg/e2e/env_file_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2e
18+
19+
import (
20+
"strings"
21+
"testing"
22+
23+
"gotest.tools/v3/assert"
24+
)
25+
26+
func TestRawEnvFile(t *testing.T) {
27+
c := NewParallelCLI(t)
28+
29+
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dotenv/raw.yaml", "run", "test")
30+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "'{\"key\": \"value\"}'")
31+
}

pkg/e2e/fixtures/dotenv/.env.raw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TEST_VAR='{"key": "value"}'

pkg/e2e/fixtures/dotenv/raw.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
test:
3+
image: alpine
4+
command: sh -c "echo $$TEST_VAR"
5+
env_file:
6+
- path: .env.raw
7+
format: raw # parse without interpolation

0 commit comments

Comments
 (0)