2021-03-22 08:55:01 -03:00
|
|
|
package gomod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
2021-03-30 21:06:25 -03:00
|
|
|
const (
|
2023-09-07 20:01:57 +02:00
|
|
|
goPreModulesError = "flag provided but not defined: -m"
|
2021-03-30 21:06:25 -03:00
|
|
|
go115NotAGoModuleError = "go list -m: not using modules"
|
|
|
|
go116NotAGoModuleError = "command-line-arguments"
|
|
|
|
)
|
|
|
|
|
2021-09-22 22:25:26 -03:00
|
|
|
// Pipe for gomod.
|
2021-03-22 08:55:01 -03:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2021-09-18 10:21:29 -03:00
|
|
|
func (Pipe) String() string { return "loading go mod information" }
|
2021-03-22 08:55:01 -03:00
|
|
|
|
2021-03-30 21:06:25 -03:00
|
|
|
// Default sets the pipe defaults.
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
if ctx.Config.GoMod.GoBinary == "" {
|
|
|
|
ctx.Config.GoMod.GoBinary = "go"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-30 09:28:44 -03:00
|
|
|
|
2021-03-22 08:55:01 -03:00
|
|
|
// Run the pipe.
|
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2022-03-17 08:53:39 -03:00
|
|
|
flags := []string{"list", "-m"}
|
|
|
|
if ctx.Config.GoMod.Mod != "" {
|
|
|
|
flags = append(flags, "-mod="+ctx.Config.GoMod.Mod)
|
|
|
|
}
|
2022-10-04 13:01:18 -03:00
|
|
|
cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...)
|
|
|
|
cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...)
|
|
|
|
out, err := cmd.CombinedOutput()
|
2021-03-22 08:55:01 -03:00
|
|
|
result := strings.TrimSpace(string(out))
|
2023-09-07 20:01:57 +02:00
|
|
|
if strings.HasPrefix(result, goPreModulesError) {
|
|
|
|
return pipe.Skip("go version does not support modules")
|
|
|
|
}
|
2021-03-30 09:28:44 -03:00
|
|
|
if result == go115NotAGoModuleError || result == go116NotAGoModuleError {
|
2021-03-22 08:55:01 -03:00
|
|
|
return pipe.Skip("not a go module")
|
|
|
|
}
|
2021-04-03 16:47:57 -03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get module path: %w: %s", err, string(out))
|
|
|
|
}
|
2021-03-22 08:55:01 -03:00
|
|
|
|
2023-11-04 01:59:25 +00:00
|
|
|
// Splits and use the first line in case a `go.work` file exists with multiple modules.
|
|
|
|
// The first module is/should be `.` in the `go.work` file, so this should be correct.
|
|
|
|
// Running `go work sync` also always puts `.` as the first line in `use`.
|
|
|
|
ctx.ModulePath = strings.Split(result, "\n")[0]
|
2021-03-22 08:55:01 -03:00
|
|
|
return nil
|
|
|
|
}
|