Skip to content

Commit

Permalink
Merge branch 'master' into abice/go1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
abice committed Mar 12, 2021
2 parents d4a4392 + f5cbb58 commit 6d2f6c5
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ phony: clean tc build

.PHONY: example
example:
$(GO) generate ./example

$(GO) generate ./example/...

bin/goimports: go.sum
$(call goinstall,golang.org/x/tools/cmd/goimports)
Expand Down
3 changes: 3 additions & 0 deletions example/globs/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:generate ../../bin/go-enum -f=*.go

package globs
8 changes: 8 additions & 0 deletions example/globs/letter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package globs

/**
ENUM(
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
)
*/
type Letter int
139 changes: 139 additions & 0 deletions example/globs/letter_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions example/globs/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package globs

/**
ENUM(
0,1,2,3,4,5,6,7,8,9
)
*/
type Number int
75 changes: 75 additions & 0 deletions example/globs/number_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 38 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func main() {
},
},
Action: func(ctx *cli.Context) error {
for _, fileName := range argv.FileNames.Value() {
for _, fileOption := range argv.FileNames.Value() {

g := generator.NewGenerator()

Expand Down Expand Up @@ -152,24 +152,47 @@ func main() {
g.WithSQLNullStr()
}

originalName := fileName
var filenames []string

out("go-enum started. file: %s\n", color.Cyan(originalName))
fileName, _ = filepath.Abs(fileName)
outFilePath := fmt.Sprintf("%s_enum.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)))

// Parse the file given in arguments
raw, err := g.GenerateFromFile(fileName)
if err != nil {
return fmt.Errorf("failed generating enums\nInputFile=%s\nError=%s", color.Cyan(fileName), color.RedBg(err))
// In order to maintain existing capabilities, only glob when a * is in the path.
// Leave execution on par with old method in case there are bad patterns in use that somehow
// work without the Glob method.
if strings.Contains(fileOption, "*") {
matches, err := filepath.Glob(fileOption)
if err != nil {
return fmt.Errorf("failed parsing glob filepath\nInputFile=%s\nError=%s", color.Cyan(fileOption), color.RedBg(err))
}
filenames = append(filenames, matches...)
} else {
filenames = append(filenames, fileOption)
}

mode := int(0644)
err = ioutil.WriteFile(outFilePath, raw, os.FileMode(mode))
if err != nil {
return fmt.Errorf("failed writing to file %s: %s", color.Cyan(outFilePath), color.Red(err))
for _, fileName := range filenames {
originalName := fileName

out("go-enum started. file: %s\n", color.Cyan(originalName))
fileName, _ = filepath.Abs(fileName)
outFilePath := fmt.Sprintf("%s_enum.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)))

// Parse the file given in arguments
raw, err := g.GenerateFromFile(fileName)
if err != nil {
return fmt.Errorf("failed generating enums\nInputFile=%s\nError=%s", color.Cyan(fileName), color.RedBg(err))
}

// Nothing was generated, ignore the output and don't create a file.
if len(raw) < 1 {
out(color.Yellow("go-enum ignored. file: %s\n"), color.Cyan(originalName))
continue
}

mode := int(0644)
err = ioutil.WriteFile(outFilePath, raw, os.FileMode(mode))
if err != nil {
return fmt.Errorf("failed writing to file %s: %s", color.Cyan(outFilePath), color.Red(err))
}
out("go-enum finished. file: %s\n", color.Cyan(originalName))
}
out("go-enum finished. file: %s\n", color.Cyan(originalName))
}

return nil
Expand Down

0 comments on commit 6d2f6c5

Please sign in to comment.