diff --git a/cmd/typels/main.go b/cmd/typels/main.go new file mode 100644 index 0000000..19f1366 --- /dev/null +++ b/cmd/typels/main.go @@ -0,0 +1,84 @@ +package main + +import ( + "flag" + "fmt" + "io" + "os" + + "github.com/gostaticanalysis/knife" +) + +var ( + flagFilter string + flagExported bool +) + +func init() { + flag.StringVar(&flagFilter, "f", "all", "object filter(all|interface|func|struct|chan|array|slice|map)") + flag.BoolVar(&flagExported, "exported", false, "filter only exported types") + flag.Parse() +} + +func main() { + if err := run(flag.Args()); err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } +} + +func run(args []string) error { + k, err := knife.New(args...) + if err != nil { + return err + } + + var w io.Writer = os.Stdout + + pkgs := k.Packages() + for i, pkg := range pkgs { + + kpkg := knife.NewPackage(pkg.Types) + + if len(pkgs) > 1 { + fmt.Fprintf(w, "# %s\n", kpkg.Path) + } + + for name, typ := range kpkg.Types { + if flagExported && !typ.Exported { + continue + } + + if typ.Exported && match(flagFilter, typ) { + fmt.Fprintln(w, name) + } + } + + if i != len(pkgs)-1 { + fmt.Fprintln(w) + } + } + + return nil +} + +func match(filter string, typ *knife.TypeName) bool { + switch filter { + case "interface": + return knife.ToInterface(typ) != nil + case "func": + return knife.ToSignature(typ) != nil + case "struct": + return knife.ToStruct(typ) != nil + case "chan": + return knife.ToChan(typ) != nil + case "array": + return knife.ToArray(typ) != nil + case "slice": + return knife.ToSlice(typ) != nil + case "map": + return knife.ToMap(typ) != nil + default: + return true + } +} diff --git a/go.mod b/go.mod index d83c207..e735be3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/gostaticanalysis/knife -go 1.14 +go 1.18 require ( github.com/gostaticanalysis/analysisutil v0.6.2 @@ -8,3 +8,10 @@ require ( github.com/gostaticanalysis/comment v1.4.2 golang.org/x/tools v0.1.1-0.20210302220138-2ac05c832e1a ) + +require ( + github.com/antchfx/xpath v1.1.10 // indirect + golang.org/x/mod v0.4.1 // indirect + golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect +) diff --git a/go.sum b/go.sum index e85eedc..e7ffaf5 100644 --- a/go.sum +++ b/go.sum @@ -17,10 +17,8 @@ github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k= github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= -github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI= github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= -github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc= github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA= github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0=