From 8782cff9f73dcb6e3364f7069d0325c2b74a22c4 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Sun, 26 Jul 2020 21:26:33 +0800 Subject: [PATCH] Update "bee " every day --- internal/app/module/beegopro/container.go | 1 + internal/app/module/beegopro/util.go | 56 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/internal/app/module/beegopro/container.go b/internal/app/module/beegopro/container.go index 3046698..0ad4a66 100644 --- a/internal/app/module/beegopro/container.go +++ b/internal/app/module/beegopro/container.go @@ -51,6 +51,7 @@ var DefaultBeegoPro = &Container{ } func (c *Container) Run() { + UpdateSelf() // init git refresh cache time c.initTimestamp() c.initUserOption() diff --git a/internal/app/module/beegopro/util.go b/internal/app/module/beegopro/util.go index b2bd05d..f6f7b06 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -4,6 +4,8 @@ import ( "crypto/md5" "errors" "fmt" + "github.com/beego/bee/internal/pkg/command" + "github.com/beego/bee/internal/pkg/system" "github.com/beego/bee/internal/pkg/utils" beeLogger "github.com/beego/bee/logger" "go/format" @@ -11,6 +13,7 @@ import ( "os" "path" "path/filepath" + "strconv" "strings" "time" ) @@ -225,4 +228,57 @@ func GetSeg(ext string) string { default: return "//" } +} + +func UpdateSelf() { + path := system.BeegoHome + fp := path + "/.updateBee" + timeNow := time.Now().Unix() + var timeOld int64 + if utils.IsExist(fp) { + oldContent, err := ioutil.ReadFile(fp) + if err != nil { + beeLogger.Log.Warnf("read file err: %s", err) + } + timeOld, _ = strconv.ParseInt(string(oldContent), 10, 64) + } else { + if cf, err := os.OpenFile(fp, os.O_CREATE, 0644); err == nil { + cf.Close() + beeLogger.Log.Warnf("Create file err: %s", err) + } + } + if timeNow-timeOld > 24*60*60 { + if w, err := os.OpenFile(fp, os.O_WRONLY|os.O_TRUNC, 0644); err == nil { + defer w.Close() + timeNowStr := strconv.FormatInt(timeNow, 10) + if _, err := w.WriteString(timeNowStr); err != nil { + beeLogger.Log.Warnf("Update file err: %s", err) + } + beeLogger.Log.Info("Updating bee") + goGetBee() + } else { + beeLogger.Log.Warnf("Update Bee file err: %s", err) + } + } +} + +func goGetBee() { + beePath := "github.com/beego/bee" + done := make(chan int, 1) + go func() { + stdout, stderr, err := command.ExecCmd("go", "get", "-u", beePath) + if err != nil { + beeLogger.Log.Warnf("Update Bee err: %s", err) + beeLogger.Log.Warnf("Update Bee err: %s", stderr) + } + beeLogger.Log.Infof("Bee was updated successfully %s", stdout) + done <- 1 + }() + // wait 30 second + select { + case <-done: + return + case <-time.After(time.Duration(30 * time.Second)): + beeLogger.Log.Warn("Update Bee timeout!!!") + } } \ No newline at end of file