Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit d01bc85

Browse files
authored
Merge pull request #13 from portainer/feat-use-working-dir
feat: allow to specify a working directory for a compose
2 parents 0a14182 + 97d860c commit d01bc85

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/portainer/docker-compose-wrapper
22

3-
go 1.15
3+
go 1.16
4+
5+
require github.com/pkg/errors v0.9.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

wrapper.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package wrapper
22

33
import (
44
"bytes"
5-
"errors"
65
"fmt"
76
"os"
87
"os/exec"
98
"strings"
9+
10+
"github.com/pkg/errors"
1011
)
1112

1213
var (
@@ -29,17 +30,17 @@ func NewComposeWrapper(binaryPath string) (*ComposeWrapper, error) {
2930
}
3031

3132
// Up create and start containers
32-
func (wrapper *ComposeWrapper) Up(filePaths []string, url, projectName, envFilePath, configPath string) ([]byte, error) {
33-
return wrapper.Command(newUpCommand(filePaths), url, projectName, envFilePath, configPath)
33+
func (wrapper *ComposeWrapper) Up(filePaths []string, projectDir, host, projectName, envFilePath, configPath string) ([]byte, error) {
34+
return wrapper.Command(newUpCommand(filePaths), projectDir, host, projectName, envFilePath, configPath)
3435
}
3536

3637
// Down stop and remove containers
37-
func (wrapper *ComposeWrapper) Down(filePaths []string, url, projectName string) ([]byte, error) {
38-
return wrapper.Command(newDownCommand(filePaths), url, projectName, "", "")
38+
func (wrapper *ComposeWrapper) Down(filePaths []string, projectDir string, host, projectName string) ([]byte, error) {
39+
return wrapper.Command(newDownCommand(filePaths), projectDir, host, projectName, "", "")
3940
}
4041

4142
// Command exectue a docker-compose commanåd
42-
func (wrapper *ComposeWrapper) Command(command composeCommand, url, projectName, envFilePath, configPath string) ([]byte, error) {
43+
func (wrapper *ComposeWrapper) Command(command composeCommand, workingDir, host, projectName, envFilePath, configPath string) ([]byte, error) {
4344
program := programPath(wrapper.binaryPath, "docker-compose")
4445

4546
if projectName != "" {
@@ -50,12 +51,13 @@ func (wrapper *ComposeWrapper) Command(command composeCommand, url, projectName,
5051
command.WithEnvFilePath(envFilePath)
5152
}
5253

53-
if url != "" {
54-
command.WithURL(url)
54+
if host != "" {
55+
command.WithHost(host)
5556
}
5657

5758
var stderr bytes.Buffer
5859
cmd := exec.Command(program, command.ToArgs()...)
60+
cmd.Dir = workingDir
5961

6062
if configPath != "" {
6163
cmd.Env = os.Environ()
@@ -66,7 +68,7 @@ func (wrapper *ComposeWrapper) Command(command composeCommand, url, projectName,
6668

6769
output, err := cmd.Output()
6870
if err != nil {
69-
return nil, errors.New(stderr.String())
71+
return nil, errors.Wrap(err, stderr.String())
7072
}
7173

7274
return output, nil
@@ -105,8 +107,8 @@ func (command *composeCommand) WithEnvFilePath(envFilePath string) {
105107
command.args = append(command.args, "--env-file", envFilePath)
106108
}
107109

108-
func (command *composeCommand) WithURL(url string) {
109-
command.args = append(command.args, "-H", url)
110+
func (command *composeCommand) WithHost(host string) {
111+
command.args = append(command.args, "-H", host)
110112
}
111113

112114
func (command *composeCommand) ToArgs() []string {

wrapper_test.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ services:
7575
t.Fatal(err)
7676
}
7777

78-
_, err = w.Up([]string{filePathOriginal, filePathOverride}, "", "test1", "", "")
78+
_, err = w.Up([]string{filePathOriginal, filePathOverride}, "", "", "test1", "", "")
7979
if err != nil {
8080
t.Fatal(err)
8181
}
@@ -84,7 +84,7 @@ services:
8484
t.Fatal("container should exist")
8585
}
8686

87-
_, err = w.Down([]string{filePathOriginal, filePathOverride}, "", "test1")
87+
_, err = w.Down([]string{filePathOriginal, filePathOverride}, "", "", "test1")
8888
if err != nil {
8989
t.Fatal(err)
9090
}
@@ -94,13 +94,6 @@ services:
9494
}
9595
}
9696

97-
type composeOptions struct {
98-
filePath string
99-
url string
100-
envFile string
101-
projectName string
102-
}
103-
10497
func createFile(dir, fileName, content string) (string, error) {
10598
filePath := filepath.Join(dir, fileName)
10699
f, err := os.Create(filePath)
@@ -114,14 +107,6 @@ func createFile(dir, fileName, content string) (string, error) {
114107
return filePath, nil
115108
}
116109

117-
func createEnvFile(dir, envFileContent string) (string, error) {
118-
return createFile(dir, "stack.env", envFileContent)
119-
}
120-
121-
func createComposeFile(dir, composeFileContent string) (string, error) {
122-
return createFile(dir, "docmer-compose.yml", composeFileContent)
123-
}
124-
125110
func containerExists(containerName string) bool {
126111
cmd := exec.Command("docker", "ps", "-a", "-f", fmt.Sprintf("name=%s", containerName))
127112

0 commit comments

Comments
 (0)