From 8433586669367683e1e4e30612a6cf463eabf555 Mon Sep 17 00:00:00 2001 From: Tomas Zahradnicek Date: Fri, 16 Aug 2024 11:06:12 +0200 Subject: [PATCH] fix: log errors, correct version from BuildInfo --- CHANGELOG.md | 4 ++++ cmd/tea/gen_id.go | 11 ++--------- cmd/tea/main.go | 12 ++++++++++-- cmd/tea/version.go | 8 ++++++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 430a725..25a52e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ How to release a new version: - Manually release new version. ## [Unreleased] +### Added +- Use `debug.ReadBuildInfo` to get correct version. +- Print to stderr on error. + ### Removed - `openapi compose`, `repo init`, `repo template` commands. diff --git a/cmd/tea/gen_id.go b/cmd/tea/gen_id.go index 99e3d81..9a3b5c4 100644 --- a/cmd/tea/gen_id.go +++ b/cmd/tea/gen_id.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "errors" "fmt" "go/ast" "go/parser" @@ -38,14 +37,8 @@ Example: RefreshToken uint64 ) `, - Run: func(cmd *cobra.Command, args []string) { - if err := runGenerateIDs(genIDOptions.SourceFilePath, genIDOptions.OutputFilePath); err != nil { - e := &cmderrors.CommandError{} - if errors.As(err, &e) { - os.Exit(e.Code) - } - os.Exit(-1) - } + RunE: func(cmd *cobra.Command, args []string) error { + return runGenerateIDs(genIDOptions.SourceFilePath, genIDOptions.OutputFilePath) }, } diff --git a/cmd/tea/main.go b/cmd/tea/main.go index 5535f55..363fe3f 100644 --- a/cmd/tea/main.go +++ b/cmd/tea/main.go @@ -1,12 +1,20 @@ package main -import "os" +import ( + "errors" + "os" + + cmderrors "go.strv.io/tea/pkg/errors" +) func main() { // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. if err := rootCmd.Execute(); err != nil { - // TODO: Log the error. + e := &cmderrors.CommandError{} + if errors.As(err, &e) { + os.Exit(e.Code) + } os.Exit(1) } } diff --git a/cmd/tea/version.go b/cmd/tea/version.go index c690737..f26a990 100644 --- a/cmd/tea/version.go +++ b/cmd/tea/version.go @@ -2,12 +2,11 @@ package main import ( "fmt" + "runtime/debug" "github.com/spf13/cobra" ) -var version = "0.0.0" // version is set during build - var ( versionCmd = &cobra.Command{ Use: "version", @@ -19,6 +18,11 @@ Example: Provided by ` + colorLinkSTRV, Run: func(cmd *cobra.Command, args []string) { + version := "unknown" + buildInfo, ok := debug.ReadBuildInfo() + if ok { + version = buildInfo.Main.Version + } _, _ = fmt.Println(version) }, }