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

Commit 3b442dc

Browse files
committed
feat: change api to feet portainer
1 parent d05a74a commit 3b442dc

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

wrapper.go

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,42 @@ type ComposeWrapper struct {
1212
}
1313

1414
// 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+
}
1619

17-
return &ComposeWrapper{binaryPath: ""}
20+
return &ComposeWrapper{binaryPath: binaryPath}, nil
1821
}
1922

2023
// 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)
2326
}
2427

2528
// 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, "")
2831
}
2932

3033
// 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) {
3235
program := programPath(wrapper.binaryPath, "docker-compose")
3336

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+
}
3548

3649
var stderr bytes.Buffer
37-
cmd := exec.Command(program, args...)
50+
cmd := exec.Command(program, command.ToArgs()...)
3851
cmd.Stderr = &stderr
3952

4053
output, err := cmd.Output()
@@ -44,3 +57,39 @@ func (wrapper *ComposeWrapper) Command(args []string, filePath string) ([]byte,
4457

4558
return output, nil
4659
}
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+
}

wrapper_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package wrapper
22

33
import (
4-
"log"
54
"testing"
65
)
76

87
func TestCommand(t *testing.T) {
9-
w := NewComposeWrapper()
8+
w, err := NewComposeWrapper("")
9+
if err != nil {
10+
t.Fatal(err)
11+
}
1012

1113
file := "docker-compose-test.yml"
12-
_, err := w.Up(file)
14+
_, err = w.Up(file, "", "", "")
1315
if err != nil {
14-
log.Fatalln(err)
16+
t.Fatal(err)
1517
}
1618

17-
_, err = w.Down(file)
19+
_, err = w.Down(file, "", "")
1820
if err != nil {
19-
log.Fatalln(err)
21+
t.Fatal(err)
2022
}
2123

2224
}

0 commit comments

Comments
 (0)