github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/macros/grouptypes/group.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "go/ast" 7 "go/parser" 8 "go/token" 9 "log" 10 "path/filepath" 11 ) 12 13 func main() { 14 flag.Parse() 15 16 fmt.Printf("package tmptypes \n") 17 for _, file := range flag.Args() { 18 pName := filepath.Base(filepath.Dir(file)) 19 typeNames := parseFile(file) 20 for _, type_ := range typeNames { 21 fmt.Printf("type %s = %s.%s\n", type_, pName, type_) 22 } 23 } 24 } 25 26 func parseFile(filename string) []string { 27 t := token.NewFileSet() 28 f, err := parser.ParseFile(t, filename, nil, parser.ParseComments) 29 if err != nil { 30 log.Fatal(err) 31 } 32 var out []string 33 for _, decl := range f.Decls { 34 gen, ok := decl.(*ast.GenDecl) 35 if !ok || gen.Tok != token.TYPE { 36 continue 37 } 38 for _, spec := range gen.Specs { 39 if type_, ok := spec.(*ast.TypeSpec); ok { 40 if type_.Name.IsExported() { 41 out = append(out, type_.Name.Name) 42 } 43 } 44 } 45 } 46 return out 47 }