forked from onsi/ginkgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements feature request in onsi#753 Signed-off-by: Daniel Lipovetsky <daniel.lipovetsky@gmail.com>
- Loading branch information
1 parent
b08408c
commit cc7c3f2
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"go/parser" | ||
"go/token" | ||
"os" | ||
|
||
"github.com/onsi/ginkgo/ginkgo/outline" | ||
) | ||
|
||
func BuildOutlineCommand() *Command { | ||
const defaultFormat = "csv" | ||
var format string | ||
flagSet := flag.NewFlagSet("outline", flag.ExitOnError) | ||
flagSet.StringVar(&format, "format", defaultFormat, "Format of outline. Accepted: 'csv', 'json'") | ||
return &Command{ | ||
Name: "outline", | ||
FlagSet: flagSet, | ||
UsageCommand: "ginkgo outline <filename>", | ||
Usage: []string{ | ||
"Outline of Ginkgo symbols for the file", | ||
}, | ||
Command: func(args []string, additionalArgs []string) { | ||
outlineFile(args, format) | ||
}, | ||
} | ||
} | ||
|
||
func outlineFile(args []string, format string) { | ||
if len(args) != 1 { | ||
println("usage: ginkgo outline <filename>") | ||
os.Exit(1) | ||
} | ||
|
||
filename := args[0] | ||
fset := token.NewFileSet() | ||
|
||
src, err := parser.ParseFile(fset, filename, nil, 0) | ||
if err != nil { | ||
println(fmt.Sprintf("error parsing source: %s", err)) | ||
os.Exit(1) | ||
} | ||
|
||
o, err := outline.FromASTFile(fset, src) | ||
if err != nil { | ||
println(fmt.Sprintf("error creating outline: %s", err)) | ||
os.Exit(1) | ||
} | ||
|
||
var oerr error | ||
switch format { | ||
case "csv": | ||
_, oerr = fmt.Print(o) | ||
case "json": | ||
b, err := json.Marshal(o) | ||
if err != nil { | ||
println(fmt.Sprintf("error marshalling to json: %s", err)) | ||
} | ||
_, oerr = fmt.Print(string(b)) | ||
default: | ||
complainAndQuit(fmt.Sprintf("format %s not accepted", format)) | ||
} | ||
if oerr != nil { | ||
println(fmt.Sprintf("error writing outline: %s", oerr)) | ||
os.Exit(1) | ||
} | ||
} |