Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit f9f8aac

Browse files
committed
Validate parameters against the actual compose file
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 433639b commit f9f8aac

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

internal/commands/validate.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/docker/app/internal/compose"
78
"github.com/docker/app/internal/packager"
89
"github.com/docker/app/render"
910
"github.com/docker/app/types"
1011
"github.com/docker/cli/cli"
1112
cliopts "github.com/docker/cli/opts"
13+
"github.com/pkg/errors"
1214
"github.com/spf13/cobra"
1315
)
1416

@@ -23,22 +25,37 @@ func validateCmd() *cobra.Command {
2325
Short: "Checks the rendered application is syntactically correct",
2426
Args: cli.RequiresMaxArgs(1),
2527
RunE: func(cmd *cobra.Command, args []string) error {
26-
app, err := packager.Extract(firstOrEmpty(args),
27-
types.WithParametersFiles(opts.parametersFiles...),
28-
)
29-
if err != nil {
30-
return err
31-
}
32-
defer app.Cleanup()
33-
argParameters := cliopts.ConvertKVStringsToMap(opts.overrides)
34-
_, err = render.Render(app, argParameters, nil)
35-
if err != nil {
36-
return err
37-
}
38-
fmt.Fprintf(os.Stdout, "Validated %q\n", app.Path)
39-
return nil
28+
return runValidate(args, opts)
4029
},
4130
}
4231
opts.parametersOptions.addFlags(cmd.Flags())
4332
return cmd
4433
}
34+
35+
func runValidate(args []string, opts validateOptions) error {
36+
app, err := packager.Extract(firstOrEmpty(args),
37+
types.WithParametersFiles(opts.parametersFiles...),
38+
)
39+
if err != nil {
40+
return err
41+
}
42+
defer app.Cleanup()
43+
argParameters := cliopts.ConvertKVStringsToMap(opts.overrides)
44+
_, err = render.Render(app, argParameters, nil)
45+
if err != nil {
46+
return err
47+
}
48+
49+
vars, err := compose.ExtractVariables(app.Composes()[0], compose.ExtrapolationPattern)
50+
if err != nil {
51+
return errors.Wrap(err, "failed to parse compose file")
52+
}
53+
for k := range app.Parameters().Flatten() {
54+
if _, ok := vars[k]; !ok {
55+
return fmt.Errorf("%s is declared as parameter but not used by the compose file", k)
56+
}
57+
}
58+
59+
fmt.Fprintf(os.Stdout, "Validated %q\n", app.Path)
60+
return nil
61+
}

0 commit comments

Comments
 (0)