internal: Improve error message consistency

This commit is contained in:
Tom Wiesing 2022-09-04 14:01:01 +02:00
parent 5d906169f4
commit a64c02cd78
No known key found for this signature in database
7 changed files with 72 additions and 71 deletions

View file

@ -1,11 +0,0 @@
package execx
import (
"github.com/tkw1536/goprogram/stream"
)
// Compose runs a docker-compose command in a specific directory, with the provided arguments and streams.
// It then waits for the process to exit, and returns the exit code.
func Compose(io stream.IOStream, workdir string, args ...string) int {
return Exec(io, workdir, "docker", append([]string{"compose"}, args...)...)
}

15
internal/execx/look.go Normal file
View file

@ -0,0 +1,15 @@
package execx
import (
"os/exec"
"path/filepath"
)
// LookPathAbs is like [exec.LookPath], but always returns an absolute path
func LookPathAbs(file string) (string, error) {
path, err := exec.LookPath(file)
if err != nil {
return "", err
}
return filepath.Abs(path)
}

View file

@ -15,6 +15,8 @@ import (
// In the future the idea is to replace this with a native docker compose client.
type Stack struct {
Dir string // Directory this Stack is located in
DockerExecutable string // Path to the native docker executable to use
}
var errStackUpdatePull = errors.New("Stack.Update: Pull returned non-zero exit code")
@ -131,9 +133,17 @@ func (ds Stack) Down(io stream.IOStream) error {
return nil
}
// Compose executes a 'docker compose' command on this stack.
// TODO: This should be removed and replaced by an internal call directly to libcompose.
// compose executes a 'docker compose' command on this stack.
//
// NOTE(twiesing): Check if this can be replaced by an internal call to libcompose.
// But probably not.
func (ds Stack) compose(io stream.IOStream, args ...string) (int, error) {
// TODO: can we migrate to a built-in version of this?
return execx.Compose(io, ds.Dir, args...), nil
if ds.DockerExecutable == "" {
var err error
ds.DockerExecutable, err = execx.LookPathAbs("docker")
if err != nil {
return execx.ExecCommandError, err
}
}
return execx.Exec(io, ds.Dir, ds.DockerExecutable, append([]string{"compose"}, args...)...), nil
}