@@ -27,16 +27,17 @@ func Init(name string, composeFiles []string) error {
27
27
return err
28
28
}
29
29
30
+ merger := NewPythonComposeConfigMerger ()
30
31
if len (composeFiles ) == 0 {
31
32
if _ , err := os .Stat ("./docker-compose.yml" ); os .IsNotExist (err ) {
32
33
log .Println ("no compose file detected" )
33
34
return initFromScratch (name )
34
35
} else if err != nil {
35
36
return err
36
37
}
37
- return initFromComposeFiles (name , []string {"./docker-compose.yml" })
38
+ return initFromComposeFiles (name , []string {"./docker-compose.yml" }, merger )
38
39
}
39
- return initFromComposeFiles (name , composeFiles )
40
+ return initFromComposeFiles (name , composeFiles , merger )
40
41
}
41
42
42
43
func initFromScratch (name string ) error {
@@ -60,11 +61,11 @@ Examples of possible values: java, mysql, redis, ruby, postgres, rabbitmq...`)
60
61
return utils .CreateFileWithData (path .Join (dirName , "settings.yml" ), []byte {'\n' })
61
62
}
62
63
63
- func initFromComposeFiles (name string , composeFiles []string ) error {
64
+ func initFromComposeFiles (name string , composeFiles []string , merger ComposeConfigMerger ) error {
64
65
log .Println ("init from compose" )
65
66
66
67
dirName := utils .DirNameFromAppName (name )
67
- composeConfig , err := mergeComposeConfig (composeFiles )
68
+ composeConfig , err := merger . MergeComposeConfig (composeFiles )
68
69
if err != nil {
69
70
return err
70
71
}
@@ -74,21 +75,6 @@ func initFromComposeFiles(name string, composeFiles []string) error {
74
75
return utils .CreateFileWithData (path .Join (dirName , "settings.yml" ), []byte {'\n' })
75
76
}
76
77
77
- func mergeComposeConfig (composeFiles []string ) ([]byte , error ) {
78
- var args []string
79
- for _ , filename := range composeFiles {
80
- args = append (args , fmt .Sprintf ("--file=%v" , filename ))
81
- }
82
- args = append (args , "config" )
83
- cmd := exec .Command ("docker-compose" , args ... )
84
- cmd .Stderr = nil
85
- out , err := cmd .Output ()
86
- if err != nil {
87
- log .Fatalln (string (err .(* exec.ExitError ).Stderr ))
88
- }
89
- return out , err
90
- }
91
-
92
78
func composeFileFromScratch (services []string ) ([]byte , error ) {
93
79
fileStruct := types .NewInitialComposeFile ()
94
80
serviceMap := * fileStruct .Services
@@ -130,3 +116,35 @@ func newMetadata(name string) types.AppMetadata {
130
116
Application : info ,
131
117
}
132
118
}
119
+
120
+ // ComposeConfigMerger is an interface exposing methods to merge
121
+ // multiple compose files into one configuration
122
+ type ComposeConfigMerger interface {
123
+ MergeComposeConfig (composeFiles []string ) ([]byte , error )
124
+ }
125
+
126
+ // PythonComposeConfigMerger implements the ComposeConfigMerger interface and
127
+ // executes a `docker-compose` command to merge configs
128
+ type PythonComposeConfigMerger struct {}
129
+
130
+ // NewPythonComposeConfigMerger returns a ComposeConfigMerger implementor
131
+ func NewPythonComposeConfigMerger () ComposeConfigMerger {
132
+ return & PythonComposeConfigMerger {}
133
+ }
134
+
135
+ // MergeComposeConfig takes a list of paths and merges the Compose files
136
+ // at those paths into a single configuration
137
+ func (m * PythonComposeConfigMerger ) MergeComposeConfig (composeFiles []string ) ([]byte , error ) {
138
+ var args []string
139
+ for _ , filename := range composeFiles {
140
+ args = append (args , fmt .Sprintf ("--file=%v" , filename ))
141
+ }
142
+ args = append (args , "config" )
143
+ cmd := exec .Command ("docker-compose" , args ... )
144
+ cmd .Stderr = nil
145
+ out , err := cmd .Output ()
146
+ if err != nil {
147
+ log .Fatalln (string (err .(* exec.ExitError ).Stderr ))
148
+ }
149
+ return out , err
150
+ }
0 commit comments