From a668fa45d21cab1c831b2c5ae0b484408c867c2c Mon Sep 17 00:00:00 2001
From: zanbin168 <zanbin168@qq.com>
Date: 星期一, 16 十月 2023 22:42:13 +0800
Subject: [PATCH] update

---
 stores/github/github.go |   65 ++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/stores/github/github.go b/stores/github/github.go
index 33e4e69..bed1f9a 100644
--- a/stores/github/github.go
+++ b/stores/github/github.go
@@ -3,10 +3,13 @@
 
 import (
 	"context"
+	"fmt"
+	"runtime"
 	"time"
 
 	"github.com/google/go-github/github"
 	update "github.com/zan8in/goupdate"
+	"github.com/zan8in/goupdate/progress"
 )
 
 // Store is the store implementation.
@@ -16,6 +19,68 @@
 	Version string
 }
 
+type GithubResult struct {
+	Status        int // update success = 1; have latest version = 2;
+	LatestVersion string
+}
+
+// 更新最新版本
+// owner = zan8in
+// repo = afrog
+// version = 2.8.8 当前版本
+func Update(owner, repo, version string) (*GithubResult, error) {
+	var (
+		result = &GithubResult{}
+		err    error
+	)
+
+	m := &update.Manager{
+		Command: repo + ".exe",
+		Store: &Store{
+			Owner:   owner,
+			Repo:    repo,
+			Version: version,
+		},
+	}
+
+	// fetch the new releases
+	releases, err := m.LatestReleases()
+	if err != nil {
+		return result, fmt.Errorf("error fetching releases: %s", err)
+	}
+
+	// no updates
+	if len(releases) == 0 {
+		result.Status = 2
+		return result, nil
+	}
+
+	// latest release
+	latest := releases[0]
+
+	// find the tarball for this system
+	a := latest.FindZip(runtime.GOOS, runtime.GOARCH)
+	if a == nil {
+		return result, fmt.Errorf("no binary for your system")
+	}
+
+	// download tarball to a tmp dir
+	tarball, err := a.DownloadProxy(progress.Reader)
+	if err != nil {
+		return result, fmt.Errorf("error downloading: %s", err)
+	}
+
+	// install it
+	if err := m.Install(tarball); err != nil {
+		return result, fmt.Errorf("error installing: %s", err)
+	}
+
+	// gologger.Info().Msgf("Successfully updated to %s %s\n", repo, version)
+	result.LatestVersion = version
+	result.Status = 1
+	return result, nil
+}
+
 // GetRelease returns the specified release or ErrNotFound.
 func (s *Store) GetRelease(version string) (*update.Release, error) {
 	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)

--
Gitblit v1.8.0