mirror of
https://github.com/beego/bee.git
synced 2024-11-24 03:20:55 +00:00
Automatic update bee every day
Backup only when content changes Fix the version
This commit is contained in:
parent
c562cedf96
commit
e0ea0abf5b
@ -57,7 +57,7 @@ Prints the current Bee, Beego and Go version alongside the platform information.
|
||||
}
|
||||
var outputFormat string
|
||||
|
||||
const version = "1.11.0"
|
||||
const version = "1.12.0"
|
||||
|
||||
func init() {
|
||||
fs := flag.NewFlagSet("version", flag.ContinueOnError)
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
var CompareExcept = []string{"@BeeGenerateTime"}
|
||||
func (c *Container) GenConfig() {
|
||||
if utils.IsExist(c.BeegoProFile) {
|
||||
beeLogger.Log.Fatalf("beego pro toml exist")
|
||||
|
@ -6,6 +6,9 @@ import (
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/flosch/pongo2"
|
||||
"github.com/smartwalle/pongo2render"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
@ -114,10 +117,36 @@ func (r *RenderFile) Exec(name string) {
|
||||
beeLogger.Log.Fatalf("Could not create the %s render tmpl: %s", name, err)
|
||||
return
|
||||
}
|
||||
err = r.write(r.FlushFile, buf)
|
||||
_, err = os.Stat(r.Descriptor.DstPath)
|
||||
var orgContent []byte
|
||||
if err == nil {
|
||||
if org, err := os.OpenFile(r.Descriptor.DstPath, os.O_RDONLY, 0666); err == nil {
|
||||
orgContent,_ = ioutil.ReadAll(org)
|
||||
org.Close()
|
||||
} else {
|
||||
beeLogger.Log.Infof("file err %s", err)
|
||||
}
|
||||
}
|
||||
// Replace or create when content changes
|
||||
output := []byte(buf)
|
||||
ext := filepath.Ext(r.FlushFile)
|
||||
if r.Option.EnableFormat && ext == ".go" {
|
||||
// format code
|
||||
var bts []byte
|
||||
bts, err = format.Source([]byte(buf))
|
||||
if err != nil {
|
||||
beeLogger.Log.Warnf("format buf error %s", err.Error())
|
||||
}
|
||||
output = bts
|
||||
}
|
||||
|
||||
if FileContentChange(orgContent,output,GetSeg(ext)) {
|
||||
err = r.write(r.FlushFile, output)
|
||||
if err != nil {
|
||||
beeLogger.Log.Fatalf("Could not create file: %s", err)
|
||||
return
|
||||
}
|
||||
beeLogger.Log.Infof("create file '%s' from %s", r.FlushFile, r.PackageName)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
package beegopro
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/beego/bee/internal/pkg/utils"
|
||||
beeLogger "github.com/beego/bee/logger"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
@ -15,12 +15,12 @@ import (
|
||||
)
|
||||
|
||||
// write to file
|
||||
func (c *RenderFile) write(filename string, buf string) (err error) {
|
||||
func (c *RenderFile) write(filename string, buf []byte) (err error) {
|
||||
if utils.IsExist(filename) && !isNeedOverwrite(filename) {
|
||||
return
|
||||
}
|
||||
|
||||
filePath := path.Dir(filename)
|
||||
filePath := filepath.Dir(filename)
|
||||
err = createPath(filePath)
|
||||
if err != nil {
|
||||
err = errors.New("write create path " + err.Error())
|
||||
@ -37,7 +37,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) {
|
||||
name := path.Base(filename)
|
||||
|
||||
if utils.IsExist(filename) {
|
||||
bakName := fmt.Sprintf("%s/%s.%s.bak", filePathBak, name, time.Now().Format("2006.01.02.15.04.05"))
|
||||
bakName := fmt.Sprintf("%s/%s.%s.bak", filePathBak, filepath.Base(name), time.Now().Format("2006.01.02.15.04.05"))
|
||||
beeLogger.Log.Infof("bak file '%s'", bakName)
|
||||
if err := os.Rename(filename, bakName); err != nil {
|
||||
err = errors.New("file is bak error, path is " + bakName)
|
||||
@ -57,20 +57,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
output := []byte(buf)
|
||||
|
||||
if c.Option.EnableFormat && filepath.Ext(filename) == ".go" {
|
||||
// format code
|
||||
var bts []byte
|
||||
bts, err = format.Source([]byte(buf))
|
||||
if err != nil {
|
||||
err = errors.New("format buf error " + err.Error())
|
||||
return
|
||||
}
|
||||
output = bts
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(filename, output, 0644)
|
||||
err = ioutil.WriteFile(filename, buf, 0644)
|
||||
if err != nil {
|
||||
err = errors.New("write write file " + err.Error())
|
||||
return
|
||||
@ -79,11 +66,7 @@ func (c *RenderFile) write(filename string, buf string) (err error) {
|
||||
}
|
||||
|
||||
func isNeedOverwrite(fileName string) (flag bool) {
|
||||
seg := "//"
|
||||
ext := filepath.Ext(fileName)
|
||||
if ext == ".sql" {
|
||||
seg = "--"
|
||||
}
|
||||
seg := GetSeg(filepath.Ext(fileName))
|
||||
|
||||
f, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
@ -188,3 +171,44 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func FileContentChange(org,new []byte, seg string) bool {
|
||||
if len(org) == 0 {
|
||||
return true
|
||||
}
|
||||
orgContent := GetFilterContent(string(org),seg)
|
||||
newContent := GetFilterContent(string(new),seg)
|
||||
orgMd5 := md5.Sum([]byte(orgContent))
|
||||
newMd5:= md5.Sum([]byte(newContent))
|
||||
if orgMd5 != newMd5 {
|
||||
return true
|
||||
}
|
||||
beeLogger.Log.Infof("File has no change in the content")
|
||||
return false
|
||||
}
|
||||
|
||||
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
|
||||
for _,except := range CompareExcept{
|
||||
if strings.HasPrefix(s, except) {
|
||||
have = true
|
||||
}
|
||||
}
|
||||
if !have {
|
||||
res += s
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func GetSeg(ext string) string {
|
||||
switch ext {
|
||||
case ".sql":
|
||||
return "--"
|
||||
default:
|
||||
return "//"
|
||||
}
|
||||
}
|
||||
|
1
main.go
1
main.go
@ -25,6 +25,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
utils.UpdateBee()
|
||||
flag.Usage = cmd.Usage
|
||||
flag.Parse()
|
||||
log.SetFlags(0)
|
||||
|
@ -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,65 @@ 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.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`")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user