支持了version接口直接返回下载地址进行下载
This commit is contained in:
parent
b95678b745
commit
fcf57a9d27
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -10,6 +10,5 @@ const (
|
|||
MachineInfoApi = "/machine"
|
||||
GetVersionApi = "/version"
|
||||
QuestionStreamApi = "/question"
|
||||
DownloadNewApi = "/download"
|
||||
GetConfigApi = "/config"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue