-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathversion.go
41 lines (34 loc) · 937 Bytes
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"fmt"
"os"
runtimeDebug "runtime/debug"
"github.com/spf13/cobra"
)
// When building with CI or Make, version is set using `ldflags`
var (
version string
commit string
)
func init() {
// Only use buildInfo if version wasn't set by ldflags, that is its being build by `go install`
if version == "" {
// Main.Version is based on the version control system tag or commit.
// This useful when app is build with `go install`
// See: https://antonz.org/go-1-24/#main-modules-version
buildInfo, _ := runtimeDebug.ReadBuildInfo()
version = buildInfo.Main.Version
}
}
func newVersionCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Show version and build information",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprintf(os.Stdout, "Version: %s\n", version)
fmt.Fprintf(os.Stdout, "CommitSHA: %s\n", commit)
return nil
},
}
return cmd
}