Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use build info to retrieve version information #107

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"os"
pathpkg "path"
"runtime/debug"
"strings"

"cloud.google.com/go/spanner"
Expand All @@ -44,7 +45,7 @@ const (
`
)

var version = "dev"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still should use the version variable populated by ldflags when using a binary built by go build.

var version string

var (
rootOpts = internal.ArgType{}
Expand Down Expand Up @@ -105,7 +106,7 @@ var (
},
SilenceUsage: true,
SilenceErrors: true,
Version: version,
Version: versionInfo(),
}
)

Expand Down Expand Up @@ -230,3 +231,16 @@ func connectSpanner(args *internal.ArgType) (*spanner.Client, error) {

return spannerClient, nil
}

func versionInfo() string {
if version != "" {
return version
}

// For those who "go install" yo
info, ok := debug.ReadBuildInfo()
if !ok {
return "(devel)"
}
return info.Main.Version
}
18 changes: 16 additions & 2 deletions v2/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package cmd

import (
"runtime/debug"
"strings"

"github.com/spf13/cobra"
Expand All @@ -36,7 +37,7 @@ const (
`
)

var version = "dev"
var version string

var (
rootCmd = &cobra.Command{
Expand All @@ -49,10 +50,23 @@ var (
RunE: nil,
SilenceUsage: true,
SilenceErrors: true,
Version: version,
Version: versionInfo(),
}
)

func Execute() error {
return rootCmd.Execute()
}

func versionInfo() string {
if version != "" {
return version
}

// For those who "go install" yo
info, ok := debug.ReadBuildInfo()
if !ok {
return "(devel)"
}
return info.Main.Version
}