Skip to content

Commit

Permalink
feat: improve the version command (#236)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danvergara authored Oct 23, 2024
1 parent 65605c9 commit b64994b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 8 additions & 20 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
package cmd

/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
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()
},
}

Expand Down
27 changes: 0 additions & 27 deletions cmd/version_test.go

This file was deleted.

67 changes: 53 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
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"
}

0 comments on commit b64994b

Please sign in to comment.