支持了version接口直接返回下载地址进行下载

This commit is contained in:
Pan Qiancheng 2025-04-23 20:26:47 +08:00
parent b95678b745
commit fcf57a9d27
6 changed files with 62 additions and 64 deletions

View File

@ -101,10 +101,9 @@ var updateCmd = &cobra.Command{
return return
} }
args := os.Args
env := os.Environ() env := os.Environ()
cmd := exec.Command(executable, args[1:]...) cmd := exec.Command(executable)
cmd.Env = env cmd.Env = env
err = cmd.Start() err = cmd.Start()

View File

@ -50,7 +50,7 @@ func CheckUpdate() (bool, error) {
logger.Info("Backup current so-lib to: %s", soBackupPath) logger.Info("Backup current so-lib to: %s", soBackupPath)
// 下载最新的exe并且校验md5 // 下载最新的exe并且校验md5
newExePath, err := handler.DownloadNewExecutable(constants.SoPath, currentExeBase, newVersion, "exe") newExePath, err := handler.DownloadNewExecutable(currentExeDir, currentExeBase, newVersion, res.ExeUrl)
if err != nil { if err != nil {
logger.Error("Failed to download new executable: %v", err) logger.Error("Failed to download new executable: %v", err)
_ = os.Remove(backupPath) _ = os.Remove(backupPath)
@ -65,7 +65,7 @@ func CheckUpdate() (bool, error) {
} }
// 下载最新的so并且校验md5 // 下载最新的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 { if err != nil {
logger.Error("Failed to download new so-lib: %v", err) logger.Error("Failed to download new so-lib: %v", err)
// Attempt rollback // Attempt rollback

View File

@ -10,6 +10,5 @@ const (
MachineInfoApi = "/machine" MachineInfoApi = "/machine"
GetVersionApi = "/version" GetVersionApi = "/version"
QuestionStreamApi = "/question" QuestionStreamApi = "/question"
DownloadNewApi = "/download"
GetConfigApi = "/config" GetConfigApi = "/config"
) )

View File

@ -11,6 +11,8 @@ type Result struct {
ExeMd5 string `json:"exeMd5"` ExeMd5 string `json:"exeMd5"`
SoMd5 string `json:"soMd5"` SoMd5 string `json:"soMd5"`
NeedUpdate bool `json:"needUpdate"` NeedUpdate bool `json:"needUpdate"`
ExeUrl string `json:"exeUrl"`
SoUrl string `json:"soUrl"`
} }
func GetVersion() (*Result, error) { func GetVersion() (*Result, error) {

View File

@ -4,7 +4,8 @@ import (
"bash_go_service/shared/pkg/constants" "bash_go_service/shared/pkg/constants"
"bash_go_service/shared/pkg/logger" "bash_go_service/shared/pkg/logger"
"fmt" "fmt"
"version/pkg/handler"
// "version/pkg/handler"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -24,5 +25,5 @@ func main() {
logger.Debug("Version: %s", constants.CurrentVersion) logger.Debug("Version: %s", constants.CurrentVersion)
handler.DoUpdate() // handler.DoUpdate()
} }

View File

@ -116,63 +116,63 @@ func RemoveUpdateInfo(exePath string) error {
} }
// DoUpdate checks for updates, downloads if needed, and executes the update. // DoUpdate checks for updates, downloads if needed, and executes the update.
func DoUpdate() { // func DoUpdate() {
logger.Debug("start check updating") // logger.Debug("start check updating")
res, err := CheckVersionUpdate() // res, err := CheckVersionUpdate()
needUpdate, newVersion, md5sum := res.NeedUpdate, res.Version, res.ExeMd5 // needUpdate, newVersion, md5sum := res.NeedUpdate, res.Version, res.ExeMd5
if err != nil { // if err != nil {
logger.Error("Failed to check for updates: %v", err) // logger.Error("Failed to check for updates: %v", err)
return // return
} // }
if !needUpdate { // if !needUpdate {
logger.Info("No update needed.") // logger.Info("No update needed.")
return // return
} // }
currentExePath, err := os.Executable() // currentExePath, err := os.Executable()
if err != nil { // if err != nil {
logger.Error("Failed to get executable path: %v", err) // logger.Error("Failed to get executable path: %v", err)
return // return
} // }
currentExeDir := filepath.Dir(currentExePath) // currentExeDir := filepath.Dir(currentExePath)
currentExeBase := filepath.Base(currentExePath) // currentExeBase := filepath.Base(currentExePath)
backupPath, err := BackupCurrentExecutable(currentExePath, currentExeBase, currentExeDir) // backupPath, err := BackupCurrentExecutable(currentExePath, currentExeBase, currentExeDir)
if err != nil { // if err != nil {
logger.Error("Failed to backup current executable: %v", err) // logger.Error("Failed to backup current executable: %v", err)
return // return
} // }
// 这只下载exe文件进行更新如果需要更新其他可以自定义逻辑 // // 这只下载exe文件进行更新如果需要更新其他可以自定义逻辑
newExePath, err := DownloadNewExecutable(currentExeDir, currentExeBase, newVersion, "exe") // newExePath, err := DownloadNewExecutable(currentExeDir, currentExeBase, newVersion, "exe")
if err != nil { // if err != nil {
logger.Error("Failed to download new executable: %v", err) // logger.Error("Failed to download new executable: %v", err)
// Attempt rollback // // Attempt rollback
_ = os.Rename(backupPath, currentExePath) // _ = os.Rename(backupPath, currentExePath)
return // return
} // }
if err := VerifyMD5(newExePath, md5sum); err != nil { // if err := VerifyMD5(newExePath, md5sum); err != nil {
logger.Error("MD5 verification failed: %v", err) // logger.Error("MD5 verification failed: %v", err)
// Attempt rollback // // Attempt rollback
_ = os.Rename(backupPath, currentExePath) // _ = os.Rename(backupPath, currentExePath)
_ = os.Remove(newExePath) // _ = os.Remove(newExePath)
return // return
} // }
if err := ExecuteUpdate(currentExeDir, backupPath, newExePath, currentExePath, true); err != nil { // if err := ExecuteUpdate(currentExeDir, backupPath, newExePath, currentExePath, true); err != nil {
logger.Error("Failed to execute update: %v", err) // logger.Error("Failed to execute update: %v", err)
// Attempt rollback // // Attempt rollback
_ = os.Rename(backupPath, currentExePath) // _ = os.Rename(backupPath, currentExePath)
_ = os.Remove(newExePath) // _ = os.Remove(newExePath)
return // return
} // }
logger.Info("Update process initiated, current process exiting.") // logger.Info("Update process initiated, current process exiting.")
os.Exit(0) // os.Exit(0)
} // }
// checkVersionUpdate checks if a new version is available. // checkVersionUpdate checks if a new version is available.
func CheckVersionUpdate() (res *api.Result, err error) { 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) logger.Error("Failed to get version information: %v", err)
return nil, err return nil, err
} }
logger.Debug("version response: %v", res)
return res, nil return res, nil
} }
@ -201,14 +200,12 @@ func BackupCurrentExecutable(exePath, exeBase, exeDir string) (backupPath string
} }
// downloadNewExecutable downloads the new executable file. // 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") newExePath = filepath.Join(exeDir, exeBase+".new")
cli := client.NewClient() cli := client.NewClient(client.WithBaseURL(url))
query := map[string]string{ err = cli.Download("", nil, newExePath)
"version": newVersion,
"filetype": filetype,
}
err = cli.Download(constants.DownloadNewApi, query, newExePath)
if err != nil { if err != nil {
logger.Error("Download failed: %v", err) logger.Error("Download failed: %v", err)
return "", err return "", err