Skip to content

Commit

Permalink
chore(cmd): Clean up version logic and prefix with * if tree is dirty
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 7, 2024
1 parent 0d78d24 commit 91f1a03
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 22 deletions.
5 changes: 1 addition & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ builds:
ldflags:
- -s
- -w
- -X main.version=v{{.Version}}
- -X main.commit={{.ShortCommit}}
- -X main.date={{.CommitDate}}
- -X main.builtBy=goreleaser
- -X main.version={{.Version}}
goarch:
- amd64
- arm
Expand Down
14 changes: 5 additions & 9 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
"github.com/spf13/cobra"
)

func New(version, commit string) *cobra.Command {
func New(opts ...Option) *cobra.Command {
tmplSubcommand := NewProfiles(FormatText)

cmd := &cobra.Command{
Use: "pwgen",
Short: "Generate passphrases",
Long: `Generate passphrases using the EFF Diceware Wordlists.
See https://www.eff.org/dice for details on the available wordlists.`,
Version: buildVersion(version, commit),
PreRunE: preRun,
RunE: run,

Expand Down Expand Up @@ -101,14 +100,11 @@ See https://www.eff.org/dice for details on the available wordlists.`,
panic(err)
}

return cmd
}

func buildVersion(version, commit string) string {
if commit != "" {
version += " (" + commit + ")"
for _, opt := range opts {
opt(cmd)
}
return version

return cmd
}

func preRun(cmd *cobra.Command, _ []string) error {
Expand Down
11 changes: 11 additions & 0 deletions cmd/opt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cmd

import "github.com/spf13/cobra"

type Option func(cmd *cobra.Command)

func WithVersion(version string) Option {
return func(cmd *cobra.Command) {
cmd.Version = buildVersion(version)
}
}
31 changes: 31 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import "runtime/debug"

func buildVersion(version string) string {
var commit string
var modified bool
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
commit = setting.Value
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
}

if commit != "" {
if len(commit) > 8 {
commit = commit[:8]
}
if modified {
commit = "*" + commit
}
version += " (" + commit + ")"
}
return version
}
2 changes: 1 addition & 1 deletion internal/generate/completions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
panic(err)
}

rootCmd := cmd.New("", "")
rootCmd := cmd.New()
name := rootCmd.Name()
var buf bytes.Buffer
rootCmd.SetOut(&buf)
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
}

var buf bytes.Buffer
root := cmd.New("", "")
root := cmd.New()
tmpl := cmd.NewProfiles(cmd.FormatMarkdown)
if err := doc.GenMarkdown(root, &buf); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/manpages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
panic(err)
}

rootCmd := cmd.New("", "")
rootCmd := cmd.New()
rootName := rootCmd.Name()

date, err := time.Parse(time.RFC3339, dateParam)
Expand Down
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import (
"github.com/gabe565/pwgen-go/cmd"
)

//nolint:gochecknoglobals
var (
version = "beta"
commit = ""
)
var version = "beta"

func main() {
rootCmd := cmd.New(version, commit)
rootCmd := cmd.New(cmd.WithVersion(version))
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down

0 comments on commit 91f1a03

Please sign in to comment.