From abb2ddba88c5668a865c39bb04788f1284272c1e Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Fri, 17 Sep 2021 08:30:02 +0200 Subject: [PATCH] `compose config` to espace `$` signs so compose file is still valid Signed-off-by: Nicolas De Loof --- pkg/compose/compose.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index daa383844..0cab4f363 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -17,6 +17,7 @@ package compose import ( + "bytes" "context" "encoding/json" "fmt" @@ -67,10 +68,24 @@ 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": - return json.MarshalIndent(project, "", " ") + marshal, err := json.MarshalIndent(project, "", " ") + if err != nil { + return nil, err + } + return escapeDollarSign(marshal), nil case "yaml": - return yaml.Marshal(project) + marshal, err := yaml.Marshal(project) + if err != nil { + return nil, err + } + return escapeDollarSign(marshal), nil default: return nil, fmt.Errorf("unsupported format %q", options) } } + +func escapeDollarSign(marshal []byte) []byte { + dollar := []byte{'$'} + escDollar := []byte{'$', '$'} + return bytes.ReplaceAll(marshal, dollar, escDollar) +}