From b64994b3b8141b2b65d684ec06f66be879474f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Omar=20Vergara=20P=C3=A9rez?= Date: Wed, 23 Oct 2024 11:09:40 -0600 Subject: [PATCH] feat: improve the version command (#236) * feat(version): improve the version handling by using native go tools * feat(main): add either a version command and version flag * docs(readme): add the version flag to the help flag output --- README.md | 1 + cmd/version.go | 28 ++++++------------- cmd/version_test.go | 27 ------------------ main.go | 67 +++++++++++++++++++++++++++++++++++---------- 4 files changed, 62 insertions(+), 61 deletions(-) delete mode 100644 cmd/version_test.go diff --git a/README.md b/README.md index e1c87fd..30481ad 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Flags: --trust-server-certificate string [false|true] server certificate is checked or not -u, --url string Database connection string --user string Database user + -v, --version version for dblab --wallet string Path for auto-login oracle wallet Use "dblab [command] --help" for more information about a command. diff --git a/cmd/version.go b/cmd/version.go index 0b0a633..a0cf3e0 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,39 +1,27 @@ package cmd -/* -Copyright © 2021 NAME HERE - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - import ( "fmt" "github.com/spf13/cobra" ) +func SetVersionInfo(version string) { + rootCmd.Version = fmt.Sprint(version) +} + // NewVersionCmd return a versionCmd instance. func NewVersionCmd() *cobra.Command { - // versionCmd represents the version command. versionCmd := &cobra.Command{ Use: "version", Short: "The version of the project", Long: `The current version of the project. This projects follows the semantic versioning standard.`, - RunE: func(cmd *cobra.Command, args []string) error { - fmt.Fprintln(cmd.OutOrStdout(), "v0.26.0") - return nil + Run: func(cmd *cobra.Command, args []string) { + root := cmd.Root() + root.SetArgs([]string{"--version"}) + _ = root.Execute() }, } diff --git a/cmd/version_test.go b/cmd/version_test.go deleted file mode 100644 index 73a60d0..0000000 --- a/cmd/version_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package cmd - -import ( - "bytes" - "io" - "strings" - "testing" -) - -func TestVersionCmd(t *testing.T) { - cmd := NewVersionCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - err := cmd.Execute() - if err != nil { - t.Fatal(err) - } - - out, err := io.ReadAll(b) - if err != nil { - t.Fatal(err) - } - - if !strings.Contains(string(out), "v0.26.0") { - t.Fatalf("expected \"%s\" got \"%s\"", "v0.26.0", string(out)) - } -} diff --git a/main.go b/main.go index ac4ea4b..53ef087 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,61 @@ -/* -Copyright © 2021 NAME HERE +package main -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +import ( + "fmt" + "runtime/debug" - http://www.apache.org/licenses/LICENSE-2.0 + "github.com/danvergara/dblab/cmd" +) -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package main +// these values are automagically populated by Goreleaser. +var ( + version = "dev" + Revision = "unknown" +) -import "github.com/danvergara/dblab/cmd" +func init() { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + + for _, kv := range info.Settings { + switch kv.Key { + case "vcs.revision": + Revision = kv.Value + } + } +} func main() { + if version == "dev" { + version = parseVersion() + } else { + // Goreleaser doesn't prefix with a `v`, which we expect. + version = "v" + version + } + + cmd.SetVersionInfo(version) cmd.Execute() } + +// parseVersion parses the version passed as a parameter. +// If the version is equal to unknown or (devel), it shows the commit hash as a revision. +func parseVersion() string { + info, _ := debug.ReadBuildInfo() + v := info.Main.Version + + if v == "unknown" || v == "(devel)" { + if Revision != "unknown" && Revision != "" { + commit := Revision + if len(commit) > 7 { + commit = commit[:7] + } + return fmt.Sprintf("rev: %s", commit) + } + } else { + return v + } + + return "unknown" +}