feat: add version command to display kcm version from version file
This commit is contained in:
37
src/commands/cmd_version.go
Normal file
37
src/commands/cmd_version.go
Normal file
@ -0,0 +1,37 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// NewVersionCmd returns the cobra command for `kcm version`
|
||||
func NewVersionCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Show kcm version",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
version := getVersionFromFile()
|
||||
fmt.Println(version)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// getVersionFromFile tries to find and read the kcm.version file next to the binary, or in /usr/local/share/kcm
|
||||
func getVersionFromFile() string {
|
||||
binPath, err := os.Executable()
|
||||
if err == nil {
|
||||
localVersion := filepath.Join(filepath.Dir(binPath), "kcm.version")
|
||||
if data, err := os.ReadFile(localVersion); err == nil {
|
||||
return string(data)
|
||||
}
|
||||
}
|
||||
// fallback to global install location
|
||||
if data, err := os.ReadFile("/usr/local/share/kcm/kcm.version"); err == nil {
|
||||
return string(data)
|
||||
}
|
||||
return "unknown version"
|
||||
}
|
@ -55,6 +55,7 @@ func main() {
|
||||
commands.NewCopyCmd(history),
|
||||
commands.NewSearchCmd(history),
|
||||
commands.NewStatusCmd(),
|
||||
commands.NewVersionCmd(),
|
||||
)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
|
Reference in New Issue
Block a user