@@ -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
@@ -24,22 +26,37 @@ func validateCmd() *cobra.Command {
2426 Example : `$ docker app validate myapp.dockerapp --set key=value --parameters-file myparam.yml` ,
2527 Args : cli .RequiresMaxArgs (1 ),
2628 RunE : func (cmd * cobra.Command , args []string ) error {
27- app , err := packager .Extract (firstOrEmpty (args ),
28- types .WithParametersFiles (opts .parametersFiles ... ),
29- )
30- if err != nil {
31- return err
32- }
33- defer app .Cleanup ()
34- argParameters := cliopts .ConvertKVStringsToMap (opts .overrides )
35- _ , err = render .Render (app , argParameters , nil )
36- if err != nil {
37- return err
38- }
39- fmt .Fprintf (os .Stdout , "Validated %q\n " , app .Path )
40- return nil
29+ return runValidate (args , opts )
4130 },
4231 }
4332 opts .parametersOptions .addFlags (cmd .Flags ())
4433 return cmd
4534}
35+
36+ func runValidate (args []string , opts validateOptions ) error {
37+ app , err := packager .Extract (firstOrEmpty (args ),
38+ types .WithParametersFiles (opts .parametersFiles ... ),
39+ )
40+ if err != nil {
41+ return err
42+ }
43+ defer app .Cleanup ()
44+ argParameters := cliopts .ConvertKVStringsToMap (opts .overrides )
45+ _ , err = render .Render (app , argParameters , nil )
46+ if err != nil {
47+ return err
48+ }
49+
50+ vars , err := compose .ExtractVariables (app .Composes ()[0 ], compose .ExtrapolationPattern )
51+ if err != nil {
52+ return errors .Wrap (err , "failed to parse compose file" )
53+ }
54+ for k := range app .Parameters ().Flatten () {
55+ if _ , ok := vars [k ]; ! ok {
56+ return fmt .Errorf ("%s is declared as parameter but not used by the compose file" , k )
57+ }
58+ }
59+
60+ fmt .Fprintf (os .Stdout , "Validated %q\n " , app .Path )
61+ return nil
62+ }
0 commit comments