From 24b73c2de4eb43496fb71fbd72128ccf3df14933 Mon Sep 17 00:00:00 2001 From: louis Date: Sun, 22 Oct 2023 10:45:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Add=20Versi?= =?UTF-8?q?on=20Command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .goreleaser.yml | 11 +++++------ cmd/root.go | 4 +--- cmd/run.go | 5 +---- cmd/version.go | 20 ++++++++++++++++++++ 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 cmd/version.go diff --git a/.goreleaser.yml b/.goreleaser.yml index bd71d48b..263bf118 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -12,12 +12,11 @@ builds: - "6" - "7" ldflags: - - "-s -w" - - "-X main.GitCommit={{ .Commit }}" - - "-X main.GitVersion={{ .Version }}" + - -s -w + - -X github.com/systemli/ticker/cmd.version={{.Version}} + - -X github.com/systemli/ticker/cmd.commit={{.Commit}} dockers: - - - goos: linux + - goos: linux goarch: amd64 image_templates: - "systemli/ticker:{{ .Tag }}" @@ -25,7 +24,7 @@ dockers: - "systemli/ticker:{{ .Major }}.{{ .Minor }}" - "systemli/ticker:latest" checksum: - name_template: 'checksums.txt' + name_template: "checksums.txt" snapshot: name_template: "{{ .Tag }}-next" changelog: diff --git a/cmd/root.go b/cmd/root.go index a709291b..e25d74eb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,9 +12,6 @@ import ( ) var ( - GitCommit string - GitVersion string - configPath string cfg config.Config @@ -50,6 +47,7 @@ func initConfig() { func Execute() { rootCmd.AddCommand(runCmd) rootCmd.AddCommand(dbCmd) + rootCmd.AddCommand(versionCmd) if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/cmd/run.go b/cmd/run.go index 2ad141a1..b4aa69b5 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -20,10 +20,7 @@ var ( Use: "run", Short: "Run the ticker", Run: func(cmd *cobra.Command, args []string) { - log.Infof("starting ticker api on %s", cfg.Listen) - if GitCommit != "" && GitVersion != "" { - log.Infof("build info: %s (commit: %s)", GitVersion, GitCommit) - } + log.Infof("starting ticker (version: %s, commit: %s) on %s", version, commit, cfg.Listen) go func() { http.Handle("/metrics", promhttp.Handler()) diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..25d4cfa7 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var ( + version = "dev" + commit = "HEAD" + + versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Ticker version: %s (commit: %s)\n", version, commit) + }, + } +)