Adding version command to cli tool
parent
6cfbcfe460
commit
e04ef0df1d
@ -0,0 +1,38 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// These variables are set at build time using ldflags
|
||||||
|
var (
|
||||||
|
Version = "dev" // Represents the version of the application
|
||||||
|
BuildTime = "unknown" // The time the binary was built
|
||||||
|
GitCommit = "unknown" // The git commit hash
|
||||||
|
)
|
||||||
|
|
||||||
|
// versionCmd represents the version command
|
||||||
|
var versionCmd = &cobra.Command{
|
||||||
|
Use: "version",
|
||||||
|
Short: "Print the version number, build time, and git commit",
|
||||||
|
Long: `Displays the application's version, the time it was built, the git commit it was built from, and the Go version used.`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Printf("Version: %s\n", Version)
|
||||||
|
fmt.Printf("Build Time: %s\n", BuildTime)
|
||||||
|
fmt.Printf("Git Commit: %s\n", GitCommit)
|
||||||
|
|
||||||
|
// Print Go version and other build info if available
|
||||||
|
if info, ok := debug.ReadBuildInfo(); ok {
|
||||||
|
fmt.Printf("Go Version: %s\n", info.GoVersion)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// init function is called by Go when the package is initialized.
|
||||||
|
// We use it to add the 'version' command to our root command.
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
}
|
Loading…
Reference in New Issue