diff --git a/cmd/update/update.go b/cmd/update/update.go index af1e33a..71c333b 100644 --- a/cmd/update/update.go +++ b/cmd/update/update.go @@ -17,6 +17,7 @@ import ( "github.com/anyproto/anytype-cli/core" "github.com/anyproto/anytype-cli/core/output" "github.com/spf13/cobra" + "golang.org/x/mod/semver" ) const ( @@ -48,7 +49,7 @@ func runUpdate(cmd *cobra.Command, args []string) error { currentBase = current[:idx] } - if currentBase >= latest { + if semver.Compare(currentBase, latest) >= 0 { output.Info("Already up to date (%s)", current) return nil } @@ -321,13 +322,26 @@ func replaceBinary(newBinary string) error { } if err := os.Rename(newBinary, currentBinary); err != nil { - if runtime.GOOS != "windows" { + if runtime.GOOS == "windows" { + // Windows locks running exe, so we can't overwrite. + // Rename the current binary first, then replace with the new one. + current := core.GetVersion() + oldBinary := currentBinary + "_" + current + if err := os.Rename(currentBinary, oldBinary); err != nil { + return output.Error("failed to move current binary (permission denied). Try running as administrator: %w", err) + } + if err := os.Rename(newBinary, currentBinary); err != nil { + // Attempt to restore the original + _ = os.Rename(oldBinary, currentBinary) + return output.Error("failed to install new binary: %w", err) + } + + return nil + } else { cmd := exec.Command("mv", newBinary, currentBinary) if err := cmd.Run(); err != nil { return output.Error("failed to replace binary at %s (permission denied). To get the latest version, reinstall from repository: https://github.com/%s/%s", currentBinary, githubOwner, githubRepo) } - } else { - return output.Error("failed to replace binary at %s. To get the latest version, reinstall from repository: https://github.com/%s/%s", currentBinary, githubOwner, githubRepo) } } diff --git a/go.mod b/go.mod index cf2c1ae..9901aae 100644 --- a/go.mod +++ b/go.mod @@ -244,7 +244,7 @@ require ( golang.org/x/crypto v0.47.0 // indirect golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect golang.org/x/image v0.27.0 // indirect - golang.org/x/mod v0.32.0 // indirect + golang.org/x/mod v0.33.0 // indirect golang.org/x/net v0.49.0 // indirect golang.org/x/oauth2 v0.32.0 // indirect golang.org/x/sync v0.19.0 // indirect diff --git a/go.sum b/go.sum index f4169ac..c65c639 100644 --- a/go.sum +++ b/go.sum @@ -1021,8 +1021,8 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c= -golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU= +golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8= +golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=