Skip to content

Commit

Permalink
chore(cmd): Clean up versioning logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 7, 2024
1 parent 2c56025 commit 4705e1b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ builds:
ldflags:
- -s
- -w
- -X github.com/gabe565/transsmute/cmd.version={{ .Version }}
- -X main.version={{ .Version }}
goarch:
- amd64
- arm
Expand Down
16 changes: 7 additions & 9 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ import (
"github.com/spf13/cobra"
)

var version = "beta"

func New() *cobra.Command {
version, commit := buildVersion(version)

func New(opts ...Option) *cobra.Command {
cmd := &cobra.Command{
Use: "transsmute",
Long: "Build RSS feeds for websites that don't provide them.",
RunE: run,

Version: version,
Annotations: map[string]string{"commit": commit},

DisableAutoGenTag: true,
}
conf := config.New()
conf.RegisterFlags(cmd)
cmd.SetContext(config.NewContext(context.Background(), conf))

for _, opt := range opts {
opt(cmd)
}

return cmd
}

Expand All @@ -43,7 +41,7 @@ func run(cmd *cobra.Command, _ []string) error {
return err
}

slog.Info("Transsmute", "version", version, "commit", cmd.Annotations["commit"])
slog.Info("Transsmute", "version", cmd.Annotations["version"], "commit", cmd.Annotations["commit"])

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
Expand Down
17 changes: 17 additions & 0 deletions cmd/opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"github.com/spf13/cobra"
)

type Option func(cmd *cobra.Command)

func WithVersion(version string) Option {
return func(cmd *cobra.Command) {
if cmd.Annotations == nil {
cmd.Annotations = make(map[string]string, 2)
}
cmd.Annotations["version"] = version
cmd.Version, cmd.Annotations["commit"] = buildVersion(version)
}
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"github.com/gabe565/transsmute/cmd"
)

var version = "beta"

func main() {
if err := cmd.New().Execute(); err != nil {
root := cmd.New(cmd.WithVersion(version))
if err := root.Execute(); err != nil {
os.Exit(1)
}
}

0 comments on commit 4705e1b

Please sign in to comment.