diff --git a/hack/docgen/def/mods/trait.go b/hack/docgen/def/mods/trait.go index 185ee2600..90ccc4143 100644 --- a/hack/docgen/def/mods/trait.go +++ b/hack/docgen/def/mods/trait.go @@ -86,7 +86,9 @@ func TraitDef(ctx context.Context, c common.Args, path, location *string, defdir }, CustomDocHeader: CustomTraitHeaderEN, } - ref.Remote = &docgen.FromCluster{Namespace: types.DefaultKubeVelaNS} + ref.Local = &docgen.FromLocal{ + Path: TraitDefDir, + } if *path != "" { ref.I18N = &docgen.En @@ -99,22 +101,24 @@ func TraitDef(ctx context.Context, c common.Args, path, location *string, defdir os.Exit(1) } fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), *path) - } - if *location == "" || *location == "en" { - ref.I18N = &docgen.En - if err := ref.GenerateReferenceDocs(ctx, c, TraitDefRefPath); err != nil { - fmt.Println(err) - os.Exit(1) + } else { + // Generate to default path depends on language + if *location == "" || *location == "en" { + ref.I18N = &docgen.En + if err := ref.GenerateReferenceDocs(ctx, c, TraitDefRefPath); err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), TraitDefRefPath) } - fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), TraitDefRefPath) - } - if *location == "" || *location == "zh" { - ref.I18N = &docgen.Zh - ref.CustomDocHeader = CustomTraitHeaderZH - if err := ref.GenerateReferenceDocs(ctx, c, TraitDefRefPathZh); err != nil { - fmt.Println(err) - os.Exit(1) + if *location == "" || *location == "zh" { + ref.I18N = &docgen.Zh + ref.CustomDocHeader = CustomTraitHeaderZH + if err := ref.GenerateReferenceDocs(ctx, c, TraitDefRefPathZh); err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), TraitDefRefPathZh) } - fmt.Printf("trait reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), TraitDefRefPathZh) } } diff --git a/references/docgen/parser.go b/references/docgen/parser.go index b72904ec5..8e9294a3c 100644 --- a/references/docgen/parser.go +++ b/references/docgen/parser.go @@ -20,7 +20,9 @@ import ( "context" "encoding/json" "fmt" + "io/fs" "os" + "path/filepath" "sort" "strconv" "strings" @@ -30,6 +32,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/olekukonko/tablewriter" "github.com/pkg/errors" + "github.com/rogpeppe/go-internal/modfile" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/klog/v2" @@ -65,11 +68,13 @@ func (ref *ParseReference) getCapabilities(ctx context.Context, c common.Args) ( ) switch { case ref.Local != nil: - lcap, err := ParseLocalFile(ref.Local.Path, c) + lcaps, err := ParseLocalFiles(ref.Local.Path, c) if err != nil { return nil, fmt.Errorf("failed to get capability from local file %s: %w", ref.DefinitionName, err) } - caps = append(caps, *lcap) + for _, lcap := range lcaps { + caps = append(caps, *lcap) + } case ref.Remote != nil: config, err := c.GetConfig() if err != nil { @@ -485,7 +490,41 @@ func (ref *ParseReference) parseTerraformCapabilityParameters(capability types.C return tables, outputsTables, nil } -// ParseLocalFile parse the local file and get name, configuration from local ComponentDefinition file +// ParseLocalFiles parse the local file and get name, configuration from local ComponentDefinition file +func ParseLocalFiles(localFilePath string, c common.Args) ([]*types.Capability, error) { + lcaps := make([]*types.Capability, 0) + if modfile.IsDirectoryPath(localFilePath) { + // walk the dir and get files + err := filepath.WalkDir(localFilePath, func(path string, info fs.DirEntry, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + if !strings.HasSuffix(info.Name(), ".yaml") && !strings.HasSuffix(info.Name(), ".cue") { + return nil + } + lcap, err := ParseLocalFile(path, c) + if err != nil { + return err + } + lcaps = append(lcaps, lcap) + return nil + }) + if err != nil { + return nil, err + } + } else { + lcap, err := ParseLocalFile(localFilePath, c) + if err != nil { + return nil, err + } + lcaps = append(lcaps, lcap) + } + return lcaps, nil +} + func ParseLocalFile(localFilePath string, c common.Args) (*types.Capability, error) { data, err := pkgUtils.ReadRemoteOrLocalPath(localFilePath, false) if err != nil { @@ -537,6 +576,7 @@ func ParseLocalFile(localFilePath string, c common.Args) (*types.Capability, err return nil, errors.Wrapf(err, "fail to parse definition to capability") } return &lcap, nil + } // WalkParameterSchema will extract properties from *openapi3.Schema