feat: add version command to display kcm version from version file

This commit is contained in:
2025-05-20 23:34:58 +02:00
parent 5c43fc60d4
commit a52fa31901
3 changed files with 42 additions and 0 deletions

View 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"
}

View File

@ -55,6 +55,7 @@ func main() {
commands.NewCopyCmd(history),
commands.NewSearchCmd(history),
commands.NewStatusCmd(),
commands.NewVersionCmd(),
)
if err := rootCmd.Execute(); err != nil {