github.com/oNaiPs/go-generate-fast@v0.3.0/src/plugins/stringer/stringer.go (about) 1 package plugin_stringer 2 3 import ( 4 "flag" 5 "fmt" 6 "path/filepath" 7 "strings" 8 9 "github.com/oNaiPs/go-generate-fast/src/plugins" 10 "github.com/oNaiPs/go-generate-fast/src/utils/fs" 11 "github.com/oNaiPs/go-generate-fast/src/utils/pkg" 12 "go.uber.org/zap" 13 ) 14 15 type StringerPlugin struct { 16 plugins.Plugin 17 } 18 19 func (p *StringerPlugin) Name() string { 20 return "stringer" 21 } 22 23 func (p *StringerPlugin) Matches(opts plugins.GenerateOpts) bool { 24 return opts.ExecutableName == "stringer" || 25 opts.GoPackage == "golang.org/x/tools/cmd/stringer" 26 } 27 28 type Flags struct { 29 TypeNames string 30 Output string 31 TrimPrefix string 32 LineComment bool 33 BuildTags string 34 } 35 36 func (p *StringerPlugin) ComputeInputOutputFiles(opts plugins.GenerateOpts) *plugins.InputOutputFiles { 37 // parsing code inspired by https://github.com/golang/tools/blob/master/cmd/stringer/stringer.go 38 flagSet := flag.NewFlagSet("stringer", flag.ContinueOnError) 39 40 flags := Flags{} 41 flagSet.StringVar(&flags.TypeNames, "type", "", "comma-separated list of type names; must be set") 42 flagSet.StringVar(&flags.Output, "output", "", "output file name; default srcdir/<type>_string.go") 43 flagSet.StringVar(&flags.TrimPrefix, "trimprefix", "", "trim the `prefix` from the generated constant names") 44 flagSet.BoolVar(&flags.LineComment, "linecomment", false, "use line comment text as printed text when present") 45 flagSet.StringVar(&flags.BuildTags, "tags", "", "comma-separated list of build tags to apply") 46 47 err := flagSet.Parse(opts.SanitizedArgs) 48 if err != nil { 49 zap.S().Warn("Cannot parse stringer arguments: ", err.Error()) 50 return nil 51 } 52 53 ioFiles := plugins.InputOutputFiles{} 54 55 args := flagSet.Args() 56 tags := []string{} 57 if len(flags.BuildTags) > 0 { 58 tags = strings.Split(flags.BuildTags, ",") 59 } 60 61 // We accept either one directory or a list of files. Which do we have? 62 if len(args) == 0 { 63 // Default: process whole package in current directory. 64 args = []string{"."} 65 } 66 67 // Parse the package once. 68 var dir string 69 if len(args) == 0 { 70 dir = opts.Dir() 71 } else if len(args) == 1 && fs.IsDir(args[0]) { 72 dir = args[0] 73 } else { 74 if len(tags) != 0 { 75 zap.S().Fatal("-tags option applies only to directories, not when files are specified") 76 } 77 dir = filepath.Dir(args[0]) 78 } 79 80 pkg := pkg.LoadPackages(dir, args, tags) 81 if pkg == nil { 82 //did not find package matches 83 return nil 84 } else if len(pkg.Errors) > 0 { 85 zap.S().Debug("Got errors parsing packages: %s", pkg.Errors) 86 return nil 87 } 88 89 ioFiles.InputFiles = append(ioFiles.InputFiles, pkg.CompiledGoFiles...) 90 91 types := strings.Split(flags.TypeNames, ",") 92 outputName := flags.Output 93 94 if outputName == "" { 95 outputName = strings.ToLower(fmt.Sprintf("%s_string.go", types[0])) 96 } 97 ioFiles.OutputFiles = append(ioFiles.OutputFiles, outputName) 98 99 return &ioFiles 100 } 101 102 func init() { 103 plugins.RegisterPlugin(&StringerPlugin{}) 104 }