github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/pipe/docker/api.go (about)

     1  package docker
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"os/exec"
     9  	"sync"
    10  
    11  	"github.com/apex/log"
    12  	"github.com/goreleaser/goreleaser/internal/gio"
    13  	"github.com/goreleaser/goreleaser/internal/logext"
    14  )
    15  
    16  var (
    17  	manifesters = map[string]manifester{}
    18  	imagers     = map[string]imager{}
    19  	lock        sync.Mutex
    20  )
    21  
    22  func registerManifester(use string, impl manifester) {
    23  	lock.Lock()
    24  	defer lock.Unlock()
    25  	manifesters[use] = impl
    26  }
    27  
    28  func registerImager(use string, impl imager) {
    29  	lock.Lock()
    30  	defer lock.Unlock()
    31  	imagers[use] = impl
    32  }
    33  
    34  // imager is something that can build and push docker images.
    35  type imager interface {
    36  	Build(ctx context.Context, root string, images, flags []string) error
    37  	Push(ctx context.Context, image string, flags []string) error
    38  }
    39  
    40  // manifester is something that can create and push docker manifests.
    41  type manifester interface {
    42  	Create(ctx context.Context, manifest string, images, flags []string) error
    43  	Push(ctx context.Context, manifest string, flags []string) error
    44  }
    45  
    46  // nolint: unparam
    47  func runCommand(ctx context.Context, dir, binary string, args ...string) error {
    48  	fields := log.Fields{
    49  		"cmd": append([]string{binary}, args[0]),
    50  		"cwd": dir,
    51  	}
    52  
    53  	/* #nosec */
    54  	cmd := exec.CommandContext(ctx, binary, args...)
    55  	cmd.Dir = dir
    56  
    57  	var b bytes.Buffer
    58  	w := gio.Safe(&b)
    59  	cmd.Stderr = io.MultiWriter(logext.NewWriter(fields, logext.Error), w)
    60  	cmd.Stdout = io.MultiWriter(logext.NewWriter(fields, logext.Info), w)
    61  
    62  	log.WithFields(fields).WithField("args", args[1:]).Debug("running")
    63  	if err := cmd.Run(); err != nil {
    64  		return fmt.Errorf("%w: %s", err, b.String())
    65  	}
    66  	return nil
    67  }