@@ -4,11 +4,13 @@ import (
4
4
"fmt"
5
5
"os"
6
6
7
+ "github.com/docker/app/internal/compose"
7
8
"github.com/docker/app/internal/packager"
8
9
"github.com/docker/app/render"
9
10
"github.com/docker/app/types"
10
11
"github.com/docker/cli/cli"
11
12
cliopts "github.com/docker/cli/opts"
13
+ "github.com/pkg/errors"
12
14
"github.com/spf13/cobra"
13
15
)
14
16
@@ -23,22 +25,37 @@ func validateCmd() *cobra.Command {
23
25
Short : "Checks the rendered application is syntactically correct" ,
24
26
Args : cli .RequiresMaxArgs (1 ),
25
27
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 )
40
29
},
41
30
}
42
31
opts .parametersOptions .addFlags (cmd .Flags ())
43
32
return cmd
44
33
}
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