Skip to content

Commit

Permalink
Merge pull request #4 from ramya-rao-a/imports-only
Browse files Browse the repository at this point in the history
Add option to parse imports only and an option to pass file contents
  • Loading branch information
lukehoban authored Oct 11, 2016
2 parents 6814d6f + 8309e44 commit e785568
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ go get -u github.com/lukehoban/go-outline
[{"label":"proc","type":"package",<...>}]
```

To parse and return only imports
```bash
> go-outline -f file.go -imports-only
```

### Schema
```go
type Declaration struct {
Expand Down
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@ type Declaration struct {
}

var (
file = flag.String("f", "", "the path to the file to outline")
file = flag.String("f", "", "the path to the file to outline")
importsOnly = flag.Bool("imports-only", false, "parse imports only")
src = flag.String("src", "", "source code of the file to outline")
)

func main() {
flag.Parse()
fset := token.NewFileSet()
fileAst, err := parser.ParseFile(fset, *file, nil, parser.ParseComments)
parserMode := parser.ParseComments
if *importsOnly == true {
parserMode = parser.ImportsOnly
}

var fileAst *ast.File
var err error

if len(*src) > 0 {
fileAst, err = parser.ParseFile(fset, *file, *src, parserMode)
} else {
fileAst, err = parser.ParseFile(fset, *file, nil, parserMode)
}

if err != nil {
reportError(fmt.Errorf("Could not parse file %s", *file))
}
Expand Down

0 comments on commit e785568

Please sign in to comment.