diff options
| author | s <[email protected]> | 2025-11-04 11:06:35 -0500 |
|---|---|---|
| committer | s <[email protected]> | 2025-11-04 11:06:35 -0500 |
| commit | 4486b6659640102dd542fea007f4c33ac02511ff (patch) | |
| tree | 3e991f3722e3b0062a6078078ff6aa1478c3ab00 /internal/utils/version.go | |
| parent | 3c06eede8ac8cb79272601aad3b2d3359657443a (diff) | |
| download | dborg-4486b6659640102dd542fea007f4c33ac02511ff.tar.gz dborg-4486b6659640102dd542fea007f4c33ac02511ff.zip | |
feat: add version checking and auto-update functionality
Diffstat (limited to 'internal/utils/version.go')
| -rw-r--r-- | internal/utils/version.go | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/internal/utils/version.go b/internal/utils/version.go new file mode 100644 index 0000000..00e6cc2 --- /dev/null +++ b/internal/utils/version.go @@ -0,0 +1,115 @@ +package utils + +import ( + "fmt" + "os" + "os/exec" + "strings" + "syscall" + + "github.com/spf13/cobra" +) + +var Version = "dev" + +func CheckForUpdates(cmd *cobra.Command) error { + if !isTerminal() || Version == "dev" { + return nil + } + + latestVersion, err := getLatestRemoteTag() + if err != nil { + return nil + } + + if latestVersion == "" || latestVersion == Version { + return nil + } + + if isNewerVersion(latestVersion, Version) { + promptAndUpdate(latestVersion) + } + + return nil +} + +func getLatestRemoteTag() (string, error) { + cmd := exec.Command("git", "ls-remote", "--tags", "--refs", "--sort=-v:refname", "git.db.org.ai/dborg") + output, err := cmd.Output() + if err != nil { + return "", err + } + + lines := strings.Split(strings.TrimSpace(string(output)), "\n") + if len(lines) == 0 { + return "", fmt.Errorf("no tags found") + } + + parts := strings.Split(lines[0], "refs/tags/") + if len(parts) < 2 { + return "", fmt.Errorf("invalid tag format") + } + + return strings.TrimSpace(parts[1]), nil +} + +func isNewerVersion(remote, local string) bool { + remote = strings.TrimPrefix(remote, "v") + local = strings.TrimPrefix(local, "v") + + return remote != local && remote > local +} + +func promptAndUpdate(newVersion string) { + fmt.Fprintf(os.Stderr, "\nš A new version of dborg is available: %s (current: %s)\n", newVersion, Version) + fmt.Fprintf(os.Stderr, "Would you like to update now? [Y/n]: ") + + var response string + fmt.Scanln(&response) + + response = strings.ToLower(strings.TrimSpace(response)) + if response != "" && response != "y" && response != "yes" { + fmt.Fprintf(os.Stderr, "Update skipped. Run 'go install git.db.org.ai/dborg@latest' to update manually.\n\n") + return + } + + fmt.Fprintf(os.Stderr, "Updating to %s...\n", newVersion) + + installCmd := exec.Command("go", "install", "git.db.org.ai/dborg") + installCmd.Stdout = os.Stderr + installCmd.Stderr = os.Stderr + + if err := installCmd.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Failed to update: %v\n", err) + fmt.Fprintf(os.Stderr, "Please update manually: go install git.db.org.ai/dborg@latest\n\n") + return + } + + fmt.Fprintf(os.Stderr, "ā Update successful! Restarting...\n\n") + + restartSelf() +} + +func restartSelf() { + executable, err := os.Executable() + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to get executable path: %v\n", err) + os.Exit(1) + } + + args := os.Args[1:] + + err = syscall.Exec(executable, append([]string{executable}, args...), os.Environ()) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to restart: %v\n", err) + os.Exit(1) + } +} + +func isTerminal() bool { + fileInfo, err := os.Stdout.Stat() + if err != nil { + return false + } + return (fileInfo.Mode() & os.ModeCharDevice) != 0 +} |
