@@ -14,6 +14,7 @@ import (
1414
1515 "github.com/docker/app/internal"
1616 "github.com/docker/app/internal/compose"
17+ "github.com/docker/app/internal/validator"
1718 "github.com/docker/app/internal/yaml"
1819 "github.com/docker/app/types"
1920 "github.com/docker/app/types/metadata"
@@ -49,6 +50,11 @@ func Init(errWriter io.Writer, name string, composeFile string) (string, error)
4950 if composeFile == "" {
5051 err = initFromScratch (name )
5152 } else {
53+ v := validator .NewValidatorWithDefaults ()
54+ err = v .Validate (composeFile )
55+ if err != nil {
56+ return "" , err
57+ }
5258 err = initFromComposeFile (errWriter , name , composeFile )
5359 }
5460 if err != nil {
@@ -82,11 +88,11 @@ func checkComposeFileVersion(compose map[string]interface{}) error {
8288
8389func getEnvFiles (svcName string , envFileEntry interface {}) ([]string , error ) {
8490 var envFiles []string
85- switch envFileEntry .(type ) {
91+ switch envFileEntry := envFileEntry .(type ) {
8692 case string :
87- envFiles = append (envFiles , envFileEntry .( string ) )
93+ envFiles = append (envFiles , envFileEntry )
8894 case []interface {}:
89- for _ , env := range envFileEntry .([] interface {}) {
95+ for _ , env := range envFileEntry {
9096 envFiles = append (envFiles , env .(string ))
9197 }
9298 default :
@@ -125,50 +131,6 @@ func checkEnvFiles(errWriter io.Writer, appName string, cfgMap map[string]interf
125131 return nil
126132}
127133
128- func checkRelativePaths (cfgMap map [string ]interface {}) error {
129- services := cfgMap ["services" ]
130- servicesMap , ok := services .(map [string ]interface {})
131- if ! ok {
132- return fmt .Errorf ("invalid Compose file" )
133- }
134- for svcName , svc := range servicesMap {
135- svcContent , ok := svc .(map [string ]interface {})
136- if ! ok {
137- return fmt .Errorf ("invalid service %q" , svcName )
138- }
139- v , ok := svcContent ["volumes" ]
140- if ! ok {
141- continue
142- }
143- volumes , ok := v .([]interface {})
144- if ! ok {
145- return fmt .Errorf ("invalid Compose file" )
146- }
147- for _ , volume := range volumes {
148- switch volume .(type ) {
149- case string :
150- svol := volume .(string )
151- source := strings .TrimRight (svol , ":" )
152- if ! filepath .IsAbs (source ) {
153- return fmt .Errorf ("invalid service %q: can't use relative path as volume source" , svcName )
154- }
155- case map [string ]interface {}:
156- lvol := volume .(map [string ]interface {})
157- src , ok := lvol ["source" ]
158- if ! ok {
159- return fmt .Errorf ("invalid volume in service %q" , svcName )
160- }
161- if ! filepath .IsAbs (src .(string )) {
162- return fmt .Errorf ("invalid service %q: can't use relative path as volume source" , svcName )
163- }
164- default :
165- return fmt .Errorf ("invalid Compose file" )
166- }
167- }
168- }
169- return nil
170- }
171-
172134func getParamsFromDefaultEnvFile (composeFile string , composeRaw []byte ) (map [string ]string , bool , error ) {
173135 params := make (map [string ]string )
174136 envs , err := opts .ParseEnvFile (filepath .Join (filepath .Dir (composeFile ), ".env" ))
@@ -217,9 +179,6 @@ func initFromComposeFile(errWriter io.Writer, name string, composeFile string) e
217179 if err := checkEnvFiles (errWriter , name , cfgMap ); err != nil {
218180 return err
219181 }
220- if err := checkRelativePaths (cfgMap ); err != nil {
221- return err
222- }
223182 params , needsFilling , err := getParamsFromDefaultEnvFile (composeFile , composeRaw )
224183 if err != nil {
225184 return err
0 commit comments