Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup only when content changes #697

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/commands/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 35 additions & 5 deletions internal/app/module/beegopro/render.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package beegopro

import (
"errors"
"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"
)
Expand Down Expand Up @@ -114,10 +118,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)
if err != nil {
beeLogger.Log.Fatalf("Could not create file: %s", err)
return
_, 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)
if r.Option.EnableFormat && filepath.Ext(r.FlushFile) == ".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
}

if FileContentChange(orgContent,output) {
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)
}
beeLogger.Log.Infof("create file '%s' from %s", r.FlushFile, r.PackageName)
}

48 changes: 30 additions & 18 deletions internal/app/module/beegopro/util.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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())
Expand All @@ -37,9 +37,10 @@ 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 {
fmt.Println(err)
err = errors.New("file is bak error, path is " + bakName)
return err
}
Expand All @@ -52,20 +53,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
Expand Down Expand Up @@ -183,3 +171,27 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) {
}
return
}

func FileContentChange(org,new []byte) bool {
if len(org) == 0 {
return true
}
var orgContent,newContent string
for i, s := range strings.Split(string(org), "\n") {
if i > 1 {
orgContent += s
}
}
for i, s := range strings.Split(string(new), "\n") {
if i > 1 {
newContent += s
}
}
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
}