supports docker compose and docker-compose (#171)

This commit is contained in:
Marco Argentieri
2023-01-29 20:19:20 +01:00
committed by GitHub
parent efb4d50f48
commit dfb48209b0
6 changed files with 21 additions and 11 deletions

View File

@@ -3,7 +3,6 @@ package main
import (
"errors"
"os"
"os/exec"
"path/filepath"
"github.com/urfave/cli/v2"
@@ -34,7 +33,7 @@ func logsAction(ctx *cli.Context) error {
datadir := ctx.String("datadir")
composePath := filepath.Join(datadir, config.DefaultCompose)
bashCmd := exec.Command("docker-compose", "-f", composePath, "logs", serviceName)
bashCmd := runDockerCompose(composePath, "logs", serviceName)
bashCmd.Stdout = os.Stdout
bashCmd.Stderr = os.Stderr

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
@@ -213,3 +214,16 @@ func cleanAndExpandPath(path string) string {
// but the variables can still be expanded via POSIX-style $VARIABLE.
return filepath.Clean(os.ExpandEnv(path))
}
// runDockerCompose runs docker-compose with the given arguments
func runDockerCompose(composePath string, args ...string) *exec.Cmd {
var cmd *exec.Cmd
_, err := exec.LookPath("docker-compose")
if err != nil {
cmd = exec.Command("docker", append([]string{"compose", "-f", composePath}, args...)...)
} else {
cmd = exec.Command("docker-compose", append([]string{"-f", composePath}, args...)...)
}
return cmd
}

View File

@@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
@@ -71,10 +70,10 @@ func startAction(ctx *cli.Context) error {
}
}
args := []string{"-f", composePath, "up", "-d"}
args := []string{"up", "-d"}
args = append(args, servicesToRun...)
bashCmd := exec.Command("docker-compose", args...)
bashCmd := runDockerCompose(composePath, args...)
bashCmd.Stdout = os.Stdout
bashCmd.Stderr = os.Stderr

View File

@@ -3,7 +3,6 @@ package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
@@ -31,9 +30,9 @@ func stopAction(ctx *cli.Context) error {
datadir := ctx.String("datadir")
composePath := filepath.Join(datadir, config.DefaultCompose)
bashCmd := exec.Command("docker-compose", "-f", composePath, "stop")
bashCmd := runDockerCompose(composePath, "stop")
if delete {
bashCmd = exec.Command("docker-compose", "-f", composePath, "down", "--volumes")
bashCmd = runDockerCompose(composePath, "down", "--volumes")
}
bashCmd.Stdout = os.Stdout
bashCmd.Stderr = os.Stderr

View File

@@ -2,7 +2,6 @@ package main
import (
"os"
"os/exec"
"path/filepath"
"github.com/urfave/cli/v2"
@@ -19,7 +18,7 @@ func updateAction(ctx *cli.Context) error {
datadir := ctx.String("datadir")
composePath := filepath.Join(datadir, config.DefaultCompose)
bashCmd := exec.Command("docker-compose", "-f", composePath, "pull")
bashCmd := runDockerCompose(composePath, "pull")
bashCmd.Stdout = os.Stdout
bashCmd.Stderr = os.Stderr

2
go.mod
View File

@@ -6,7 +6,7 @@ require (
github.com/btcsuite/btcd v0.21.0-beta // indirect
github.com/btcsuite/btcutil v1.0.2
github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/urfave/cli/v2 v2.3.0
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect