| New file |
| | |
| | | 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 |
| | | } |