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" +}