Skip to content

Commit

Permalink
refactor: use ordered.First (goreleaser#4362)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 authored Oct 11, 2023
1 parent 37e3fde commit 6b00bb9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 25 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 4 additions & 10 deletions internal/client/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down
11 changes: 6 additions & 5 deletions internal/client/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
}, ":")
}

Expand All @@ -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 {
Expand Down
12 changes: 2 additions & 10 deletions internal/pipe/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, "<unknown>")).
WithField("previous_tag", ordered.First(info.PreviousTag, "<unknown>")).
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",
Expand Down

0 comments on commit 6b00bb9

Please sign in to comment.