Skip to content

Commit

Permalink
feat: add --parse-vendors parameter to allow select which vendors wil…
Browse files Browse the repository at this point in the history
…l be parsed
  • Loading branch information
denouche committed Dec 9, 2019
1 parent e0159db commit 24bd020
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ Available Commands:
merge Merge multiple openapi specification into one
Flags:
-h, --help help for openapi-parser
--output string The output file (default "openapi.yaml")
--path string The Folder to parse (default ".")
-h, --help help for openapi-parser
--output string The output file (default "openapi.yaml")
--parse-vendors stringArray Give the vendor to parse
--path string The Folder to parse (default ".")
```

### Example

`openapi-parser`

`openapi-parser --path /my/path --output my-openapi.yaml`

`openapi-parser --path /my/path --output my-openapi.yaml --parse-vendors github.com/my/library-to-parse`
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

var (
outputPath string
inputPath string
outputPath string
inputPath string
parseVendors []string
)

// RootCmd represents the root command
Expand All @@ -23,7 +24,7 @@ var RootCmd = &cobra.Command{
Long: `Parse comments in code to generate an OpenAPI documentation`,
Run: func(cmd *cobra.Command, args []string) {
spec := docparser.NewOpenAPI()
spec.Parse(inputPath)
spec.Parse(inputPath, parseVendors)
d, err := yaml.Marshal(&spec)
if err != nil {
log.Fatalf("error: %v", err)
Expand All @@ -44,4 +45,5 @@ func Execute() {
func init() {
RootCmd.Flags().StringVar(&outputPath, "output", "openapi.yaml", "The output file")
RootCmd.Flags().StringVar(&inputPath, "path", ".", "The Folder to parse")
RootCmd.Flags().StringArrayVar(&parseVendors, "parse-vendors", []string{}, "Give the vendor to parse")
}
17 changes: 13 additions & 4 deletions docparser/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,19 @@ type content struct {
Schema schema
}

func validatePath(path string) bool {
func validatePath(path string, parseVendors []string) bool {
// vendoring path
if strings.Contains(path, "vendor") {
return false
found := false
for _, vendorPath := range parseVendors {
if strings.Contains(path, vendorPath) {
found = true
break
}
}
if !found {
return false
}
}

// not golang file
Expand All @@ -164,11 +173,11 @@ func validatePath(path string) bool {
return true
}

func (spec *openAPI) Parse(path string) {
func (spec *openAPI) Parse(path string, parseVendors []string) {
// fset := token.NewFileSet() // positions are relative to fset

_ = filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if validatePath(path) {
if validatePath(path, parseVendors) {
astFile, _ := parseFile(path)
spec.parseInfos(astFile)
spec.parseSchemas(astFile)
Expand Down
2 changes: 1 addition & 1 deletion docparser/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Test_validatePath(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validatePath(tt.args.path); got != tt.want {
if got := validatePath(tt.args.path, []string{}); got != tt.want {
t.Errorf("validatePath() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 24bd020

Please sign in to comment.