1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-21 21:07:19 +02:00
Carlos Alexandro Becker ec2db4a727
feat!: rename module to /v2 (#4894)
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->


<!-- If applied, this commit will... -->

...

<!-- Why is this change being made? -->

...

<!-- # Provide links to any relevant tickets, URLs or other resources
-->

...

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2024-05-26 15:02:57 -03:00

60 lines
1.7 KiB
Go

package gomod
import (
"fmt"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/v2/internal/pipe"
"github.com/goreleaser/goreleaser/v2/pkg/context"
)
const (
goPreModulesError = "flag provided but not defined: -m"
go115NotAGoModuleError = "go list -m: not using modules"
go116NotAGoModuleError = "command-line-arguments"
)
// Pipe for gomod.
type Pipe struct{}
func (Pipe) String() string { return "loading go mod information" }
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.GoMod.GoBinary == "" {
ctx.Config.GoMod.GoBinary = "go"
}
return nil
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
flags := []string{"list", "-m"}
if ctx.Config.GoMod.Mod != "" {
flags = append(flags, "-mod="+ctx.Config.GoMod.Mod)
}
cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...)
cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...)
if dir := ctx.Config.GoMod.Dir; dir != "" {
cmd.Dir = dir
}
out, err := cmd.CombinedOutput()
result := strings.TrimSpace(string(out))
if strings.HasPrefix(result, goPreModulesError) {
return pipe.Skip("go version does not support modules")
}
if result == go115NotAGoModuleError || result == go116NotAGoModuleError {
return pipe.Skip("not a go module")
}
if err != nil {
return fmt.Errorf("failed to get module path: %w: %s", err, string(out))
}
// 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]
return nil
}