diff --git a/README.md b/README.md index 17e59bb..269345c 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,10 @@ 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 @@ -97,3 +98,5 @@ Flags: `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` diff --git a/cmd/root.go b/cmd/root.go index 0417f5b..cc1431d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,8 +12,9 @@ import ( ) var ( - outputPath string - inputPath string + outputPath string + inputPath string + parseVendors []string ) // RootCmd represents the root command @@ -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) @@ -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") } diff --git a/docparser/model.go b/docparser/model.go index b886eef..74ccd25 100644 --- a/docparser/model.go +++ b/docparser/model.go @@ -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 @@ -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) diff --git a/docparser/model_test.go b/docparser/model_test.go index a1b304e..a29c399 100644 --- a/docparser/model_test.go +++ b/docparser/model_test.go @@ -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) } })