github.com/triarius/goreleaser@v1.12.5/internal/pipe/gomod/gomod.go (about)

     1  package gomod
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	"github.com/triarius/goreleaser/internal/pipe"
     9  	"github.com/triarius/goreleaser/pkg/context"
    10  )
    11  
    12  const (
    13  	go115NotAGoModuleError = "go list -m: not using modules"
    14  	go116NotAGoModuleError = "command-line-arguments"
    15  )
    16  
    17  // Pipe for gomod.
    18  type Pipe struct{}
    19  
    20  func (Pipe) String() string { return "loading go mod information" }
    21  
    22  // Default sets the pipe defaults.
    23  func (Pipe) Default(ctx *context.Context) error {
    24  	if ctx.Config.GoMod.GoBinary == "" {
    25  		ctx.Config.GoMod.GoBinary = "go"
    26  	}
    27  	return nil
    28  }
    29  
    30  // Run the pipe.
    31  func (Pipe) Run(ctx *context.Context) error {
    32  	flags := []string{"list", "-m"}
    33  	if ctx.Config.GoMod.Mod != "" {
    34  		flags = append(flags, "-mod="+ctx.Config.GoMod.Mod)
    35  	}
    36  	cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...)
    37  	cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...)
    38  	out, err := cmd.CombinedOutput()
    39  	result := strings.TrimSpace(string(out))
    40  	if result == go115NotAGoModuleError || result == go116NotAGoModuleError {
    41  		return pipe.Skip("not a go module")
    42  	}
    43  	if err != nil {
    44  		return fmt.Errorf("failed to get module path: %w: %s", err, string(out))
    45  	}
    46  
    47  	ctx.ModulePath = result
    48  	return nil
    49  }