2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2025-06-23 22:02:25 +00:00

refactoring: explicit docker commands

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2022-04-24 16:27:04 +02:00
parent 20ba409039
commit 085e57a07a
6 changed files with 54 additions and 22 deletions

View File

@ -15,8 +15,10 @@
package docker
import (
"bufio"
"context"
_ "embed"
"path/filepath"
"strings"
"go.linka.cloud/d2vm/pkg/exec"
@ -35,3 +37,36 @@ func Cmd(ctx context.Context, args ...string) error {
func CmdOut(ctx context.Context, args ...string) (string, string, error) {
return exec.RunOut(ctx, "docker", args...)
}
func Build(ctx context.Context, tag, dockerfile, dir string, buildArgs ...string) error {
if dockerfile == "" {
dockerfile = filepath.Join(dir, "Dockerfile")
}
args := []string{"image", "build", "-t", tag, "-f", dockerfile}
for _, v := range buildArgs {
args = append(args, "--build-arg", v)
}
args = append(args, dir)
return Cmd(ctx, args...)
}
func Remove(ctx context.Context, tag string) error {
return Cmd(ctx, "image", "rm", tag)
}
func ImageList(ctx context.Context, tag string) ([]string, error) {
o, _, err := CmdOut(ctx, "image", "ls", "--format={{ .Repository }}:{{ .Tag }}", tag)
if err != nil {
return nil, err
}
s := bufio.NewScanner(strings.NewReader(o))
var imgs []string
for s.Scan() {
imgs = append(imgs, s.Text())
}
return imgs, s.Err()
}
func Pull(ctx context.Context, tag string) error {
return Cmd(ctx, "image", "pull", tag)
}

View File

@ -29,6 +29,14 @@ var (
CommandContext = exec.CommandContext
)
func SetDebug(debug bool) {
if debug {
Run = RunStdout
} else {
Run = RunNoOut
}
}
func RunStdout(ctx context.Context, c string, args ...string) error {
cmd := exec.CommandContext(ctx, c, args...)
cmd.Stdout = os.Stdout