1 Star 0 Fork 0

hh / iris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
version.go 2.31 KB
一键复制 编辑 原始数据 按行查看 历史
package iris
import (
"bufio"
"encoding/json"
"io/ioutil"
"net/url"
"os"
"os/exec"
"sync"
"time"
"github.com/kataras/golog"
"github.com/kataras/iris/core/netutil"
)
var checkVersionOnce = sync.Once{}
// CheckVersion checks for any available updates.
func CheckVersion() {
checkVersionOnce.Do(func() {
checkVersion()
})
}
type versionInfo struct {
Version string `json:"version"`
ChangelogURL string `json:"changelog_url"`
UpdateAvailable bool `json:"update_available"`
}
func checkVersion() {
client := netutil.Client(20 * time.Second)
r, err := client.PostForm("https://iris-go.com/version", url.Values{"current_version": {Version}})
if err != nil {
golog.Debugf("%v", err)
return
}
defer r.Body.Close()
if r.StatusCode >= 400 {
return
}
b, err := ioutil.ReadAll(r.Body)
if len(b) == 0 || err != nil {
golog.Debugf("%v", err)
return
}
v := new(versionInfo)
if err := json.Unmarshal(b, v); err != nil {
golog.Debugf("error while unmarshal the response body: %v", err)
return
}
if !v.UpdateAvailable {
return
}
format := "A new version is available online[%s > %s].\n"
if v.ChangelogURL != "" {
format += "Release notes: %s\n"
}
format += "Update now?[%s]: "
// currentVersion.LessThan(latestVersion)
updaterYesInput := [...]string{"y", "yes"}
golog.Warnf(format, v.Version, Version,
v.ChangelogURL,
updaterYesInput[0]+"/n")
silent := false
sc := bufio.NewScanner(os.Stdin)
shouldUpdate := silent
if !silent {
if sc.Scan() {
inputText := sc.Text()
for _, s := range updaterYesInput {
if inputText == s {
shouldUpdate = true
}
}
}
}
if !shouldUpdate {
golog.Infof("Ignore updates? Disable version checker via:\napp.Run(..., iris.WithoutVersionChecker)")
return
}
if shouldUpdate {
repo := "github.com/kataras/iris/..."
cmd := exec.Command("go", "get", "-u", "-v", repo)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
golog.Warnf("unexpected message while trying to go get,\nif you edited the original source code then you've to remove the whole $GOPATH/src/github.com/kataras folder and execute `go get -u github.com/kataras/iris/...` manually\n%v", err)
return
}
golog.Infof("Update process finished.\nManual rebuild and restart is required to apply the changes...")
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/w1229748769/iris.git
git@gitee.com:w1229748769/iris.git
w1229748769
iris
iris
v8.5.0

搜索帮助

344bd9b3 5694891 D2dac590 5694891