@@ -12,29 +12,42 @@ type ComposeWrapper struct {
12
12
}
13
13
14
14
// NewComposeWrapper initializes a new ComposeWrapper service with local docker-compose binary.
15
- func NewComposeWrapper () * ComposeWrapper {
15
+ func NewComposeWrapper (binaryPath string ) (* ComposeWrapper , error ) {
16
+ if ! IsBinaryPresent (programPath (binaryPath , "docker-compose" )) {
17
+ return nil , errors .New ("docker-compose binary not found" )
18
+ }
16
19
17
- return & ComposeWrapper {binaryPath : "" }
20
+ return & ComposeWrapper {binaryPath : binaryPath }, nil
18
21
}
19
22
20
23
// Up create and start containers
21
- func (wrapper * ComposeWrapper ) Up (filePath string ) ([]byte , error ) {
22
- return wrapper .Command ([] string { "up" , "-d" }, filePath )
24
+ func (wrapper * ComposeWrapper ) Up (filePath , url , projectName , envFilePath string ) ([]byte , error ) {
25
+ return wrapper .Command (newUpCommand ( filePath ), url , projectName , envFilePath )
23
26
}
24
27
25
28
// Down stop and remove containers
26
- func (wrapper * ComposeWrapper ) Down (filePath string ) ([]byte , error ) {
27
- return wrapper .Command ([] string { "down" , "--remove-orphans" }, filePath )
29
+ func (wrapper * ComposeWrapper ) Down (filePath , url , projectName string ) ([]byte , error ) {
30
+ return wrapper .Command (newDownCommand ( filePath ), url , projectName , "" )
28
31
}
29
32
30
33
// Command exectue a docker-compose commanåd
31
- func (wrapper * ComposeWrapper ) Command (args [] string , filePath string ) ([]byte , error ) {
34
+ func (wrapper * ComposeWrapper ) Command (command composeCommand , url , projectName , envFilePath string ) ([]byte , error ) {
32
35
program := programPath (wrapper .binaryPath , "docker-compose" )
33
36
34
- args = append ([]string {"-f" , filePath }, args ... )
37
+ if projectName != "" {
38
+ command .WithProjectName (projectName )
39
+ }
40
+
41
+ if envFilePath != "" {
42
+ command .WithEnvFilePath (envFilePath )
43
+ }
44
+
45
+ if url != "" {
46
+ command .WithURL (url )
47
+ }
35
48
36
49
var stderr bytes.Buffer
37
- cmd := exec .Command (program , args ... )
50
+ cmd := exec .Command (program , command . ToArgs () ... )
38
51
cmd .Stderr = & stderr
39
52
40
53
output , err := cmd .Output ()
@@ -44,3 +57,39 @@ func (wrapper *ComposeWrapper) Command(args []string, filePath string) ([]byte,
44
57
45
58
return output , nil
46
59
}
60
+
61
+ type composeCommand struct {
62
+ command []string
63
+ args []string
64
+ }
65
+
66
+ func newCommand (command []string , filePath string ) composeCommand {
67
+ return composeCommand {
68
+ args : []string {"-f" , filePath },
69
+ command : command ,
70
+ }
71
+ }
72
+
73
+ func newUpCommand (filePath string ) composeCommand {
74
+ return newCommand ([]string {"up" , "-d" }, filePath )
75
+ }
76
+
77
+ func newDownCommand (filePath string ) composeCommand {
78
+ return newCommand ([]string {"down" , "--remove-orphans" }, filePath )
79
+ }
80
+
81
+ func (command * composeCommand ) WithProjectName (projectName string ) {
82
+ command .args = append (command .args , "-p" , projectName )
83
+ }
84
+
85
+ func (command * composeCommand ) WithEnvFilePath (envFilePath string ) {
86
+ command .args = append (command .args , "--env-file" , envFilePath )
87
+ }
88
+
89
+ func (command * composeCommand ) WithURL (url string ) {
90
+ command .args = append (command .args , "-H" , url )
91
+ }
92
+
93
+ func (command * composeCommand ) ToArgs () []string {
94
+ return append (command .args , command .command ... )
95
+ }
0 commit comments