Skip to content

Commit

Permalink
fix(generate): correct parsing of --build json (#131)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza authored May 7, 2024
1 parent da81771 commit 239e278
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Examples:
score-compose generate score.yaml --override-file=./overrides.score.yaml --override-property=metadata.key=value
Flags:
--build stringArray An optional build context to use for the given container --build=container=./dir or --build=container={'"context":"./dir"}
--build stringArray An optional build context to use for the given container --build=container=./dir or --build=container={"context":"./dir"}
--env-file string Location to store a skeleton .env file for compose - this will override existing content
-h, --help help for generate
--image string An optional container image to use for any container with image == '.'
Expand Down
12 changes: 7 additions & 5 deletions internal/command/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ package command

import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"slices"
"strings"

composeloader "github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/types"
"github.com/imdario/mergo"
"github.com/score-spec/score-go/framework"
Expand Down Expand Up @@ -158,10 +158,12 @@ arguments.
return fmt.Errorf("invalid --%s '%s': unknown container '%s'", generateCmdBuildFlag, buildFlag, parts[0])
}
if strings.HasPrefix(parts[1], "{") {
var intermediate interface{}
if err := yaml.Unmarshal([]byte(parts[1]), &intermediate); err != nil {
return fmt.Errorf("invalid --%s '%s': %w", generateCmdBuildFlag, buildFlag, err)
}
var out types.BuildConfig
dec := json.NewDecoder(strings.NewReader(parts[1]))
dec.DisallowUnknownFields()
if err := dec.Decode(&out); err != nil {
if err := composeloader.Transform(intermediate, &out); err != nil {
return fmt.Errorf("invalid --%s '%s': %w", generateCmdBuildFlag, buildFlag, err)
}
containerBuildContexts[parts[0]] = out
Expand Down Expand Up @@ -327,7 +329,7 @@ func init() {
generateCommand.Flags().String(generateCmdOverridesFileFlag, "", "An optional file of Score overrides to merge in")
generateCommand.Flags().StringArray(generateCmdOverridePropertyFlag, []string{}, "An optional set of path=key overrides to set or remove")
generateCommand.Flags().String(generateCmdImageFlag, "", "An optional container image to use for any container with image == '.'")
generateCommand.Flags().StringArray(generateCmdBuildFlag, []string{}, "An optional build context to use for the given container --build=container=./dir or --build=container={'\"context\":\"./dir\"}")
generateCommand.Flags().StringArray(generateCmdBuildFlag, []string{}, "An optional build context to use for the given container --build=container=./dir or --build=container={\"context\":\"./dir\"}")
generateCommand.Flags().String(generateCmdEnvFileFlag, "", "Location to store a skeleton .env file for compose - this will override existing content")
rootCmd.AddCommand(generateCommand)
}
Expand Down
40 changes: 39 additions & 1 deletion internal/command/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Examples:
score-compose generate score.yaml --override-file=./overrides.score.yaml --override-property=metadata.key=value
Flags:
--build stringArray An optional build context to use for the given container --build=container=./dir or --build=container={'"context":"./dir"}
--build stringArray An optional build context to use for the given container --build=container=./dir or --build=container={"context":"./dir"}
--env-file string Location to store a skeleton .env file for compose - this will override existing content
-h, --help help for generate
--image string An optional container image to use for any container with image == '.'
Expand Down Expand Up @@ -270,6 +270,44 @@ services:
assert.Equal(t, expectedOutput, string(raw))
})

t.Run("generate with json build context and array args", func(t *testing.T) {
stdout, _, err = executeAndResetCommand(context.Background(), rootCmd, []string{
"generate", "-o", "compose-output.yaml", "--build", `example={"context":"./dir","args":["DEBUG"]}`, "--", "score.yaml",
})
assert.NoError(t, err)
assert.Equal(t, "", stdout)
raw, err := os.ReadFile(filepath.Join(td, "compose-output.yaml"))
assert.NoError(t, err)
expectedOutput := `name: "001"
services:
example-example:
build:
context: ./dir
args:
DEBUG: null
hostname: example
`
assert.Equal(t, expectedOutput, string(raw))
})

t.Run("generate with yaml build context", func(t *testing.T) {
stdout, _, err = executeAndResetCommand(context.Background(), rootCmd, []string{
"generate", "-o", "compose-output.yaml", "--build", `example={context: "./dir"}`, "--", "score.yaml",
})
assert.NoError(t, err)
assert.Equal(t, "", stdout)
raw, err := os.ReadFile(filepath.Join(td, "compose-output.yaml"))
assert.NoError(t, err)
expectedOutput := `name: "001"
services:
example-example:
build:
context: ./dir
hostname: example
`
assert.Equal(t, expectedOutput, string(raw))
})

}

func TestInitAndGenerate_with_files(t *testing.T) {
Expand Down

0 comments on commit 239e278

Please sign in to comment.