docs(goctl): goctl 1.3.4 migration note (#1780)

* docs(goctl): goctl 1.3.4 migration note

* adds a simple lang check
* adds migration notes

* chore: remove i18n

* chore: remove todo
This commit is contained in:
Fyn
2022-04-18 14:42:13 +08:00
committed by GitHub
parent c6ab11b14f
commit 036d803fbb
4 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package migrationnotes
import (
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
)
// BeforeCommands run before comamnd run to show some migration notes
func BeforeCommands(ctx *cli.Context) error {
if err := migrateBefore1_3_4(ctx); err != nil {
return err
}
return nil
}
func getModelSuffix(style string) (string, error) {
cfg, err := config.NewConfig(style)
if err != nil {
return "", err
}
baseSuffix, err := format.FileNamingFormat(cfg.NamingFormat, "_model")
if err != nil {
return "", err
}
return baseSuffix + ".go", nil
}

View File

@@ -0,0 +1,57 @@
package migrationnotes
import (
"fmt"
"io/ioutil"
"strings"
"github.com/urfave/cli"
)
func migrateBefore1_3_4(ctx *cli.Context) error {
dir := ctx.String("dir")
style := ctx.String("style")
ok, err := needShow1_3_4(dir, style)
if err != nil {
return err
}
if !ok {
return nil
}
fmt.Println(`It seems like that your goctl has just been upgraded to version 1.3.4 or later, which refactored the code of the model module. The original XXXmodel.go has been split into XXXmodel_gen.go (read-only) and XXXmodel.go. You just need to follow these steps to complete the migration:
1. back up the original XXXmodel.go (make sure the file name is no longer in the current directory)
2. re-run the generate command (a new XXXmodel.go will be created)
3. populate XXXmodel.go with the code that is not generated by goctl according to the comments in XXXmodel_gen.go`)
return nil
}
func needShow1_3_4(dir, style string) (bool, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return false, nil
}
// Returns false when the directory contains a file with the suffix "_gen.go"
// In addition, it returns true if it contains a model file extension.
// In other case, false is returned.
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasSuffix(f.Name(), "_gen.go") {
return false, nil
}
}
modelSuffix, err := getModelSuffix(style)
if err != nil {
return false, err
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), modelSuffix) {
return true, nil
}
}
return false, nil
}

View File

@@ -0,0 +1,75 @@
package migrationnotes
import (
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func Test_needShow1_3_4(t *testing.T) {
root, err := os.MkdirTemp("", "goctl-model")
require.NoError(t, err)
defer os.RemoveAll(root)
dir1 := path.Join(root, "dir1")
require.NoError(t, os.Mkdir(dir1, fs.ModePerm))
ioutil.WriteFile(filepath.Join(dir1, "foo_gen.go"), nil, fs.ModePerm)
dir2 := path.Join(root, "dir2")
require.NoError(t, os.Mkdir(dir2, fs.ModePerm))
ioutil.WriteFile(filepath.Join(dir2, "foomodel.go"), nil, fs.ModePerm)
dir3 := path.Join(root, "dir3")
require.NoError(t, os.Mkdir(dir3, fs.ModePerm))
ioutil.WriteFile(filepath.Join(dir3, "irrelevant.go"), nil, fs.ModePerm)
type args struct {
dir string
style string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "dir that contains *_gen.go should return false",
args: args{
dir: dir1,
},
want: false,
},
{
name: "dir that contains *model.go without *_gen.go should return true",
args: args{
dir: dir2,
},
want: true,
},
{
name: "dir that only contains irrelevant files should return false",
args: args{
dir: dir3,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := needShow1_3_4(tt.args.dir, tt.args.style)
if (err != nil) != tt.wantErr {
t.Errorf("needShow1_3_4() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("needShow1_3_4() = %v, want %v", got, tt.want)
}
})
}
}