golang的私有化定制自动更新插件
shenwc
2024-11-26 e986f97874f0e4dd12974bae58dd08b5b466b16d
add qiniu
1个文件已添加
79 ■■■■■ 已修改文件
stores/qiniu/qiniu.go 79 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
stores/qiniu/qiniu.go
New file
@@ -0,0 +1,79 @@
package qiniu
import (
    "fmt"
    "runtime"
    "strings"
    "github.com/zan8in/goupdate/progress"
)
type Store struct {
    Owner   string
    Repo    string
    Version string
}
type QiniuResult struct {
    Status        int // update success = 1; have latest version = 2;
    LatestVersion string
}
func init() {
    fmt.Printf("qiniu store %s", "init!")
}
// 更新最新版本
// owner = zan8in
// repo = afrog
// version = 2.8.8 当前版本
func Update(owner, repo, version string) (*QiniuResult, error) {
    var (
        result = &QiniuResult{}
        err    error
    )
    var command string
    switch runtime.GOOS {
    case "windows":
        command = repo + ".exe"
    default:
        command = repo
    }
    s := &Store{
        Owner:   owner,
        Repo:    repo,
        Version: version,
    }
    m := update.Manager{
        Command: command,
    }
    asset, err := s.LatestGitReleases()
    if err != nil {
        if strings.Contains(err.Error(), update.LatestVersionTips) {
            result.LatestVersion = asset.LatestVersion
            result.Status = 2
            return result, nil
        }
        return nil, err
    }
    // download tarball to a tmp dir
    tarball, err := asset.DownloadProxy(progress.Reader)
    if err != nil {
        return nil, fmt.Errorf("error downloading: %s", err)
    }
    // install it
    if err := m.Install(tarball); err != nil {
        return nil, fmt.Errorf("error installing: %s", err)
    }
    // gologger.Info().Msgf("Successfully updated to %s %s\n", command, s.Version)
    result.LatestVersion = asset.LatestVersion
    result.Status = 1
    return result, nil
}