`compose config` to espace `$` signs so compose file is still valid

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-09-17 08:30:02 +02:00 committed by Nicolas De loof
parent 15cd034485
commit abb2ddba88
1 changed files with 17 additions and 2 deletions

View File

@ -17,6 +17,7 @@
package compose package compose
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "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) { func (s *composeService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
switch options.Format { switch options.Format {
case "json": case "json":
return json.MarshalIndent(project, "", " ") marshal, err := json.MarshalIndent(project, "", " ")
if err != nil {
return nil, err
}
return escapeDollarSign(marshal), nil
case "yaml": case "yaml":
return yaml.Marshal(project) marshal, err := yaml.Marshal(project)
if err != nil {
return nil, err
}
return escapeDollarSign(marshal), nil
default: default:
return nil, fmt.Errorf("unsupported format %q", options) return nil, fmt.Errorf("unsupported format %q", options)
} }
} }
func escapeDollarSign(marshal []byte) []byte {
dollar := []byte{'$'}
escDollar := []byte{'$', '$'}
return bytes.ReplaceAll(marshal, dollar, escDollar)
}