chore: better updater

This commit is contained in:
Larvan2 2023-05-17 00:17:23 +08:00
parent e552b5475f
commit 6b1a4385b2
2 changed files with 29 additions and 4 deletions

View File

@ -30,8 +30,9 @@ func upgrade(w http.ResponseWriter, r *http.Request) {
err = updater.Update(execPath) err = updater.Update(execPath)
if err != nil { if err != nil {
log.Warnln("%s", err)
render.Status(r, http.StatusInternalServerError) 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 return
} }

View File

@ -8,6 +8,7 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -74,7 +75,7 @@ func Update(execPath string) (err error) {
log.Infoln("current version %s, latest version %s", constant.Version, latestVersion) log.Infoln("current version %s, latest version %s", constant.Version, latestVersion)
if latestVersion == constant.Version { if latestVersion == constant.Version {
err := &updateError{Message: "Already using latest version"} err := &updateError{Message: "already using latest version"}
return err return err
} }
@ -433,8 +434,12 @@ func getLatestVersion() (version string, err error) {
func updateDownloadURL() { func updateDownloadURL() {
var middle string var middle string
if runtime.GOARCH == "arm" && goarm != "" { if runtime.GOARCH == "arm" && probeGoARM() {
middle = fmt.Sprintf("-%s-%sv%s-%s", runtime.GOOS, runtime.GOARCH, goarm, latestVersion) //-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 != "" { } else if isMIPS(runtime.GOARCH) && gomips != "" {
middle = fmt.Sprintf("-%s-%s-%s-%s", runtime.GOOS, runtime.GOARCH, gomips, latestVersion) middle = fmt.Sprintf("-%s-%s-%s-%s", runtime.GOOS, runtime.GOARCH, gomips, latestVersion)
} else { } else {
@ -463,3 +468,22 @@ func isMIPS(arch string) (ok bool) {
return false 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
}