Skip to content

Instantly share code, notes, and snippets.

@simcap
Created April 4, 2018 13:31
Show Gist options
  • Save simcap/1fb6b80e1fe5a4119dc8230b4deb4bce to your computer and use it in GitHub Desktop.
Save simcap/1fb6b80e1fe5a4119dc8230b4deb4bce to your computer and use it in GitHub Desktop.
Collect calls to http package parsing Go source files
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
)
var (
dirFlag string
)
func main() {
flag.StringVar(&dirFlag, "dir", "./", "Dir where to parse go files")
flag.Parse()
fset := token.NewFileSet()
packages, err := parser.ParseDir(fset, dirFlag, nil, 0)
if err != nil {
fmt.Println(err)
return
}
for _, pkg := range packages {
for _, f := range pkg.Files {
visitor := &callExprVisitor{f}
ast.Walk(visitor, f)
}
}
}
type callExprVisitor struct {
f *ast.File
}
func (v *callExprVisitor) Visit(n ast.Node) ast.Visitor {
switch node := n.(type) {
case *ast.File:
return v
case *ast.CallExpr:
switch fn := node.Fun.(type) {
case *ast.SelectorExpr:
switch expr := fn.X.(type) {
case *ast.Ident:
if expr.Name == "http" {
fmt.Printf("(%s) calling pkg http with %v\n", v.f.Name, fn.Sel)
}
}
}
}
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment