/
api.go
96 lines (80 loc) · 2.45 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package docker
import (
"bytes"
"fmt"
"io"
"os/exec"
"sync"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/logext"
"github.com/goreleaser/goreleaser/pkg/context"
)
var (
manifesters = map[string]manifester{}
imagers = map[string]imager{}
lock sync.Mutex
)
func registerManifester(use string, impl manifester) {
lock.Lock()
defer lock.Unlock()
manifesters[use] = impl
}
func registerImager(use string, impl imager) {
lock.Lock()
defer lock.Unlock()
imagers[use] = impl
}
// imager is something that can build and push docker images.
type imager interface {
Build(ctx *context.Context, root string, images, flags []string) error
Push(ctx *context.Context, image string, flags []string) (digest string, err error)
}
// manifester is something that can create and push docker manifests.
type manifester interface {
Create(ctx *context.Context, manifest string, images, flags []string) error
Push(ctx *context.Context, manifest string, flags []string) error
}
// nolint: unparam
func runCommand(ctx *context.Context, dir, binary string, args ...string) error {
fields := log.Fields{
"cmd": append([]string{binary}, args[0]),
"cwd": dir,
}
/* #nosec */
cmd := exec.CommandContext(ctx, binary, args...)
cmd.Dir = dir
cmd.Env = ctx.Env.Strings()
var b bytes.Buffer
w := gio.Safe(&b)
cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)
cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
log.WithFields(fields).WithField("args", args[1:]).Debug("running")
if err := cmd.Run(); err != nil {
return fmt.Errorf("%w: %s", err, b.String())
}
return nil
}
func runCommandWithOutput(ctx *context.Context, dir, binary string, args ...string) ([]byte, error) {
fields := log.Fields{
"cmd": append([]string{binary}, args[0]),
"cwd": dir,
}
/* #nosec */
cmd := exec.CommandContext(ctx, binary, args...)
cmd.Dir = dir
cmd.Env = ctx.Env.Strings()
var b bytes.Buffer
w := gio.Safe(&b)
cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)
log.WithFields(fields).WithField("args", args[1:]).Debug("running")
out, err := cmd.Output()
if out != nil {
// regardless of command success, always print stdout for backward-compatibility with runCommand()
_, _ = io.MultiWriter(logext.NewWriter(fields, logext.Error), w).Write(out)
}
if err != nil {
return nil, fmt.Errorf("%w: %s", err, b.String())
}
return out, nil
}