2010-04-27 20:36:39 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"go/parser"
|
|
|
|
"go/printer"
|
|
|
|
"go/ast"
|
2011-01-25 21:56:52 -07:00
|
|
|
"go/token"
|
2010-04-27 20:36:39 -06:00
|
|
|
"log"
|
|
|
|
"os"
|
2011-02-09 15:08:19 -07:00
|
|
|
"template"
|
2010-04-27 20:36:39 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
srcFn = flag.String("src", "", "source filename")
|
|
|
|
getName = flag.String("name", "", "func/type name to output")
|
|
|
|
html = flag.Bool("html", true, "output HTML")
|
|
|
|
showPkg = flag.Bool("pkg", false, "show package in output")
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// handle input
|
|
|
|
flag.Parse()
|
|
|
|
if *srcFn == "" || *getName == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
// load file
|
2011-01-25 21:56:52 -07:00
|
|
|
fs := token.NewFileSet()
|
|
|
|
file, err := parser.ParseFile(fs, *srcFn, nil, 0)
|
2010-04-27 20:36:39 -06:00
|
|
|
if err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal(err)
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
// create filter
|
|
|
|
filter := func(name string) bool {
|
|
|
|
return name == *getName
|
|
|
|
}
|
|
|
|
// filter
|
|
|
|
if !ast.FilterFile(file, filter) {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2011-02-09 15:08:19 -07:00
|
|
|
// print the AST
|
|
|
|
var b bytes.Buffer
|
|
|
|
printer.Fprint(&b, fs, file)
|
2010-04-27 20:36:39 -06:00
|
|
|
// drop package declaration
|
|
|
|
if !*showPkg {
|
|
|
|
for {
|
|
|
|
c, err := b.ReadByte()
|
|
|
|
if c == '\n' || err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// drop leading newlines
|
|
|
|
for {
|
|
|
|
b, err := b.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if b != '\n' {
|
|
|
|
os.Stdout.Write([]byte{b})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// output
|
2011-02-09 15:08:19 -07:00
|
|
|
if *html {
|
|
|
|
template.HTMLEscape(os.Stdout, b.Bytes())
|
|
|
|
} else {
|
|
|
|
b.WriteTo(os.Stdout)
|
|
|
|
}
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|