fix: upgrade backup

This commit is contained in:
Larvan2 2023-03-29 13:59:36 +00:00
parent db7623968d
commit 2fef329319
2 changed files with 35 additions and 28 deletions

View file

@ -268,14 +268,6 @@ jobs:
generate_release_notes: true generate_release_notes: true
body_path: release.txt body_path: release.txt
- name: Push to `MetaCubeX/AlphaBinary`
uses: s0/git-publish-subdir-action@develop
env:
REPO: git@github.com:MetaCubeX/AlphaBinary.git
BRANCH: release
FOLDER: bin
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_PRIVATE_KEY }}
Upload-Release: Upload-Release:
permissions: write-all permissions: write-all
if: ${{ github.ref_type=='tag' }} if: ${{ github.ref_type=='tag' }}

View file

@ -38,10 +38,9 @@ var (
backupDir string // 备份目录 backupDir string // 备份目录
backupExeName string // 备份文件名 backupExeName string // 备份文件名
updateExeName string // 更新后的可执行文件 updateExeName string // 更新后的可执行文件
unpackedFile string
baseURL string = "https://testingcf.jsdelivr.net/gh/MetaCubeX/Clash.Meta@release/clash.meta" baseURL string = "https://github.com/MetaCubeX/Clash.Meta/releases/download/Prerelease-Alpha/clash.meta"
versionURL string = "https://raw.githubusercontent.com/MetaCubeX/Clash.Meta/release/version.txt" versionURL string = "https://github.com/MetaCubeX/Clash.Meta/releases/download/Prerelease-Alpha/version.txt"
packageURL string packageURL string
latestVersion string latestVersion string
) )
@ -57,6 +56,9 @@ func (e *updateError) Error() string {
// Update performs the auto-updater. It returns an error if the updater failed. // Update performs the auto-updater. It returns an error if the updater failed.
// If firstRun is true, it assumes the configuration file doesn't exist. // If firstRun is true, it assumes the configuration file doesn't exist.
func Update() (err error) { func Update() (err error) {
mu.Lock()
defer mu.Unlock()
goos = runtime.GOOS goos = runtime.GOOS
goarch = runtime.GOARCH goarch = runtime.GOARCH
latestVersion, err = getLatestVersion() latestVersion, err = getLatestVersion()
@ -73,8 +75,6 @@ func Update() (err error) {
} }
updateDownloadURL() updateDownloadURL()
mu.Lock()
defer mu.Unlock()
defer func() { defer func() {
if err != nil { if err != nil {
@ -90,7 +90,6 @@ func Update() (err error) {
} }
workDir = filepath.Dir(execPath) workDir = filepath.Dir(execPath)
//log.Infoln("workDir %s", execPath)
err = prepare(execPath) err = prepare(execPath)
if err != nil { if err != nil {
@ -109,6 +108,11 @@ func Update() (err error) {
return fmt.Errorf("unpacking: %w", err) return fmt.Errorf("unpacking: %w", err)
} }
err = backup()
if err != nil {
return fmt.Errorf("replacing: %w", err)
}
err = replace() err = replace()
if err != nil { if err != nil {
return fmt.Errorf("replacing: %w", err) return fmt.Errorf("replacing: %w", err)
@ -136,7 +140,7 @@ func prepare(exePath string) (err error) {
updateExeName = "clash.meta" + "-" + goos + "-" + goarch updateExeName = "clash.meta" + "-" + goos + "-" + goarch
} }
log.Infoln("updateExeName: %s ,currentExeName: %s", updateExeName, currentExeName) log.Infoln("updateExeName: %s ", updateExeName)
backupExeName = filepath.Join(backupDir, filepath.Base(exePath)) backupExeName = filepath.Join(backupDir, filepath.Base(exePath))
updateExeName = filepath.Join(updateDir, updateExeName) updateExeName = filepath.Join(updateDir, updateExeName)
@ -162,13 +166,13 @@ func unpack() error {
log.Debugln("updater: unpacking package") log.Debugln("updater: unpacking package")
if strings.HasSuffix(pkgNameOnly, ".zip") { if strings.HasSuffix(pkgNameOnly, ".zip") {
unpackedFile, err = zipFileUnpack(packageName, updateDir) _, err = zipFileUnpack(packageName, updateDir)
if err != nil { if err != nil {
return fmt.Errorf(".zip unpack failed: %w", err) return fmt.Errorf(".zip unpack failed: %w", err)
} }
} else if strings.HasSuffix(pkgNameOnly, ".gz") { } else if strings.HasSuffix(pkgNameOnly, ".gz") {
unpackedFile, err = gzFileUnpack(packageName, updateDir) _, err = gzFileUnpack(packageName, updateDir)
if err != nil { if err != nil {
return fmt.Errorf(".gz unpack failed: %w", err) return fmt.Errorf(".gz unpack failed: %w", err)
} }
@ -180,25 +184,37 @@ func unpack() error {
return nil return nil
} }
// backup makes a backup of the current configuration and supporting files. It
// ignores the configuration file if firstRun is true.
func backup() (err error) {
log.Infoln("updater: backing up current Exefile")
_ = os.Mkdir(backupDir, 0o755)
err = copyFile(currentExeName, backupExeName)
if err != nil {
return fmt.Errorf("copySupportingFiles(%s, %s) failed: %w", currentExeName, backupExeName, err)
}
return nil
}
// replace moves the current executable with the updated one and also copies the // replace moves the current executable with the updated one and also copies the
// supporting files. // supporting files.
func replace() error { func replace() error {
//err := copySupportingFiles(unpackedFiles, updateDir, workDir) var err error
//if err != nil {
// return fmt.Errorf("copySupportingFiles(%s, %s) failed: %w", updateDir, workDir, err)
//}
log.Infoln("updater: renaming: %s to %s", currentExeName, backupExeName) // log.Infoln("updater: renaming: %s to %s", currentExeName, backupExeName)
err := os.Rename(currentExeName, backupExeName) // err := os.Rename(currentExeName, backupExeName)
if err != nil { // if err != nil {
return err // return err
} // }
if goos == "windows" { if goos == "windows" {
// rename fails with "File in use" error // rename fails with "File in use" error
log.Infoln("copying:%s to %s", updateExeName, currentExeName) log.Infoln("copying: %s to %s", updateExeName, currentExeName)
err = copyFile(updateExeName, currentExeName) err = copyFile(updateExeName, currentExeName)
} else { } else {
log.Infoln("copying: %s to %s", updateExeName, currentExeName)
err = os.Rename(updateExeName, currentExeName) err = os.Rename(updateExeName, currentExeName)
} }
if err != nil { if err != nil {
@ -421,7 +437,6 @@ func getLatestVersion() (version string, err error) {
return "", fmt.Errorf("get Latest Version fail: %w", err) return "", fmt.Errorf("get Latest Version fail: %w", err)
} }
content := strings.TrimRight(string(body), "\n") content := strings.TrimRight(string(body), "\n")
log.Infoln("latest:%s", content)
return content, nil return content, nil
} }