From 6b00bb9664447e94ec5a4c30996420bd6fdc60da Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 10 Oct 2023 23:16:27 -0300 Subject: [PATCH] refactor: use ordered.First (#4362) --- go.mod | 1 + go.sum | 2 ++ internal/client/git.go | 14 ++++---------- internal/client/github.go | 11 ++++++----- internal/pipe/git/git.go | 12 ++---------- 5 files changed, 15 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index c7ba8d5388a..d0363455ac5 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/caarlos0/log v0.4.2 github.com/charmbracelet/keygen v0.5.0 github.com/charmbracelet/lipgloss v0.8.0 + github.com/charmbracelet/x/exp/ordered v0.0.0-20231010190216-1cb11efc897d github.com/chrismellard/docker-credential-acr-env v0.0.0-20220327082430-c57b701bfc08 github.com/dghubble/go-twitter v0.0.0-20211115160449-93a8679adecb github.com/dghubble/oauth1 v0.7.2 diff --git a/go.sum b/go.sum index b67c318bf11..cc1cb83f7df 100644 --- a/go.sum +++ b/go.sum @@ -247,6 +247,8 @@ github.com/charmbracelet/keygen v0.5.0 h1:XY0fsoYiCSM9axkrU+2ziE6u6YjJulo/b9Dghn github.com/charmbracelet/keygen v0.5.0/go.mod h1:DfvCgLHxZ9rJxdK0DGw3C/LkV4SgdGbnliHcObV3L+8= github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU= github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU= +github.com/charmbracelet/x/exp/ordered v0.0.0-20231010190216-1cb11efc897d h1:+o+e/8hf7cG0SbAzEAm/usJ8qoZPgFXhudLjop+TM0g= +github.com/charmbracelet/x/exp/ordered v0.0.0-20231010190216-1cb11efc897d/go.mod h1:aoG4bThKYIOnyB55r202eHqo6TkN7ZXV+cu4Do3eoBQ= github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E= github.com/chrismellard/docker-credential-acr-env v0.0.0-20220327082430-c57b701bfc08 h1:9Qh4lJ/KMr5iS1zfZ8I97+3MDpiKjl+0lZVUNBhdvRs= github.com/chrismellard/docker-credential-acr-env v0.0.0-20220327082430-c57b701bfc08/go.mod h1:MAuu1uDJNOS3T3ui0qmKdPUwm59+bO19BbTph2wZafE= diff --git a/internal/client/git.go b/internal/client/git.go index 0cc73364304..9f0ebc944cf 100644 --- a/internal/client/git.go +++ b/internal/client/git.go @@ -10,6 +10,7 @@ import ( "sync" "github.com/caarlos0/log" + "github.com/charmbracelet/x/exp/ordered" "github.com/goreleaser/goreleaser/internal/git" "github.com/goreleaser/goreleaser/internal/pipe" "github.com/goreleaser/goreleaser/internal/tmpl" @@ -54,7 +55,7 @@ func (g *gitClient) CreateFiles( return pipe.Skip("url is empty") } - repo.Name = firstNonEmpty(repo.Name, nameFromURL(url)) + repo.Name = ordered.First(repo.Name, nameFromURL(url)) key, err := tmpl.New(ctx).Apply(repo.PrivateKey) if err != nil { @@ -68,7 +69,7 @@ func (g *gitClient) CreateFiles( sshcmd, err := tmpl.New(ctx).WithExtraFields(tmpl.Fields{ "KeyPath": key, - }).Apply(firstNonEmpty(repo.GitSSHCommand, DefaulGitSSHCommand)) + }).Apply(ordered.First(repo.GitSSHCommand, DefaulGitSSHCommand)) if err != nil { return fmt.Errorf("git: failed to template ssh command: %w", err) } @@ -93,7 +94,7 @@ func (g *gitClient) CreateFiles( {"config", "--local", "user.name", commitAuthor.Name}, {"config", "--local", "user.email", commitAuthor.Email}, {"config", "--local", "commit.gpgSign", "false"}, - {"config", "--local", "init.defaultBranch", firstNonEmpty(g.branch, "master")}, + {"config", "--local", "init.defaultBranch", ordered.First(g.branch, "master")}, }); err != nil { return fmt.Errorf("git: failed to setup local repository: %w", err) } @@ -212,13 +213,6 @@ func runGitCmds(ctx *context.Context, cwd string, env []string, cmds [][]string) return nil } -func firstNonEmpty(s1, s2 string) string { - if s1 != "" { - return s1 - } - return s2 -} - func nameFromURL(url string) string { return strings.TrimSuffix(url[strings.LastIndex(url, "/")+1:], ".git") } diff --git a/internal/client/github.go b/internal/client/github.go index 4e242155a7d..b7ad89ade68 100644 --- a/internal/client/github.go +++ b/internal/client/github.go @@ -12,6 +12,7 @@ import ( "time" "github.com/caarlos0/log" + "github.com/charmbracelet/x/exp/ordered" "github.com/google/go-github/v55/github" "github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/tmpl" @@ -167,9 +168,9 @@ func (c *githubClient) CloseMilestone(ctx *context.Context, repo Repo, title str func headString(base, head Repo) string { return strings.Join([]string{ - firstNonEmpty(head.Owner, base.Owner), - firstNonEmpty(head.Name, base.Name), - firstNonEmpty(head.Branch, base.Branch), + ordered.First(head.Owner, base.Owner), + ordered.First(head.Name, base.Name), + ordered.First(head.Branch, base.Branch), }, ":") } @@ -196,8 +197,8 @@ func (c *githubClient) OpenPullRequest( draft bool, ) error { c.checkRateLimit(ctx) - base.Owner = firstNonEmpty(base.Owner, head.Owner) - base.Name = firstNonEmpty(base.Name, head.Name) + base.Owner = ordered.First(base.Owner, head.Owner) + base.Name = ordered.First(base.Name, head.Name) if base.Branch == "" { def, err := c.getDefaultBranch(ctx, base) if err != nil { diff --git a/internal/pipe/git/git.go b/internal/pipe/git/git.go index 2b98199528d..131d4d1e4e2 100644 --- a/internal/pipe/git/git.go +++ b/internal/pipe/git/git.go @@ -10,6 +10,7 @@ import ( "time" "github.com/caarlos0/log" + "github.com/charmbracelet/x/exp/ordered" "github.com/goreleaser/goreleaser/internal/git" "github.com/goreleaser/goreleaser/internal/pipe" "github.com/goreleaser/goreleaser/internal/skips" @@ -46,22 +47,13 @@ func (Pipe) Run(ctx *context.Context) error { log.WithField("commit", info.Commit). WithField("branch", info.Branch). WithField("current_tag", info.CurrentTag). - WithField("previous_tag", firstNonEmpty(info.PreviousTag, "")). + WithField("previous_tag", ordered.First(info.PreviousTag, "")). WithField("dirty", info.Dirty). Info("git state") ctx.Version = strings.TrimPrefix(ctx.Git.CurrentTag, "v") return validate(ctx) } -func firstNonEmpty(ss ...string) string { - for _, s := range ss { - if s != "" { - return s - } - } - return "" -} - // nolint: gochecknoglobals var fakeInfo = context.GitInfo{ Branch: "none",