Skip to content

Commit 24bd020

Browse files
committed
feat: add --parse-vendors parameter to allow select which vendors will be parsed
1 parent e0159db commit 24bd020

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ Available Commands:
8787
merge Merge multiple openapi specification into one
8888
8989
Flags:
90-
-h, --help help for openapi-parser
91-
--output string The output file (default "openapi.yaml")
92-
--path string The Folder to parse (default ".")
90+
-h, --help help for openapi-parser
91+
--output string The output file (default "openapi.yaml")
92+
--parse-vendors stringArray Give the vendor to parse
93+
--path string The Folder to parse (default ".")
9394
```
9495

9596
### Example
9697

9798
`openapi-parser`
9899

99100
`openapi-parser --path /my/path --output my-openapi.yaml`
101+
102+
`openapi-parser --path /my/path --output my-openapi.yaml --parse-vendors github.com/my/library-to-parse`

cmd/root.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
)
1313

1414
var (
15-
outputPath string
16-
inputPath string
15+
outputPath string
16+
inputPath string
17+
parseVendors []string
1718
)
1819

1920
// RootCmd represents the root command
@@ -23,7 +24,7 @@ var RootCmd = &cobra.Command{
2324
Long: `Parse comments in code to generate an OpenAPI documentation`,
2425
Run: func(cmd *cobra.Command, args []string) {
2526
spec := docparser.NewOpenAPI()
26-
spec.Parse(inputPath)
27+
spec.Parse(inputPath, parseVendors)
2728
d, err := yaml.Marshal(&spec)
2829
if err != nil {
2930
log.Fatalf("error: %v", err)
@@ -44,4 +45,5 @@ func Execute() {
4445
func init() {
4546
RootCmd.Flags().StringVar(&outputPath, "output", "openapi.yaml", "The output file")
4647
RootCmd.Flags().StringVar(&inputPath, "path", ".", "The Folder to parse")
48+
RootCmd.Flags().StringArrayVar(&parseVendors, "parse-vendors", []string{}, "Give the vendor to parse")
4749
}

docparser/model.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,19 @@ type content struct {
145145
Schema schema
146146
}
147147

148-
func validatePath(path string) bool {
148+
func validatePath(path string, parseVendors []string) bool {
149149
// vendoring path
150150
if strings.Contains(path, "vendor") {
151-
return false
151+
found := false
152+
for _, vendorPath := range parseVendors {
153+
if strings.Contains(path, vendorPath) {
154+
found = true
155+
break
156+
}
157+
}
158+
if !found {
159+
return false
160+
}
152161
}
153162

154163
// not golang file
@@ -164,11 +173,11 @@ func validatePath(path string) bool {
164173
return true
165174
}
166175

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

170179
_ = filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
171-
if validatePath(path) {
180+
if validatePath(path, parseVendors) {
172181
astFile, _ := parseFile(path)
173182
spec.parseInfos(astFile)
174183
spec.parseSchemas(astFile)

docparser/model_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func Test_validatePath(t *testing.T) {
4848
}
4949
for _, tt := range tests {
5050
t.Run(tt.name, func(t *testing.T) {
51-
if got := validatePath(tt.args.path); got != tt.want {
51+
if got := validatePath(tt.args.path, []string{}); got != tt.want {
5252
t.Errorf("validatePath() = %v, want %v", got, tt.want)
5353
}
5454
})

0 commit comments

Comments
 (0)