1
0
mirror of https://github.com/beego/bee.git synced 2024-11-23 22:10:54 +00:00

add cmd 'bee update' to update self

just notice once a day if there is a new version
This commit is contained in:
wangle 2020-08-02 16:17:02 +08:00 committed by askuy
parent e0ea0abf5b
commit 9c63635169
9 changed files with 119 additions and 58 deletions

View File

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

View File

@ -65,6 +65,7 @@ func (c *Command) Out() io.Writer {
if c.output != nil {
return *c.output
}
return colors.NewColorWriter(os.Stderr)
}

View File

@ -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
}

View File

@ -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)

View File

@ -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"`

View File

@ -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)

View File

@ -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

View File

@ -25,7 +25,7 @@ import (
)
func main() {
utils.UpdateBee()
utils.NoticeUpdateBee()
flag.Usage = cmd.Usage
flag.Parse()
log.SetFlags(0)

View File

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