From fcf57a9d27f8aba266738770e1cd5111f5a93d40 Mon Sep 17 00:00:00 2001 From: "qcqcqc@wsl" <1220204124@zust.edu.cn> Date: Wed, 23 Apr 2025 20:26:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BA=86version=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E8=BF=9B=E8=A1=8C=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend-service/cmd/main.go | 3 +- backend-service/pkg/version/version.go | 4 +- shared/pkg/constants/server.go | 1 - version/api/api.go | 2 + version/cmd/main.go | 5 +- version/pkg/handler/handler.go | 111 ++++++++++++------------- 6 files changed, 62 insertions(+), 64 deletions(-) diff --git a/backend-service/cmd/main.go b/backend-service/cmd/main.go index 308fc2d..242bca7 100644 --- a/backend-service/cmd/main.go +++ b/backend-service/cmd/main.go @@ -101,10 +101,9 @@ var updateCmd = &cobra.Command{ return } - args := os.Args env := os.Environ() - cmd := exec.Command(executable, args[1:]...) + cmd := exec.Command(executable) cmd.Env = env err = cmd.Start() diff --git a/backend-service/pkg/version/version.go b/backend-service/pkg/version/version.go index 10d3d9e..8d7036f 100644 --- a/backend-service/pkg/version/version.go +++ b/backend-service/pkg/version/version.go @@ -50,7 +50,7 @@ func CheckUpdate() (bool, error) { logger.Info("Backup current so-lib to: %s", soBackupPath) // 下载最新的exe,并且校验md5 - newExePath, err := handler.DownloadNewExecutable(constants.SoPath, currentExeBase, newVersion, "exe") + newExePath, err := handler.DownloadNewExecutable(currentExeDir, currentExeBase, newVersion, res.ExeUrl) if err != nil { logger.Error("Failed to download new executable: %v", err) _ = os.Remove(backupPath) @@ -65,7 +65,7 @@ func CheckUpdate() (bool, error) { } // 下载最新的so,并且校验md5 - newSoPath, err := handler.DownloadNewExecutable(constants.SoPath, constants.SoName, newVersion, "so") + newSoPath, err := handler.DownloadNewExecutable(constants.SoPath, constants.SoName, newVersion, res.SoUrl) if err != nil { logger.Error("Failed to download new so-lib: %v", err) // Attempt rollback diff --git a/shared/pkg/constants/server.go b/shared/pkg/constants/server.go index 4b5b693..0ece1d1 100644 --- a/shared/pkg/constants/server.go +++ b/shared/pkg/constants/server.go @@ -10,6 +10,5 @@ const ( MachineInfoApi = "/machine" GetVersionApi = "/version" QuestionStreamApi = "/question" - DownloadNewApi = "/download" GetConfigApi = "/config" ) diff --git a/version/api/api.go b/version/api/api.go index 3115765..a07d260 100644 --- a/version/api/api.go +++ b/version/api/api.go @@ -11,6 +11,8 @@ type Result struct { ExeMd5 string `json:"exeMd5"` SoMd5 string `json:"soMd5"` NeedUpdate bool `json:"needUpdate"` + ExeUrl string `json:"exeUrl"` + SoUrl string `json:"soUrl"` } func GetVersion() (*Result, error) { diff --git a/version/cmd/main.go b/version/cmd/main.go index c0c91e3..373bb26 100644 --- a/version/cmd/main.go +++ b/version/cmd/main.go @@ -4,7 +4,8 @@ import ( "bash_go_service/shared/pkg/constants" "bash_go_service/shared/pkg/logger" "fmt" - "version/pkg/handler" + + // "version/pkg/handler" "github.com/spf13/viper" ) @@ -24,5 +25,5 @@ func main() { logger.Debug("Version: %s", constants.CurrentVersion) - handler.DoUpdate() + // handler.DoUpdate() } diff --git a/version/pkg/handler/handler.go b/version/pkg/handler/handler.go index ce31642..b55e05c 100644 --- a/version/pkg/handler/handler.go +++ b/version/pkg/handler/handler.go @@ -116,63 +116,63 @@ func RemoveUpdateInfo(exePath string) error { } // DoUpdate checks for updates, downloads if needed, and executes the update. -func DoUpdate() { - logger.Debug("start check updating") +// func DoUpdate() { +// logger.Debug("start check updating") - res, err := CheckVersionUpdate() - needUpdate, newVersion, md5sum := res.NeedUpdate, res.Version, res.ExeMd5 - if err != nil { - logger.Error("Failed to check for updates: %v", err) - return - } +// res, err := CheckVersionUpdate() +// needUpdate, newVersion, md5sum := res.NeedUpdate, res.Version, res.ExeMd5 +// if err != nil { +// logger.Error("Failed to check for updates: %v", err) +// return +// } - if !needUpdate { - logger.Info("No update needed.") - return - } +// if !needUpdate { +// logger.Info("No update needed.") +// return +// } - currentExePath, err := os.Executable() - if err != nil { - logger.Error("Failed to get executable path: %v", err) - return - } - currentExeDir := filepath.Dir(currentExePath) - currentExeBase := filepath.Base(currentExePath) +// currentExePath, err := os.Executable() +// if err != nil { +// logger.Error("Failed to get executable path: %v", err) +// return +// } +// currentExeDir := filepath.Dir(currentExePath) +// currentExeBase := filepath.Base(currentExePath) - backupPath, err := BackupCurrentExecutable(currentExePath, currentExeBase, currentExeDir) - if err != nil { - logger.Error("Failed to backup current executable: %v", err) - return - } +// backupPath, err := BackupCurrentExecutable(currentExePath, currentExeBase, currentExeDir) +// if err != nil { +// logger.Error("Failed to backup current executable: %v", err) +// return +// } - // 这只下载exe文件进行更新,如果需要更新其他,可以自定义逻辑 - newExePath, err := DownloadNewExecutable(currentExeDir, currentExeBase, newVersion, "exe") - if err != nil { - logger.Error("Failed to download new executable: %v", err) - // Attempt rollback - _ = os.Rename(backupPath, currentExePath) - return - } +// // 这只下载exe文件进行更新,如果需要更新其他,可以自定义逻辑 +// newExePath, err := DownloadNewExecutable(currentExeDir, currentExeBase, newVersion, "exe") +// if err != nil { +// logger.Error("Failed to download new executable: %v", err) +// // Attempt rollback +// _ = os.Rename(backupPath, currentExePath) +// return +// } - if err := VerifyMD5(newExePath, md5sum); err != nil { - logger.Error("MD5 verification failed: %v", err) - // Attempt rollback - _ = os.Rename(backupPath, currentExePath) - _ = os.Remove(newExePath) - return - } +// if err := VerifyMD5(newExePath, md5sum); err != nil { +// logger.Error("MD5 verification failed: %v", err) +// // Attempt rollback +// _ = os.Rename(backupPath, currentExePath) +// _ = os.Remove(newExePath) +// return +// } - if err := ExecuteUpdate(currentExeDir, backupPath, newExePath, currentExePath, true); err != nil { - logger.Error("Failed to execute update: %v", err) - // Attempt rollback - _ = os.Rename(backupPath, currentExePath) - _ = os.Remove(newExePath) - return - } +// if err := ExecuteUpdate(currentExeDir, backupPath, newExePath, currentExePath, true); err != nil { +// logger.Error("Failed to execute update: %v", err) +// // Attempt rollback +// _ = os.Rename(backupPath, currentExePath) +// _ = os.Remove(newExePath) +// return +// } - logger.Info("Update process initiated, current process exiting.") - os.Exit(0) -} +// logger.Info("Update process initiated, current process exiting.") +// os.Exit(0) +// } // checkVersionUpdate checks if a new version is available. func CheckVersionUpdate() (res *api.Result, err error) { @@ -181,7 +181,6 @@ func CheckVersionUpdate() (res *api.Result, err error) { logger.Error("Failed to get version information: %v", err) return nil, err } - logger.Debug("version response: %v", res) return res, nil } @@ -201,14 +200,12 @@ func BackupCurrentExecutable(exePath, exeBase, exeDir string) (backupPath string } // downloadNewExecutable downloads the new executable file. -func DownloadNewExecutable(exeDir, exeBase, newVersion string, filetype string) (newExePath string, err error) { +func DownloadNewExecutable(exeDir, exeBase, newVersion string, url string) (newExePath string, err error) { + logger.Debug("downloading new executable from: %s", url) + // 这里的url是api的地址 newExePath = filepath.Join(exeDir, exeBase+".new") - cli := client.NewClient() - query := map[string]string{ - "version": newVersion, - "filetype": filetype, - } - err = cli.Download(constants.DownloadNewApi, query, newExePath) + cli := client.NewClient(client.WithBaseURL(url)) + err = cli.Download("", nil, newExePath) if err != nil { logger.Error("Download failed: %v", err) return "", err