From 7a8d157871aa0095d73aefb542a1bda2a58a3efe Mon Sep 17 00:00:00 2001 From: Lucas Berg <55436804+BergLucas@users.noreply.github.com> Date: Thu, 8 Sep 2022 22:25:23 +0200 Subject: [PATCH] convert: do not escape $ into $$ when using the --no-interpolate option (#9703) Signed-off-by: Lucas Berg Signed-off-by: Ulysses Souza Co-authored-by: Ulysses Souza --- cmd/compose/convert.go | 19 ++++++++++++---- pkg/compose/compose.go | 19 ++-------------- pkg/e2e/compose_test.go | 22 +++++++++++++++++++ .../compose-interpolate.yaml | 5 +++++ 4 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 pkg/e2e/fixtures/simple-build-test/compose-interpolate.yaml diff --git a/cmd/compose/convert.go b/cmd/compose/convert.go index 79692620e..b1adecc98 100644 --- a/cmd/compose/convert.go +++ b/cmd/compose/convert.go @@ -18,6 +18,7 @@ package compose import ( "bufio" + "bytes" "context" "fmt" "io" @@ -112,7 +113,7 @@ func convertCommand(p *projectOptions, backend api.Service) *cobra.Command { } func runConvert(ctx context.Context, backend api.Service, opts convertOptions, services []string) error { - var json []byte + var content []byte project, err := opts.toProject(services, cli.WithInterpolation(!opts.noInterpolate), cli.WithResolvedPaths(true), @@ -136,7 +137,7 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s } } - json, err = backend.Convert(ctx, project, api.ConvertOptions{ + content, err = backend.Convert(ctx, project, api.ConvertOptions{ Format: opts.Format, Output: opts.Output, }) @@ -144,19 +145,23 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s return err } + if !opts.noInterpolate { + content = escapeDollarSign(content) + } + if opts.quiet { return nil } var out io.Writer = os.Stdout - if opts.Output != "" && len(json) > 0 { + if opts.Output != "" && len(content) > 0 { file, err := os.Create(opts.Output) if err != nil { return err } out = bufio.NewWriter(file) } - _, err = fmt.Fprint(out, string(json)) + _, err = fmt.Fprint(out, string(content)) return err } @@ -237,3 +242,9 @@ func runConfigImages(opts convertOptions, services []string) error { } return nil } + +func escapeDollarSign(marshal []byte) []byte { + dollar := []byte{'$'} + escDollar := []byte{'$', '$'} + return bytes.ReplaceAll(marshal, dollar, escDollar) +} diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 603e7057a..ef5d58780 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -17,7 +17,6 @@ package compose import ( - "bytes" "context" "encoding/json" "fmt" @@ -95,28 +94,14 @@ func getContainerNameWithoutProject(c moby.Container) string { func (s *composeService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) { switch options.Format { case "json": - marshal, err := json.MarshalIndent(project, "", " ") - if err != nil { - return nil, err - } - return escapeDollarSign(marshal), nil + return json.MarshalIndent(project, "", " ") case "yaml": - marshal, err := yaml.Marshal(project) - if err != nil { - return nil, err - } - return escapeDollarSign(marshal), nil + return yaml.Marshal(project) default: return nil, fmt.Errorf("unsupported format %q", options) } } -func escapeDollarSign(marshal []byte) []byte { - dollar := []byte{'$'} - escDollar := []byte{'$', '$'} - return bytes.ReplaceAll(marshal, dollar, escDollar) -} - // projectFromName builds a types.Project based on actual resources with compose labels set func (s *composeService) projectFromName(containers Containers, projectName string, services ...string) (*types.Project, error) { project := &types.Project{ diff --git a/pkg/e2e/compose_test.go b/pkg/e2e/compose_test.go index 668bb7747..de5d3c029 100644 --- a/pkg/e2e/compose_test.go +++ b/pkg/e2e/compose_test.go @@ -234,3 +234,25 @@ networks: name: compose-e2e-convert_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0}) }) } + +func TestConvertInterpolate(t *testing.T) { + const projectName = "compose-e2e-convert-interpolate" + c := NewParallelCLI(t) + + wd, err := os.Getwd() + assert.NilError(t, err) + + t.Run("convert", func(t *testing.T) { + res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose-interpolate.yaml", "-p", projectName, "convert", "--no-interpolate") + res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services: + nginx: + build: + context: %s + dockerfile: ${MYVAR} + networks: + default: null +networks: + default: + name: compose-e2e-convert-interpolate_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0}) + }) +} diff --git a/pkg/e2e/fixtures/simple-build-test/compose-interpolate.yaml b/pkg/e2e/fixtures/simple-build-test/compose-interpolate.yaml new file mode 100644 index 000000000..57d092a99 --- /dev/null +++ b/pkg/e2e/fixtures/simple-build-test/compose-interpolate.yaml @@ -0,0 +1,5 @@ +services: + nginx: + build: + context: nginx-build + dockerfile: ${MYVAR}