1
0
mirror of https://github.com/beego/bee.git synced 2024-11-24 03:20:55 +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/rs"
_ "github.com/beego/bee/cmd/commands/run" _ "github.com/beego/bee/cmd/commands/run"
_ "github.com/beego/bee/cmd/commands/server" _ "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/cmd/commands/version"
"github.com/beego/bee/utils" "github.com/beego/bee/utils"
) )

View File

@ -65,6 +65,7 @@ func (c *Command) Out() io.Writer {
if c.output != nil { if c.output != nil {
return *c.output return *c.output
} }
return colors.NewColorWriter(os.Stderr) 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" "runtime"
"strings" "strings"
"gopkg.in/yaml.v2"
"github.com/beego/bee/cmd/commands" "github.com/beego/bee/cmd/commands"
"github.com/beego/bee/config"
beeLogger "github.com/beego/bee/logger" beeLogger "github.com/beego/bee/logger"
"github.com/beego/bee/logger/colors" "github.com/beego/bee/logger/colors"
"github.com/beego/bee/utils" "github.com/beego/bee/utils"
"gopkg.in/yaml.v2"
) )
const verboseVersionBanner string = `%s%s______ const verboseVersionBanner string = `%s%s______
@ -57,7 +59,7 @@ Prints the current Bee, Beego and Go version alongside the platform information.
} }
var outputFormat string var outputFormat string
const version = "1.12.0" const version = config.Version
func init() { func init() {
fs := flag.NewFlagSet("version", flag.ContinueOnError) fs := flag.NewFlagSet("version", flag.ContinueOnError)

View File

@ -19,12 +19,19 @@ import (
"os" "os"
"path/filepath" "path/filepath"
beeLogger "github.com/beego/bee/logger"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
beeLogger "github.com/beego/bee/logger"
) )
const confVer = 0 const confVer = 0
const (
Version = "1.12.0"
GitRemotePath = "github.com/beego/bee"
)
var Conf = struct { var Conf = struct {
Version int Version int
WatchExts []string `json:"watch_ext" yaml:"watch_ext"` WatchExts []string `json:"watch_ext" yaml:"watch_ext"`

View File

@ -1,16 +1,18 @@
package beegopro package beegopro
import ( 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" "go/format"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "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 // render

View File

@ -4,14 +4,15 @@ import (
"crypto/md5" "crypto/md5"
"errors" "errors"
"fmt" "fmt"
"github.com/beego/bee/internal/pkg/utils"
beeLogger "github.com/beego/bee/logger"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/beego/bee/internal/pkg/utils"
beeLogger "github.com/beego/bee/logger"
) )
// write to file // write to file
@ -191,7 +192,7 @@ func GetFilterContent(content string, seg string) string {
res := "" res := ""
for _, s := range strings.Split(content, "\n") { for _, s := range strings.Split(content, "\n") {
s = strings.TrimSpace(strings.TrimPrefix(s, seg)) s = strings.TrimSpace(strings.TrimPrefix(s, seg))
var have = false var have bool
for _,except := range CompareExcept{ for _,except := range CompareExcept{
if strings.HasPrefix(s, except) { if strings.HasPrefix(s, except) {
have = true have = true

View File

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

View File

@ -30,9 +30,12 @@ import (
"time" "time"
"unicode" "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" beeLogger "github.com/beego/bee/logger"
"github.com/beego/bee/logger/colors" "github.com/beego/bee/logger/colors"
"github.com/beego/bee/internal/pkg/system"
) )
func GetBeeWorkPath() string { func GetBeeWorkPath() string {
@ -466,64 +469,70 @@ func IsGOMODULE() bool {
return false return false
} }
func UpdateBee() { func NoticeUpdateBee() {
cmd := exec.Command("go", "version") cmd := exec.Command("go", "version")
cmd.Output() cmd.Output()
if cmd.Process == nil || cmd.Process.Pid <= 0 { if cmd.Process == nil || cmd.Process.Pid <= 0 {
beeLogger.Log.Warn("There is no go environment") beeLogger.Log.Warn("There is no go environment")
return return
} }
path := system.BeegoHome beeHome := system.BeegoHome
fp := path + "/.updateBee" fp := beeHome + "/.noticeUpdateBee"
timeNow := time.Now().Unix() timeNow := time.Now().Unix()
var timeOld int64 var timeOld int64
if IsExist(fp) { if !IsExist(fp) {
f, err := os.Create(fp)
if err != nil {
beeLogger.Log.Warnf("Create noticeUpdateBee file err: %s", err)
return
}
defer f.Close()
}
oldContent, err := ioutil.ReadFile(fp) oldContent, err := ioutil.ReadFile(fp)
if err != nil { if err != nil {
beeLogger.Log.Warnf("read file err: %s", err) beeLogger.Log.Warnf("Read noticeUpdateBee file err: %s", err)
return
} }
timeOld, _ = strconv.ParseInt(string(oldContent), 10, 64) timeOld, _ = strconv.ParseInt(string(oldContent), 10, 64)
} else { if timeNow-timeOld < 24*60*60 {
if cf, err := os.OpenFile(fp, os.O_CREATE, 0644); err == nil { return
cf.Close()
} else {
beeLogger.Log.Warnf("Create file err: %s", err)
} }
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
} }
if timeNow-timeOld > 24*60*60 {
if w, err := os.OpenFile(fp, os.O_WRONLY|os.O_TRUNC, 0644); err == nil {
defer w.Close() defer w.Close()
timeNowStr := strconv.FormatInt(timeNow, 10) timeNowStr := strconv.FormatInt(timeNow, 10)
if _, err := w.WriteString(timeNowStr); err != nil { if _, err := w.WriteString(timeNowStr); err != nil {
beeLogger.Log.Warnf("Update file err: %s", err) beeLogger.Log.Warnf("Update noticeUpdateBee file err: %s", err)
} return
beeLogger.Log.Info("Updating bee")
goGetBee()
} else {
beeLogger.Log.Warnf("Update Bee file err: %s", err)
}
} }
newVersion()
} }
func goGetBee() { func newVersion() {
beePath := "github.com/beego/bee" workPath := GetBeeWorkPath()
done := make(chan int, 1) repo, err := git.OpenRepository(workPath)
go func() {
cmd := exec.Command("go", "get", "-u", beePath)
output, err := cmd.Output()
if err != nil { if err != nil {
beeLogger.Log.Warnf("Update Bee err: %s", err) beeLogger.Log.Fatalf("Fail to open repository, 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 return
case <-time.After(time.Second * 30): }
beeLogger.Log.Warn("Update Bee timeout! The next automatic update will be in 24 hours.") tags, err := repo.GetTags()
beeLogger.Log.Warn("Or you can update it yourself with `go get -u github.com/beego/bee`") 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")
} }
} }