github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/project/project.go (about)

     1  // Package project sets "high level" defaults related to the project.
     2  package project
     3  
     4  import (
     5  	"fmt"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	"github.com/goreleaser/goreleaser/internal/git"
    10  	"github.com/goreleaser/goreleaser/pkg/context"
    11  )
    12  
    13  // Pipe implemens defaulter to set the project name.
    14  type Pipe struct{}
    15  
    16  func (Pipe) String() string {
    17  	return "project name"
    18  }
    19  
    20  // Default set project defaults.
    21  func (Pipe) Default(ctx *context.Context) error {
    22  	if ctx.Config.ProjectName != "" {
    23  		return nil
    24  	}
    25  
    26  	for _, candidate := range []string{
    27  		ctx.Config.Release.GitHub.Name,
    28  		ctx.Config.Release.GitLab.Name,
    29  		ctx.Config.Release.Gitea.Name,
    30  		moduleName(),
    31  		gitRemote(ctx),
    32  	} {
    33  		if candidate == "" {
    34  			continue
    35  		}
    36  		ctx.Config.ProjectName = candidate
    37  		return nil
    38  	}
    39  
    40  	return fmt.Errorf("couldn't guess project_name, please add it to your config")
    41  }
    42  
    43  func moduleName() string {
    44  	bts, err := exec.Command("go", "list", "-m").CombinedOutput()
    45  	if err != nil {
    46  		return ""
    47  	}
    48  
    49  	mod := strings.TrimSpace(string(bts))
    50  
    51  	// this is the default module used when go runs without a go module.
    52  	// https://pkg.go.dev/cmd/go@master#hdr-Package_lists_and_patterns
    53  	if mod == "command-line-arguments" {
    54  		return ""
    55  	}
    56  
    57  	parts := strings.Split(mod, "/")
    58  	return strings.TrimSpace(parts[len(parts)-1])
    59  }
    60  
    61  func gitRemote(ctx *context.Context) string {
    62  	repo, err := git.ExtractRepoFromConfig(ctx)
    63  	if err != nil {
    64  		return ""
    65  	}
    66  	if err := repo.CheckSCM(); err != nil {
    67  		return ""
    68  	}
    69  	return repo.Name
    70  }