1
0
mirror of https://github.com/beego/bee.git synced 2024-11-05 06:50:53 +00:00

Update bee when use "bee", not at "bee pro" and detection environment

This commit is contained in:
wangle 2020-07-27 00:14:53 +08:00
parent 148d8e5f80
commit 877c2b2b80
4 changed files with 64 additions and 58 deletions

View File

@ -51,7 +51,6 @@ var DefaultBeegoPro = &Container{
}
func (c *Container) Run() {
UpdateSelf()
// init git refresh cache time
c.initTimestamp()
c.initUserOption()

View File

@ -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!!!")
}
}

View File

@ -30,6 +30,7 @@ var (
)
func main() {
utils.UpdateBee()
currentpath, _ := os.Getwd()
if workspace != "" {
currentpath = workspace

View File

@ -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!!!")
}
}