Skip to content

Commit

Permalink
updated exec - copy command for include and exclude patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Jeannopoulos committed Aug 4, 2020
1 parent 74d4433 commit 2574c97
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 52 deletions.
22 changes: 18 additions & 4 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _test/dbmeta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
goopt.Description = func() string {
return "ORM and RESTful meta data viewer for SQl databases"
}
goopt.Version = "v0.9.26 (07/31/2020)"
goopt.Version = "v0.9.27 (08/04/2020)"
goopt.Summary = `dbmeta [-v] --sqltype=mysql --connstr "user:password@/dbname" --database <databaseName>
sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]
Expand Down
4 changes: 2 additions & 2 deletions code_http.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func GetInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoices [post]
// echo '{"billing_state": "USFzllBypZZJDjtbuDWBnWcrU","billing_country": "KgrvVzwkLcFjnchqPwVoHWkRT","billing_postal_code": "ScJIRKwUTRltasmLeQNMLxJYS","total": 0.03391519763659607,"customer_id": 66,"invoice_date": "2123-11-18T21:39:12.15674111-08:00","billing_address": "nPoieNSaqVDVMBQvfwSXcYdtM","billing_city": "ObTQBQeAscRjOcAkHOOKmkHXU","invoice_id": 7}' | http POST "http://127.0.0.1:8080/invoices" X-Api-User:user123
// echo '{"billing_state": "ZDIUXoQexYEYgpaYJgyitZpjS","billing_country": "xbuWQjVGOCsrKSXCJhxeVSFhQ","billing_postal_code": "fEHjgTFVSOwVZEPNUjpVkmOZV","customer_id": 40,"invoice_date": "2021-12-17T01:29:16.603984343-08:00","billing_address": "GoPOJOqToKgHAXoCRwOLzSsgW","billing_city": "pRhdTdgdcpgiBrgvmCMlFKapm","total": 0.38248355155004915,"invoice_id": 46}' | http POST "http://127.0.0.1:8080/invoices" X-Api-User:user123
func AddInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := initializeContext(r)
invoices := &model.Invoices{}
Expand Down Expand Up @@ -192,7 +192,7 @@ func AddInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoices/{argInvoiceID} [put]
// echo '{"billing_state": "USFzllBypZZJDjtbuDWBnWcrU","billing_country": "KgrvVzwkLcFjnchqPwVoHWkRT","billing_postal_code": "ScJIRKwUTRltasmLeQNMLxJYS","total": 0.03391519763659607,"customer_id": 66,"invoice_date": "2123-11-18T21:39:12.15674111-08:00","billing_address": "nPoieNSaqVDVMBQvfwSXcYdtM","billing_city": "ObTQBQeAscRjOcAkHOOKmkHXU","invoice_id": 7}' | http PUT "http://127.0.0.1:8080/invoices/1" X-Api-User:user123
// echo '{"billing_state": "ZDIUXoQexYEYgpaYJgyitZpjS","billing_country": "xbuWQjVGOCsrKSXCJhxeVSFhQ","billing_postal_code": "fEHjgTFVSOwVZEPNUjpVkmOZV","customer_id": 40,"invoice_date": "2021-12-17T01:29:16.603984343-08:00","billing_address": "GoPOJOqToKgHAXoCRwOLzSsgW","billing_city": "pRhdTdgdcpgiBrgvmCMlFKapm","total": 0.38248355155004915,"invoice_id": 46}' | http PUT "http://127.0.0.1:8080/invoices/1" X-Api-User:user123
func UpdateInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := initializeContext(r)

Expand Down
91 changes: 53 additions & 38 deletions dbmeta/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,46 +622,77 @@ func (c *Config) DisplayConfig() string {
return info
}

type copyRules struct {
result bool
pattern string
r *regexp.Regexp
}

// FileSystemCopy template command to copy files, directories and to pass --include XXX and --exclude YYY regular expressions. Files ending in .tmpl will be processed as a template.
// Files ending in .table.tmpl will be processed as a template iterating through all the tables
func (c *Config) FileSystemCopy(src, dst string, options ...string) string {
dstDir := filepath.Join(c.OutDir, dst)
opt := utils.DefaultCopyOptions()
opt.FileHandler = c.handleFile

excludePattern := ""
includePattern := ""
patterns := make([]*copyRules, 0)

for _, o := range options {

if strings.HasPrefix(o, "--exclude ") {
excludePattern = o[len("--exclude "):]
pattern := o[len("--exclude "):]
r, _ := regexp.Compile(pattern)
if r != nil {
patterns = append(patterns, &copyRules{result: false, r: r, pattern: pattern})
}
}

if strings.HasPrefix(o, "--include ") {
includePattern = o[len("--include "):]
pattern := o[len("--include "):]
r, _ := regexp.Compile(pattern)
if r != nil {
patterns = append(patterns, &copyRules{result: true, r: r, pattern: pattern})
}
}
}

excludeRegex, _ := regexp.Compile(excludePattern)
if excludeRegex != nil {
fmt.Printf("copy excludePattern: [%s]\n", excludePattern)
}
includeRegex, _ := regexp.Compile(includePattern)
if includeRegex != nil {
fmt.Printf("copy includePattern: [%s]\n", includePattern)
}
opt := utils.DefaultCopyOptions()

opt.ShouldCopyDir = func(info os.FileInfo) bool {
opt.ShouldCopy = func(info os.FileInfo) bool {
name := info.Name()

if includeRegex != nil && includeRegex.Match([]byte(name)) {
return true
}
if excludeRegex != nil && excludeRegex.Match([]byte(name)) {
return false
for _, r := range patterns {
if r.r.Match([]byte(name)) {
//fmt.Printf("copy ShouldCopy %s pattern: [%s] result: %t\n", name, r.pattern, r.result)
return r.result
}
}

return true
}

opt.FileHandler = func(src, dest string, info os.FileInfo) utils.FileHandlerFunc {

if !opt.ShouldCopy(info) {
return func(src, dest string, info os.FileInfo, opt utils.Options, results *utils.Results) (err error) {
results.Info.WriteString(fmt.Sprintf("CopyFile Skipping %s\n", src))
return nil
}
}

if strings.HasSuffix(src, ".table.tmpl") {
//fmt.Printf("@@ HandleTableTemplateFile: src: %s dest: %s Name: %s\n", src, dest, info.Name())
return c.tableFileHandlerFunc
}
if strings.HasSuffix(src, ".tmpl") {
//fmt.Printf("@@ HandleTemplateFile: src: %s dest: %s Name: %s\n", src, dest, info.Name())
return c.fileHandlerFunc
}

return func(src, dest string, info os.FileInfo, opt utils.Options, results *utils.Results) (err error) {
results.Info.WriteString(fmt.Sprintf("CopyFile %s\n", dest))
return utils.DefaultFileCopy(src, dest, info, opt, results)
}
}

result, err := utils.Copy(src, dstDir, opt)
if err != nil {
return fmt.Sprintf("copy returned an error %v", err)
Expand Down Expand Up @@ -702,23 +733,6 @@ func (c *Config) Touch(dst string) string {
return fmt.Sprintf("touch %s", dstDir)
}

func (c *Config) handleFile(src, dest string, info os.FileInfo) utils.FileHandlerFunc {

if strings.HasSuffix(src, ".table.tmpl") {
//fmt.Printf("@@ HandleTableTemplateFile: src: %s dest: %s Name: %s\n", src, dest, info.Name())
return c.tableFileHandlerFunc
}
if strings.HasSuffix(src, ".tmpl") {
//fmt.Printf("@@ HandleTemplateFile: src: %s dest: %s Name: %s\n", src, dest, info.Name())
return c.fileHandlerFunc
}

return func(src, dest string, info os.FileInfo, opt utils.Options, results *utils.Results) (err error) {
results.Info.WriteString(fmt.Sprintf("CopyFile: %s\n", dest))
return utils.DefaultFileCopy(src, dest, info, opt, results)
}
}

// ".tmpl"
func (c *Config) fileHandlerFunc(src, dest string, info os.FileInfo, opt utils.Options, results *utils.Results) (err error) {
genTemplate := &GenTemplate{
Expand All @@ -745,13 +759,14 @@ func (c *Config) tableFileHandlerFunc(src, dest string, info os.FileInfo, opt ut
dir := filepath.Dir(outputFile)
tmplateName := filepath.Base(outputFile)
// parent := filepath.Base(dir)
results.Info.WriteString(fmt.Sprintf("WriteTableTemplate %s\n", src))

for tableName, tableInfo := range c.TableInfos {
data := c.CreateContextForTableFile(tableInfo)
// fileName := filepath.Join(dir, tableName+name)
name := c.ReplaceFileNamingTemplate(tableName) + filepath.Ext(tmplateName)
fileName := filepath.Join(dir, name)
results.Info.WriteString(fmt.Sprintf("WriteTableTemplate %s\n", fileName))
results.Info.WriteString(fmt.Sprintf(" table: %-25s %s\n", tableName, fileName))
c.WriteTemplate(genTemplate, data, fileName)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func init() {
goopt.Description = func() string {
return "ORM and RESTful API generator for SQl databases"
}
goopt.Version = "v0.9.26 (07/31/2020)"
goopt.Version = "v0.9.27 (08/04/2020)"
goopt.Summary = `gen [-v] --sqltype=mysql --connstr "user:password@/dbname" --database <databaseName> --module=example.com/example [--json] [--gorm] [--guregu] [--generate-dao] [--generate-proj]
git fetch up
sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]
Expand Down
Loading

0 comments on commit 2574c97

Please sign in to comment.