From 4705e1b0441e6af6f32546a29b71ea6a5211c8f2 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Wed, 7 Aug 2024 16:59:47 -0500 Subject: [PATCH] chore(cmd): Clean up versioning logic --- .goreleaser.yml | 2 +- cmd/cmd.go | 16 +++++++--------- cmd/opts.go | 17 +++++++++++++++++ main.go | 5 ++++- 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 cmd/opts.go diff --git a/.goreleaser.yml b/.goreleaser.yml index e987326..ac13b54 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -8,7 +8,7 @@ builds: ldflags: - -s - -w - - -X github.com/gabe565/transsmute/cmd.version={{ .Version }} + - -X main.version={{ .Version }} goarch: - amd64 - arm diff --git a/cmd/cmd.go b/cmd/cmd.go index f1967ea..2d79b42 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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 } @@ -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() diff --git a/cmd/opts.go b/cmd/opts.go new file mode 100644 index 0000000..5c4822c --- /dev/null +++ b/cmd/opts.go @@ -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) + } +} diff --git a/main.go b/main.go index 0ba063d..6124d2e 100644 --- a/main.go +++ b/main.go @@ -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) } }