model support globbing patterns (#153)
* model support globbing patterns * optimize model * optimize model * format code
This commit is contained in:
29
tools/goctl/model/sql/util/match_test.go
Normal file
29
tools/goctl/model/sql/util/match_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMatchFiles(t *testing.T) {
|
||||
dir, err := filepath.Abs("./")
|
||||
assert.Nil(t, err)
|
||||
|
||||
files, err := MatchFiles("./*.sql")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []string{filepath.Join(dir, "studeat.sql"), filepath.Join(dir, "student.sql"), filepath.Join(dir, "xx.sql")}, files)
|
||||
|
||||
files, err = MatchFiles("./??.sql")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []string{filepath.Join(dir, "xx.sql")}, files)
|
||||
|
||||
files, err = MatchFiles("./*.sq*")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []string{filepath.Join(dir, "studeat.sql"), filepath.Join(dir, "student.sql"), filepath.Join(dir, "xx.sql"), filepath.Join(dir, "xx.sql1")}, files)
|
||||
|
||||
files, err = MatchFiles("./student.sql")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []string{filepath.Join(dir, "student.sql")}, files)
|
||||
}
|
||||
38
tools/goctl/model/sql/util/matcher.go
Normal file
38
tools/goctl/model/sql/util/matcher.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// expression: globbing patterns
|
||||
func MatchFiles(in string) ([]string, error) {
|
||||
dir, pattern := filepath.Split(in)
|
||||
abs, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files, err := ioutil.ReadDir(abs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var res []string
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := file.Name()
|
||||
match, err := filepath.Match(pattern, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !match {
|
||||
continue
|
||||
}
|
||||
|
||||
res = append(res, filepath.Join(abs, name))
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
0
tools/goctl/model/sql/util/studeat.sql
Normal file
0
tools/goctl/model/sql/util/studeat.sql
Normal file
0
tools/goctl/model/sql/util/student.sql
Normal file
0
tools/goctl/model/sql/util/student.sql
Normal file
0
tools/goctl/model/sql/util/sub/sub.sql
Normal file
0
tools/goctl/model/sql/util/sub/sub.sql
Normal file
0
tools/goctl/model/sql/util/xx.sql
Normal file
0
tools/goctl/model/sql/util/xx.sql
Normal file
0
tools/goctl/model/sql/util/xx.sql1
Normal file
0
tools/goctl/model/sql/util/xx.sql1
Normal file
Reference in New Issue
Block a user