|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/getkin/kin-openapi/openapi3" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +type operationPath struct { |
| 13 | + operation *openapi3.Operation |
| 14 | + pathItem *openapi3.PathItem |
| 15 | + pathUrl string |
| 16 | + specPath string |
| 17 | + method string |
| 18 | + docsHint string |
| 19 | +} |
| 20 | + |
| 21 | +func generateReferenceDocs(config config, docsPath string) error { |
| 22 | + aggregatedDocs := map[string][]operationPath{} |
| 23 | + |
| 24 | + assetPathBase := path.Join(docsPath, "docs") |
| 25 | + |
| 26 | + for _, spec := range config.Specs { |
| 27 | + loader := openapi3.NewLoader() |
| 28 | + doc, err := loader.LoadFromFile(path.Join(assetPathBase, spec.Path)) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + for pathUrl, pathItem := range doc.Paths.Map() { |
| 33 | + for method, operation := range pathItem.Operations() { |
| 34 | + for _, tag := range operation.Tags { |
| 35 | + tag = tag + spec.Suffix |
| 36 | + aggregatedDocs[tag] = append(aggregatedDocs[tag], operationPath{ |
| 37 | + operation: operation, |
| 38 | + pathItem: pathItem, |
| 39 | + pathUrl: pathUrl, |
| 40 | + specPath: spec.Path, |
| 41 | + method: method, |
| 42 | + docsHint: spec.DocsHint, |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + var summary []string |
| 50 | + for label, operation := range aggregatedDocs { |
| 51 | + if label == "OpenAPI" { |
| 52 | + continue |
| 53 | + } |
| 54 | + filePath := path.Join(docsPath, "docs/", config.Output.APIReferencePath, labelToFileName(label)) |
| 55 | + docsFile, err := os.Create(filePath) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + summary = append(summary, fmt.Sprintf("* [%s](%s)\n", label, path.Join(config.Output.APIReferencePath, labelToFileName(label)))) |
| 60 | + |
| 61 | + fmt.Fprintf(docsFile, `# %s |
| 62 | +
|
| 63 | +{%% hint style="info" %%} |
| 64 | +%s |
| 65 | +{%% endhint %%}`, label, operation[0].docsHint) |
| 66 | + |
| 67 | + // sort for stability |
| 68 | + sort.Slice(operation, func(i, j int) bool { |
| 69 | + return operation[i].pathUrl+operation[i].method > operation[j].pathUrl+operation[j].method |
| 70 | + }) |
| 71 | + |
| 72 | + for _, op := range operation { |
| 73 | + _, err = fmt.Fprintf(docsFile, |
| 74 | + ` |
| 75 | +{%% swagger src="../../%s" path="%s" method="%s" %%} |
| 76 | +[spec.yaml](../../%s) |
| 77 | +{%% endswagger %%} |
| 78 | +`, |
| 79 | + op.specPath, |
| 80 | + op.pathUrl, |
| 81 | + strings.ToLower(op.method), |
| 82 | + op.specPath, |
| 83 | + ) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + sort.Strings(summary) |
| 90 | + fmt.Printf("generated menu for summary:\n") |
| 91 | + fmt.Printf("%s", strings.Join(summary, "")) |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func labelToFileName(label string) string { |
| 97 | + replacements := []string{"(", ")"} |
| 98 | + for _, replacement := range replacements { |
| 99 | + label = strings.ReplaceAll(label, replacement, "") |
| 100 | + } |
| 101 | + return strings.ToLower(strings.ReplaceAll(label, " ", "-")) + ".md" |
| 102 | +} |
0 commit comments