2024-12-18 16:07:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2025-01-02 15:40:06 +00:00
|
|
|
"github.com/databricks/cli/bundle/internal/annotation"
|
2024-12-18 16:07:09 +00:00
|
|
|
"github.com/databricks/cli/libs/jsonschema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) != 3 {
|
|
|
|
fmt.Println("Usage: go run main.go <annotation-file> <output-file>")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
annotationFile := os.Args[1]
|
|
|
|
outputFile := os.Args[2]
|
|
|
|
|
|
|
|
err := generateDocs(annotationFile, outputFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateDocs(workdir, outputPath string) error {
|
|
|
|
annotationsPath := filepath.Join(workdir, "annotations.yml")
|
|
|
|
|
2025-01-02 15:40:06 +00:00
|
|
|
annotations, err := annotation.LoadAndMerge([]string{annotationsPath})
|
2024-12-18 16:07:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
schemas := map[string]jsonschema.Schema{}
|
2024-12-18 19:53:38 +00:00
|
|
|
customFields := map[string]bool{}
|
2024-12-18 16:07:09 +00:00
|
|
|
|
|
|
|
s, err := jsonschema.FromType(reflect.TypeOf(config.Root{}), []func(reflect.Type, jsonschema.Schema) jsonschema.Schema{
|
|
|
|
func(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema {
|
2024-12-18 19:53:38 +00:00
|
|
|
_, isCustomField := annotations[jsonschema.TypePath(typ)]
|
|
|
|
if isCustomField {
|
|
|
|
customFields[jsonschema.TypePath(typ)] = true
|
|
|
|
}
|
2024-12-18 16:07:09 +00:00
|
|
|
schemas[jsonschema.TypePath(typ)] = s
|
|
|
|
|
|
|
|
refPath := getPath(typ)
|
|
|
|
shouldHandle := strings.HasPrefix(refPath, "github.com")
|
|
|
|
if !shouldHandle {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
a := annotations[refPath]
|
|
|
|
if a == nil {
|
2025-01-02 15:40:06 +00:00
|
|
|
a = map[string]annotation.Descriptor{}
|
2024-12-18 16:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rootTypeAnnotation, ok := a["_"]
|
|
|
|
if ok {
|
|
|
|
assignAnnotation(&s, rootTypeAnnotation)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range s.Properties {
|
|
|
|
assignAnnotation(v, a[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-12-18 19:53:38 +00:00
|
|
|
nodes := getNodes(s, schemas, customFields)
|
2024-12-18 16:07:09 +00:00
|
|
|
err = buildMarkdown(nodes, outputPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPath(typ reflect.Type) string {
|
|
|
|
return typ.PkgPath() + "." + typ.Name()
|
|
|
|
}
|
|
|
|
|
2025-01-02 15:40:06 +00:00
|
|
|
func assignAnnotation(s *jsonschema.Schema, a annotation.Descriptor) {
|
|
|
|
if a.Description != "" && a.Description != annotation.Placeholder {
|
2024-12-18 16:07:09 +00:00
|
|
|
s.Description = a.Description
|
|
|
|
}
|
|
|
|
if a.MarkdownDescription != "" {
|
|
|
|
s.MarkdownDescription = a.MarkdownDescription
|
|
|
|
}
|
2025-01-02 15:13:24 +00:00
|
|
|
if a.MarkdownExamples != "" {
|
|
|
|
s.Examples = []any{a.MarkdownExamples}
|
|
|
|
}
|
2024-12-18 16:07:09 +00:00
|
|
|
}
|