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

Commit 03b2b09

Browse files
authored
feat: change api to feet portainer
2 parents d05a74a + fd31892 commit 03b2b09

File tree

4 files changed

+151
-16
lines changed

4 files changed

+151
-16
lines changed

go.mod

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

33
go 1.15
4+
5+
require github.com/stretchr/testify v1.7.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,96 @@
11
package wrapper
22

33
import (
4+
"fmt"
45
"log"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
510
"testing"
611
)
712

8-
func TestCommand(t *testing.T) {
9-
w := NewComposeWrapper()
13+
func setup(t *testing.T) *ComposeWrapper {
14+
w, err := NewComposeWrapper("")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
return w
20+
}
21+
22+
func Test_UpAndDown(t *testing.T) {
23+
24+
const composeFileContent = `version: "3.9"
25+
services:
26+
busybox:
27+
image: "alpine:latest"
28+
container_name: "compose_wrapper_test"`
29+
const composedContainerName = "compose_wrapper_test"
30+
31+
w := setup(t)
32+
33+
dir := os.TempDir()
34+
35+
filePath, err := createComposeFile(dir, composeFileContent)
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
_, err = w.Up(filePath, "", "test1", "")
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
45+
if !containerExists(composedContainerName) {
46+
t.Fatal("container should exist")
47+
}
1048

11-
file := "docker-compose-test.yml"
12-
_, err := w.Up(file)
49+
_, err = w.Down(filePath, "", "test1")
1350
if err != nil {
14-
log.Fatalln(err)
51+
t.Fatal(err)
1552
}
1653

17-
_, err = w.Down(file)
54+
if containerExists(composedContainerName) {
55+
t.Fatal("container should be removed")
56+
}
57+
}
58+
59+
type composeOptions struct {
60+
filePath string
61+
url string
62+
envFile string
63+
projectName string
64+
}
65+
66+
func createFile(dir, fileName, content string) (string, error) {
67+
filePath := filepath.Join(dir, fileName)
68+
f, err := os.Create(filePath)
69+
if err != nil {
70+
return "", err
71+
}
72+
73+
f.WriteString(content)
74+
f.Close()
75+
76+
return filePath, nil
77+
}
78+
79+
func createEnvFile(dir, envFileContent string) (string, error) {
80+
return createFile(dir, "stack.env", envFileContent)
81+
}
82+
83+
func createComposeFile(dir, composeFileContent string) (string, error) {
84+
return createFile(dir, "docmer-compose.yml", composeFileContent)
85+
}
86+
87+
func containerExists(containerName string) bool {
88+
cmd := exec.Command("docker", "ps", "-a", "-f", fmt.Sprintf("name=%s", containerName))
89+
90+
out, err := cmd.Output()
1891
if err != nil {
19-
log.Fatalln(err)
92+
log.Fatalf("failed to list containers: %s", err)
2093
}
2194

95+
return strings.Contains(string(out), containerName)
2296
}

0 commit comments

Comments
 (0)