From 877c2b2b805105401a107ea3bffc9e3eee95f7d9 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Mon, 27 Jul 2020 00:14:53 +0800 Subject: [PATCH] Update bee when use "bee", not at "bee pro" and detection environment --- internal/app/module/beegopro/container.go | 1 - internal/app/module/beegopro/util.go | 57 -------------------- main.go | 1 + utils/utils.go | 63 +++++++++++++++++++++++ 4 files changed, 64 insertions(+), 58 deletions(-) diff --git a/internal/app/module/beegopro/container.go b/internal/app/module/beegopro/container.go index 0ad4a66..3046698 100644 --- a/internal/app/module/beegopro/container.go +++ b/internal/app/module/beegopro/container.go @@ -51,7 +51,6 @@ 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 49d449b..666bc45 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -4,8 +4,6 @@ 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" @@ -13,7 +11,6 @@ import ( "os" "path" "path/filepath" - "strconv" "strings" "time" ) @@ -229,57 +226,3 @@ func GetSeg(ext string) string { 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() - } else { - 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 diff --git a/main.go b/main.go index ef51e0d..b16d1be 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ var ( ) func main() { + utils.UpdateBee() currentpath, _ := os.Getwd() if workspace != "" { currentpath = workspace diff --git a/utils/utils.go b/utils/utils.go index eedd57a..3f60fad 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -24,6 +24,7 @@ import ( "path/filepath" "regexp" "runtime" + "strconv" "strings" "text/template" "time" @@ -31,6 +32,7 @@ import ( beeLogger "github.com/beego/bee/logger" "github.com/beego/bee/logger/colors" + "github.com/beego/bee/internal/pkg/system" ) func GetBeeWorkPath() string { @@ -463,3 +465,64 @@ func IsGOMODULE() bool { } return false } + +func UpdateBee() { + cmd := exec.Command("go", "version") + cmd.Output() + if cmd.Process == nil || cmd.Process.Pid <= 0 { + beeLogger.Log.Warn("There is no go environment") + return + } + path := system.BeegoHome + fp := path + "/.updateBee" + timeNow := time.Now().Unix() + var timeOld int64 + if 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() + } else { + 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() { + cmd := exec.Command("go", "get", "-u", beePath) + output, err := cmd.Output() + if err != nil { + beeLogger.Log.Warnf("Update Bee err: %s", err) + beeLogger.Log.Warnf("Update Bee err: %s", output) + } + beeLogger.Log.Infof("Bee was updated successfully %s", output) + done <- 1 + }() + // wait 30 second + select { + case <-done: + return + case <-time.After(time.Duration(30 * time.Second)): + beeLogger.Log.Warn("Update Bee timeout!!!") + } +}