From a52fa31901908bb1d9061fa04479028513d5ba21 Mon Sep 17 00:00:00 2001 From: Menno van Leeuwen Date: Tue, 20 May 2025 23:34:58 +0200 Subject: [PATCH] feat: add version command to display kcm version from version file --- bin/scripts/install.sh | 4 ++++ src/commands/cmd_version.go | 37 +++++++++++++++++++++++++++++++++++++ src/main.go | 1 + 3 files changed, 42 insertions(+) create mode 100644 src/commands/cmd_version.go diff --git a/bin/scripts/install.sh b/bin/scripts/install.sh index bb8a389..39e3fda 100755 --- a/bin/scripts/install.sh +++ b/bin/scripts/install.sh @@ -24,4 +24,8 @@ printfe "%s\n" "cyan" "Installing kcm..." cp $(pwd)/bin/kcm /usr/local/bin/kcm cp $(pwd)/bin/kcm-completion.bash /usr/share/bash-completion/completions/kcm +# Copy version file to /usr/local/share/kcm/kcm.version +mkdir -p /usr/local/share/kcm +cp $(pwd)/bin/kcm.version /usr/local/share/kcm/kcm.version + printfe "%s\n" "green" "Installation complete." diff --git a/src/commands/cmd_version.go b/src/commands/cmd_version.go new file mode 100644 index 0000000..70c511f --- /dev/null +++ b/src/commands/cmd_version.go @@ -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" +} diff --git a/src/main.go b/src/main.go index 7546de6..4796414 100644 --- a/src/main.go +++ b/src/main.go @@ -55,6 +55,7 @@ func main() { commands.NewCopyCmd(history), commands.NewSearchCmd(history), commands.NewStatusCmd(), + commands.NewVersionCmd(), ) if err := rootCmd.Execute(); err != nil {