diff options
| author | sinner <[email protected]> | 2026-04-15 15:16:02 -0400 |
|---|---|---|
| committer | sinner <[email protected]> | 2026-04-15 15:16:02 -0400 |
| commit | a5f907854f29e1c267ad30d1dfe85c2c47f5ac48 (patch) | |
| tree | bc8685c3b22e6d5d47702ba0607c694f938ba7fd /internal/utils/version.go | |
| parent | 8a1cf20dd5014ebe15ced77344902b79dcfa2e66 (diff) | |
| download | dborg-a5f907854f29e1c267ad30d1dfe85c2c47f5ac48.tar.gz dborg-a5f907854f29e1c267ad30d1dfe85c2c47f5ac48.zip | |
Diffstat (limited to 'internal/utils/version.go')
| -rw-r--r-- | internal/utils/version.go | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/internal/utils/version.go b/internal/utils/version.go index 46ebb73..ab72bfb 100644 --- a/internal/utils/version.go +++ b/internal/utils/version.go @@ -4,7 +4,9 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "runtime/debug" + "strconv" "strings" "syscall" @@ -83,10 +85,21 @@ func isNewerVersion(remote, local string) bool { localParts := strings.Split(local, ".") for i := 0; i < len(remoteParts) && i < len(localParts); i++ { - if remoteParts[i] > localParts[i] { + r, errR := strconv.Atoi(remoteParts[i]) + l, errL := strconv.Atoi(localParts[i]) + if errR != nil || errL != nil { + if remoteParts[i] > localParts[i] { + return true + } + if remoteParts[i] < localParts[i] { + return false + } + continue + } + if r > l { return true } - if remoteParts[i] < localParts[i] { + if r < l { return false } } @@ -125,26 +138,36 @@ func promptAndUpdate(newVersion string) { restartSelf() } +func resolveInstalledBinary() (string, error) { + if gobin := os.Getenv("GOBIN"); gobin != "" { + return filepath.Join(gobin, "dborg"), nil + } + + gopath := os.Getenv("GOPATH") + if gopath == "" { + out, err := exec.Command("go", "env", "GOPATH").Output() + if err != nil { + return "", fmt.Errorf("could not determine GOPATH: %w", err) + } + gopath = strings.TrimSpace(string(out)) + } + + return filepath.Join(gopath, "bin", "dborg"), nil +} + func restartSelf() { - executable, err := os.Executable() + newBinary, err := resolveInstalledBinary() if err != nil { - fmt.Fprintf(os.Stderr, "Failed to get executable path: %v\n", err) + fmt.Fprintf(os.Stderr, "Failed to locate installed binary: %v\n", err) os.Exit(1) } args := os.Args[1:] - err = syscall.Exec(executable, append([]string{executable}, args...), os.Environ()) + err = syscall.Exec(newBinary, append([]string{newBinary}, 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 -} |
