From 6b1a4385b298251099f25189833234bbd5feb65f Mon Sep 17 00:00:00 2001 From: Larvan2 <78135608+Larvan2@users.noreply.github.com> Date: Wed, 17 May 2023 00:17:23 +0800 Subject: [PATCH] chore: better updater --- hub/route/upgrade.go | 3 ++- hub/updater/updater.go | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/hub/route/upgrade.go b/hub/route/upgrade.go index 0a7c54f9..a58f224c 100644 --- a/hub/route/upgrade.go +++ b/hub/route/upgrade.go @@ -30,8 +30,9 @@ func upgrade(w http.ResponseWriter, r *http.Request) { err = updater.Update(execPath) if err != nil { + log.Warnln("%s", err) render.Status(r, http.StatusInternalServerError) - render.JSON(w, r, newError(fmt.Sprintf("Upgrade: %s", err))) + render.JSON(w, r, newError(fmt.Sprintf("%s", err))) return } diff --git a/hub/updater/updater.go b/hub/updater/updater.go index 9762f3f3..90b14c27 100644 --- a/hub/updater/updater.go +++ b/hub/updater/updater.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "os" + "os/exec" "path/filepath" "runtime" "strings" @@ -74,7 +75,7 @@ func Update(execPath string) (err error) { log.Infoln("current version %s, latest version %s", constant.Version, latestVersion) if latestVersion == constant.Version { - err := &updateError{Message: "Already using latest version"} + err := &updateError{Message: "already using latest version"} return err } @@ -433,8 +434,12 @@ func getLatestVersion() (version string, err error) { func updateDownloadURL() { var middle string - if runtime.GOARCH == "arm" && goarm != "" { - middle = fmt.Sprintf("-%s-%sv%s-%s", runtime.GOOS, runtime.GOARCH, goarm, latestVersion) + if runtime.GOARCH == "arm" && probeGoARM() { + //-linux-armv7-alpha-e552b54.gz + middle = fmt.Sprintf("-%s-%s%s-%s", runtime.GOOS, runtime.GOARCH, goarm, latestVersion) + } else if runtime.GOARCH == "arm64" { + //-linux-arm64-alpha-e552b54.gz + middle = fmt.Sprintf("-%s-%s-%s", runtime.GOOS, runtime.GOARCH, latestVersion) } else if isMIPS(runtime.GOARCH) && gomips != "" { middle = fmt.Sprintf("-%s-%s-%s-%s", runtime.GOOS, runtime.GOARCH, gomips, latestVersion) } else { @@ -463,3 +468,22 @@ func isMIPS(arch string) (ok bool) { return false } } + +// linux only +func probeGoARM() (ok bool) { + cmd := exec.Command("cat", "/proc/cpuinfo") + output, err := cmd.Output() + if err != nil { + log.Errorln("probe goarm error:%s", err) + return false + } + cpuInfo := string(output) + if strings.Contains(cpuInfo, "vfpv3") || strings.Contains(cpuInfo, "vfpv4") { + goarm = "v7" + } else if strings.Contains(cpuInfo, "vfp") { + goarm = "v6" + } else { + goarm = "v5" + } + return true +}