From e539c34ea0a96a16376cc295ef8e0427d4591415 Mon Sep 17 00:00:00 2001 From: wangle <285273592@qq.com> Date: Sun, 2 Aug 2020 16:17:02 +0800 Subject: [PATCH] add cmd 'bee update' to update self just notice once a day if there is a new version --- cmd/bee.go | 1 + cmd/commands/command.go | 1 + cmd/commands/update/update.go | 38 ++++++++++ cmd/commands/version/version.go | 6 +- config/conf.go | 9 ++- internal/app/module/beegopro/render.go | 14 ++-- internal/app/module/beegopro/util.go | 7 +- main.go | 2 +- utils/utils.go | 99 ++++++++++++++------------ 9 files changed, 119 insertions(+), 58 deletions(-) create mode 100644 cmd/commands/update/update.go diff --git a/cmd/bee.go b/cmd/bee.go index a6c2c80..8a4f9ec 100644 --- a/cmd/bee.go +++ b/cmd/bee.go @@ -31,6 +31,7 @@ import ( _ "github.com/beego/bee/cmd/commands/rs" _ "github.com/beego/bee/cmd/commands/run" _ "github.com/beego/bee/cmd/commands/server" + _ "github.com/beego/bee/cmd/commands/update" _ "github.com/beego/bee/cmd/commands/version" "github.com/beego/bee/utils" ) diff --git a/cmd/commands/command.go b/cmd/commands/command.go index cab663f..95a1891 100644 --- a/cmd/commands/command.go +++ b/cmd/commands/command.go @@ -65,6 +65,7 @@ func (c *Command) Out() io.Writer { if c.output != nil { return *c.output } + return colors.NewColorWriter(os.Stderr) } diff --git a/cmd/commands/update/update.go b/cmd/commands/update/update.go new file mode 100644 index 0000000..3e5a87a --- /dev/null +++ b/cmd/commands/update/update.go @@ -0,0 +1,38 @@ +package update + +import ( + "flag" + "os" + "os/exec" + + "github.com/beego/bee/cmd/commands" + "github.com/beego/bee/config" + beeLogger "github.com/beego/bee/logger" +) + +var CmdUpdate = &commands.Command{ + UsageLine: "update", + Short: "Update Bee", + Long: ` +Automatic run command "go get -u github.com/beego/bee" for selfupdate +`, + Run: updateBee, +} + +func init() { + fs := flag.NewFlagSet("update", flag.ContinueOnError) + CmdUpdate.Flag = *fs + commands.AvailableCommands = append(commands.AvailableCommands, CmdUpdate) +} + +func updateBee(cmd *commands.Command, args []string) int { + beeLogger.Log.Info("Updating") + beePath := config.GitRemotePath + cmdUp := exec.Command("go", "get", "-u", beePath) + cmdUp.Stdout = os.Stdout + cmdUp.Stderr = os.Stderr + if err := cmdUp.Run(); err != nil { + beeLogger.Log.Warnf("Run cmd err:%s",err) + } + return 0 +} diff --git a/cmd/commands/version/version.go b/cmd/commands/version/version.go index a1ce710..32862a7 100644 --- a/cmd/commands/version/version.go +++ b/cmd/commands/version/version.go @@ -14,11 +14,13 @@ import ( "runtime" "strings" + "gopkg.in/yaml.v2" + "github.com/beego/bee/cmd/commands" + "github.com/beego/bee/config" beeLogger "github.com/beego/bee/logger" "github.com/beego/bee/logger/colors" "github.com/beego/bee/utils" - "gopkg.in/yaml.v2" ) const verboseVersionBanner string = `%s%s______ @@ -57,7 +59,7 @@ Prints the current Bee, Beego and Go version alongside the platform information. } var outputFormat string -const version = "1.12.0" +const version = config.Version func init() { fs := flag.NewFlagSet("version", flag.ContinueOnError) diff --git a/config/conf.go b/config/conf.go index 32bdf2c..e0603e9 100644 --- a/config/conf.go +++ b/config/conf.go @@ -19,12 +19,19 @@ import ( "os" "path/filepath" - beeLogger "github.com/beego/bee/logger" "gopkg.in/yaml.v2" + + beeLogger "github.com/beego/bee/logger" ) const confVer = 0 +const ( + Version = "1.12.0" + GitRemotePath = "github.com/beego/bee" +) + + var Conf = struct { Version int WatchExts []string `json:"watch_ext" yaml:"watch_ext"` diff --git a/internal/app/module/beegopro/render.go b/internal/app/module/beegopro/render.go index 7ca97ae..7ca1464 100644 --- a/internal/app/module/beegopro/render.go +++ b/internal/app/module/beegopro/render.go @@ -1,16 +1,18 @@ package beegopro import ( - "github.com/beego/bee/internal/pkg/system" - beeLogger "github.com/beego/bee/logger" - "github.com/davecgh/go-spew/spew" - "github.com/flosch/pongo2" - "github.com/smartwalle/pongo2render" "go/format" "io/ioutil" "os" "path" "path/filepath" + + "github.com/davecgh/go-spew/spew" + "github.com/flosch/pongo2" + "github.com/smartwalle/pongo2render" + + "github.com/beego/bee/internal/pkg/system" + beeLogger "github.com/beego/bee/logger" ) // render @@ -140,7 +142,7 @@ func (r *RenderFile) Exec(name string) { output = bts } - if FileContentChange(orgContent,output,GetSeg(ext)) { + if FileContentChange(orgContent, output, GetSeg(ext)) { err = r.write(r.FlushFile, output) if err != nil { beeLogger.Log.Fatalf("Could not create file: %s", err) diff --git a/internal/app/module/beegopro/util.go b/internal/app/module/beegopro/util.go index bfef1db..604e404 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -4,14 +4,15 @@ import ( "crypto/md5" "errors" "fmt" - "github.com/beego/bee/internal/pkg/utils" - beeLogger "github.com/beego/bee/logger" "io/ioutil" "os" "path" "path/filepath" "strings" "time" + + "github.com/beego/bee/internal/pkg/utils" + beeLogger "github.com/beego/bee/logger" ) // write to file @@ -191,7 +192,7 @@ func GetFilterContent(content string, seg string) string { res := "" for _, s := range strings.Split(content, "\n") { s = strings.TrimSpace(strings.TrimPrefix(s, seg)) - var have = false + var have bool for _,except := range CompareExcept{ if strings.HasPrefix(s, except) { have = true diff --git a/main.go b/main.go index 112d6c0..251ee51 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,7 @@ import ( ) func main() { - utils.UpdateBee() + utils.NoticeUpdateBee() flag.Usage = cmd.Usage flag.Parse() log.SetFlags(0) diff --git a/utils/utils.go b/utils/utils.go index 969e902..b2056e4 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -30,9 +30,12 @@ import ( "time" "unicode" + "github.com/beego/bee/config" + "github.com/beego/bee/internal/pkg/git" + + "github.com/beego/bee/internal/pkg/system" beeLogger "github.com/beego/bee/logger" "github.com/beego/bee/logger/colors" - "github.com/beego/bee/internal/pkg/system" ) func GetBeeWorkPath() string { @@ -466,64 +469,70 @@ func IsGOMODULE() bool { return false } -func UpdateBee() { +func NoticeUpdateBee() { 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" + beeHome := system.BeegoHome + fp := beeHome + "/.noticeUpdateBee" timeNow := time.Now().Unix() var timeOld int64 - if IsExist(fp) { - oldContent, err := ioutil.ReadFile(fp) + if !IsExist(fp) { + f, err := os.Create(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) + beeLogger.Log.Warnf("Create noticeUpdateBee file err: %s", err) + return } + defer f.Close() } - 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) - } + oldContent, err := ioutil.ReadFile(fp) + if err != nil { + beeLogger.Log.Warnf("Read noticeUpdateBee file err: %s", err) + return } + timeOld, _ = strconv.ParseInt(string(oldContent), 10, 64) + if timeNow-timeOld < 24*60*60 { + return + } + w, err := os.OpenFile(fp, os.O_WRONLY|os.O_TRUNC, 0644) + if err != nil { + beeLogger.Log.Warnf("Open noticeUpdateBee file err: %s", err) + return + } + defer w.Close() + timeNowStr := strconv.FormatInt(timeNow, 10) + if _, err := w.WriteString(timeNowStr); err != nil { + beeLogger.Log.Warnf("Update noticeUpdateBee file err: %s", err) + return + } + newVersion() } -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: +func newVersion() { + workPath := GetBeeWorkPath() + repo, err := git.OpenRepository(workPath) + if err != nil { + beeLogger.Log.Fatalf("Fail to open repository, err: %s", err) return - case <-time.After(time.Second * 30): - beeLogger.Log.Warn("Update Bee timeout! The next automatic update will be in 24 hours.") - beeLogger.Log.Warn("Or you can update it yourself with `go get -u github.com/beego/bee`") + } + tags, err := repo.GetTags() + if err != nil { + beeLogger.Log.Fatalf("Fail to get tags, err: %s", err) + return + } + // v1.12.0 | V_1.12.0 => 1.12.0 + re, _ := regexp.Compile(`[0-9.]+`) + var versionLast string + versionList := re.FindStringSubmatch(tags[0]) + if len(versionList) >= 1 { + versionLast = versionList[0] + } + versionNow := config.Version + if versionNow != versionLast { + beeLogger.Log.Warnf("Update available %s ==> %s", versionNow, versionLast) + beeLogger.Log.Warn("Run `bee update` to update") } }