From e0fd23700212b2110d0f042fc57766a552cb5f33 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Fri, 9 Oct 2020 23:33:58 +0800 Subject: [PATCH] add dev githook command --- cmd/bee.go | 1 + cmd/commands/api/apiapp.go | 3 +- cmd/commands/beegopro/beegopro.go | 3 +- cmd/commands/dev/cmd.go | 56 +++++++++++++++++++ cmd/commands/dev/githook.go | 47 ++++++++++++++++ cmd/commands/dlv/dlv_amd64.go | 4 +- cmd/commands/hprose/hprose.go | 3 +- cmd/commands/update/update.go | 2 +- config/conf.go | 3 +- internal/app/module/beegopro/config.go | 5 +- internal/app/module/beegopro/container.go | 7 ++- internal/app/module/beegopro/migration.go | 5 +- internal/app/module/beegopro/parser_mysql.go | 1 + internal/app/module/beegopro/pongo2.go | 5 +- internal/app/module/beegopro/render.go | 3 +- internal/app/module/beegopro/schema.go | 7 ++- internal/app/module/beegopro/schema_model.go | 3 +- .../app/module/beegopro/schema_mysql_model.go | 1 + internal/app/module/beegopro/util.go | 10 ++-- internal/pkg/git/repository.go | 7 ++- internal/pkg/utils/file.go | 3 +- utils/utils.go | 4 +- 22 files changed, 150 insertions(+), 33 deletions(-) create mode 100644 cmd/commands/dev/cmd.go create mode 100644 cmd/commands/dev/githook.go diff --git a/cmd/bee.go b/cmd/bee.go index 8a4f9ec..894aae9 100644 --- a/cmd/bee.go +++ b/cmd/bee.go @@ -21,6 +21,7 @@ import ( _ "github.com/beego/bee/cmd/commands/bale" _ "github.com/beego/bee/cmd/commands/beefix" _ "github.com/beego/bee/cmd/commands/beegopro" + _ "github.com/beego/bee/cmd/commands/dev" _ "github.com/beego/bee/cmd/commands/dlv" _ "github.com/beego/bee/cmd/commands/dockerize" _ "github.com/beego/bee/cmd/commands/generate" diff --git a/cmd/commands/api/apiapp.go b/cmd/commands/api/apiapp.go index 4ecbc6a..f9553f6 100644 --- a/cmd/commands/api/apiapp.go +++ b/cmd/commands/api/apiapp.go @@ -16,11 +16,12 @@ package apiapp import ( "fmt" - "github.com/beego/bee/logger/colors" "os" path "path/filepath" "strings" + "github.com/beego/bee/logger/colors" + "github.com/beego/bee/cmd/commands" "github.com/beego/bee/cmd/commands/version" "github.com/beego/bee/generate" diff --git a/cmd/commands/beegopro/beegopro.go b/cmd/commands/beegopro/beegopro.go index e30567e..b094a0a 100644 --- a/cmd/commands/beegopro/beegopro.go +++ b/cmd/commands/beegopro/beegopro.go @@ -14,10 +14,11 @@ package beegopro import ( + "strings" + "github.com/beego/bee/cmd/commands" "github.com/beego/bee/internal/app/module/beegopro" "github.com/beego/bee/logger" - "strings" ) var CmdBeegoPro = &commands.Command{ diff --git a/cmd/commands/dev/cmd.go b/cmd/commands/dev/cmd.go new file mode 100644 index 0000000..dda319a --- /dev/null +++ b/cmd/commands/dev/cmd.go @@ -0,0 +1,56 @@ +// Copyright 2020 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package dev + +import ( + "github.com/beego/bee/cmd/commands" + beeLogger "github.com/beego/bee/logger" +) + +var CmdDev = &commands.Command{ + CustomFlags: true, + UsageLine: "dev [command]", + Short: "Commands which used to help to develop beego and bee", + Long: ` +Commands that help developer develop, build and test beego. +- githook Prepare githooks +`, + Run: Run, +} + +func init() { + commands.AvailableCommands = append(commands.AvailableCommands, CmdDev) +} + +func Run(cmd *commands.Command, args []string) int { + if len(args) < 1 { + beeLogger.Log.Fatal("Command is missing") + } + + if len(args) >= 2 { + cmd.Flag.Parse(args[1:]) + } + + gcmd := args[0] + + switch gcmd { + + case "githook": + initGitHook() + default: + beeLogger.Log.Fatal("Unknown command") + } + return 0 +} diff --git a/cmd/commands/dev/githook.go b/cmd/commands/dev/githook.go new file mode 100644 index 0000000..be0d18a --- /dev/null +++ b/cmd/commands/dev/githook.go @@ -0,0 +1,47 @@ +// Copyright 2020 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package dev + +import ( + "os" + + beeLogger "github.com/beego/bee/logger" +) + +var preCommit = ` +goimports -w -format-only ./ \ +ineffassign . \ +staticcheck -show-ignored -checks "-ST1017,-U1000,-ST1005,-S1034,-S1012,-SA4006,-SA6005,-SA1019,-SA1024" ./ \ +` + +// for now, we simply override pre-commit file +func initGitHook() { + // pcf => pre-commit file + pcfPath := "./.git/hooks/pre-commit" + pcf, err := os.OpenFile(pcfPath, os.O_RDWR|os.O_CREATE, 0777) + if err != nil { + beeLogger.Log.Errorf("try to create or open file failed: %s, cause: %s", pcfPath, err.Error()) + return + } + + defer pcf.Close() + _, err = pcf.Write(([]byte)(preCommit)) + + if err != nil { + beeLogger.Log.Errorf("could not init githooks: %s", err.Error()) + } else { + beeLogger.Log.Successf("The githooks has been added, the content is:\n %s ", preCommit) + } +} diff --git a/cmd/commands/dlv/dlv_amd64.go b/cmd/commands/dlv/dlv_amd64.go index 86eee8a..dc91816 100644 --- a/cmd/commands/dlv/dlv_amd64.go +++ b/cmd/commands/dlv/dlv_amd64.go @@ -28,12 +28,12 @@ import ( "github.com/beego/bee/cmd/commands/version" beeLogger "github.com/beego/bee/logger" "github.com/beego/bee/utils" + "github.com/fsnotify/fsnotify" + "github.com/gadelkareem/delve/pkg/terminal" "github.com/gadelkareem/delve/service" "github.com/gadelkareem/delve/service/debugger" "github.com/gadelkareem/delve/service/rpc2" "github.com/gadelkareem/delve/service/rpccommon" - "github.com/gadelkareem/delve/pkg/terminal" - "github.com/fsnotify/fsnotify" ) var cmdDlv = &commands.Command{ diff --git a/cmd/commands/hprose/hprose.go b/cmd/commands/hprose/hprose.go index bdd52c8..7285260 100644 --- a/cmd/commands/hprose/hprose.go +++ b/cmd/commands/hprose/hprose.go @@ -2,11 +2,12 @@ package hprose import ( "fmt" - "github.com/beego/bee/logger/colors" "os" "path" "strings" + "github.com/beego/bee/logger/colors" + "github.com/beego/bee/cmd/commands" "github.com/beego/bee/cmd/commands/api" "github.com/beego/bee/cmd/commands/version" diff --git a/cmd/commands/update/update.go b/cmd/commands/update/update.go index 3e5a87a..2c8ff8a 100644 --- a/cmd/commands/update/update.go +++ b/cmd/commands/update/update.go @@ -32,7 +32,7 @@ func updateBee(cmd *commands.Command, args []string) int { cmdUp.Stdout = os.Stdout cmdUp.Stderr = os.Stderr if err := cmdUp.Run(); err != nil { - beeLogger.Log.Warnf("Run cmd err:%s",err) + beeLogger.Log.Warnf("Run cmd err:%s", err) } return 0 } diff --git a/config/conf.go b/config/conf.go index 4f150aa..e0a7fec 100644 --- a/config/conf.go +++ b/config/conf.go @@ -27,11 +27,10 @@ import ( const confVer = 0 const ( - Version = "2.0.0" + Version = "2.0.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/config.go b/internal/app/module/beegopro/config.go index 7fbdc2c..b305d02 100644 --- a/internal/app/module/beegopro/config.go +++ b/internal/app/module/beegopro/config.go @@ -1,11 +1,14 @@ package beegopro import ( + "io/ioutil" + "github.com/beego/bee/internal/pkg/utils" 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") diff --git a/internal/app/module/beegopro/container.go b/internal/app/module/beegopro/container.go index 3046698..69c9084 100644 --- a/internal/app/module/beegopro/container.go +++ b/internal/app/module/beegopro/container.go @@ -2,6 +2,10 @@ package beegopro import ( "fmt" + "io/ioutil" + "sync" + "time" + "github.com/beego/bee/internal/pkg/git" "github.com/beego/bee/internal/pkg/system" beeLogger "github.com/beego/bee/logger" @@ -9,9 +13,6 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/pelletier/go-toml" "github.com/spf13/viper" - "io/ioutil" - "sync" - "time" ) const MDateFormat = "20060102_150405" diff --git a/internal/app/module/beegopro/migration.go b/internal/app/module/beegopro/migration.go index a17a233..ec1eb08 100644 --- a/internal/app/module/beegopro/migration.go +++ b/internal/app/module/beegopro/migration.go @@ -2,11 +2,12 @@ package beegopro import ( "database/sql" - beeLogger "github.com/beego/bee/logger" - "github.com/beego/bee/utils" "io/ioutil" "path/filepath" "strings" + + beeLogger "github.com/beego/bee/logger" + "github.com/beego/bee/utils" ) var SQL utils.DocValue diff --git a/internal/app/module/beegopro/parser_mysql.go b/internal/app/module/beegopro/parser_mysql.go index 08180cc..af5e80c 100644 --- a/internal/app/module/beegopro/parser_mysql.go +++ b/internal/app/module/beegopro/parser_mysql.go @@ -3,6 +3,7 @@ package beegopro import ( "database/sql" "fmt" + "github.com/beego/bee/internal/pkg/utils" beeLogger "github.com/beego/bee/logger" ) diff --git a/internal/app/module/beegopro/pongo2.go b/internal/app/module/beegopro/pongo2.go index 31453c6..24ee5b2 100644 --- a/internal/app/module/beegopro/pongo2.go +++ b/internal/app/module/beegopro/pongo2.go @@ -1,10 +1,11 @@ package beegopro import ( - "github.com/beego/bee/utils" - "github.com/flosch/pongo2" "strings" "unicode/utf8" + + "github.com/beego/bee/utils" + "github.com/flosch/pongo2" ) func init() { diff --git a/internal/app/module/beegopro/render.go b/internal/app/module/beegopro/render.go index 7ca1464..3721621 100644 --- a/internal/app/module/beegopro/render.go +++ b/internal/app/module/beegopro/render.go @@ -123,7 +123,7 @@ func (r *RenderFile) Exec(name string) { var orgContent []byte if err == nil { if org, err := os.OpenFile(r.Descriptor.DstPath, os.O_RDONLY, 0666); err == nil { - orgContent,_ = ioutil.ReadAll(org) + orgContent, _ = ioutil.ReadAll(org) org.Close() } else { beeLogger.Log.Infof("file err %s", err) @@ -151,4 +151,3 @@ func (r *RenderFile) Exec(name string) { beeLogger.Log.Infof("create file '%s' from %s", r.FlushFile, r.PackageName) } } - diff --git a/internal/app/module/beegopro/schema.go b/internal/app/module/beegopro/schema.go index c4f6a1e..9a2baac 100644 --- a/internal/app/module/beegopro/schema.go +++ b/internal/app/module/beegopro/schema.go @@ -2,15 +2,16 @@ package beegopro import ( "fmt" + "path/filepath" + "strings" + "sync" + "github.com/beego/bee/internal/pkg/command" "github.com/beego/bee/internal/pkg/system" beeLogger "github.com/beego/bee/logger" "github.com/beego/bee/utils" "github.com/flosch/pongo2" "github.com/smartwalle/pongo2render" - "path/filepath" - "strings" - "sync" ) // store all data diff --git a/internal/app/module/beegopro/schema_model.go b/internal/app/module/beegopro/schema_model.go index 25e2c8c..22e7507 100644 --- a/internal/app/module/beegopro/schema_model.go +++ b/internal/app/module/beegopro/schema_model.go @@ -1,8 +1,9 @@ package beegopro import ( - "github.com/beego/bee/utils" "strings" + + "github.com/beego/bee/utils" ) // parse get the model info diff --git a/internal/app/module/beegopro/schema_mysql_model.go b/internal/app/module/beegopro/schema_mysql_model.go index 177aa0e..7bc2d8b 100644 --- a/internal/app/module/beegopro/schema_mysql_model.go +++ b/internal/app/module/beegopro/schema_mysql_model.go @@ -3,6 +3,7 @@ package beegopro import ( "database/sql" "errors" + "github.com/beego/bee/logger" ) diff --git a/internal/app/module/beegopro/util.go b/internal/app/module/beegopro/util.go index 604e404..22a668a 100644 --- a/internal/app/module/beegopro/util.go +++ b/internal/app/module/beegopro/util.go @@ -173,14 +173,14 @@ func getModelType(orm string) (inputType, goType, mysqlType, tag string) { return } -func FileContentChange(org,new []byte, seg string) bool { +func FileContentChange(org, new []byte, seg string) bool { if len(org) == 0 { return true } - orgContent := GetFilterContent(string(org),seg) - newContent := GetFilterContent(string(new),seg) + orgContent := GetFilterContent(string(org), seg) + newContent := GetFilterContent(string(new), seg) orgMd5 := md5.Sum([]byte(orgContent)) - newMd5:= md5.Sum([]byte(newContent)) + newMd5 := md5.Sum([]byte(newContent)) if orgMd5 != newMd5 { return true } @@ -193,7 +193,7 @@ func GetFilterContent(content string, seg string) string { for _, s := range strings.Split(content, "\n") { s = strings.TrimSpace(strings.TrimPrefix(s, seg)) var have bool - for _,except := range CompareExcept{ + for _, except := range CompareExcept { if strings.HasPrefix(s, except) { have = true } diff --git a/internal/pkg/git/repository.go b/internal/pkg/git/repository.go index d24a840..ebad8dd 100644 --- a/internal/pkg/git/repository.go +++ b/internal/pkg/git/repository.go @@ -3,13 +3,14 @@ package git import ( "errors" "fmt" - "github.com/beego/bee/internal/pkg/command" - "github.com/beego/bee/internal/pkg/utils" - beeLogger "github.com/beego/bee/logger" "path/filepath" "sort" "strconv" "strings" + + "github.com/beego/bee/internal/pkg/command" + "github.com/beego/bee/internal/pkg/utils" + beeLogger "github.com/beego/bee/logger" ) // git tag diff --git a/internal/pkg/utils/file.go b/internal/pkg/utils/file.go index 2dc51ba..5d931eb 100644 --- a/internal/pkg/utils/file.go +++ b/internal/pkg/utils/file.go @@ -1,8 +1,9 @@ package utils import ( - beeLogger "github.com/beego/bee/logger" "os" + + beeLogger "github.com/beego/bee/logger" ) // Mkdir ... diff --git a/utils/utils.go b/utils/utils.go index e9656dc..398aa1b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -39,7 +39,7 @@ import ( ) type tagName struct { - Name string `json:"name"` + Name string `json:"name"` } func GetBeeWorkPath() string { @@ -521,7 +521,7 @@ func NoticeUpdateBee() { beeLogger.Log.Info("Getting bee latest version...") versionLast := BeeLastVersion() versionNow := config.Version - if versionLast == ""{ + if versionLast == "" { beeLogger.Log.Warn("Get latest version err") return }