github.com/oam-dev/kubevela@v1.9.11/hack/docgen/def/gen.go (about)

     1  /*
     2   Copyright 2022 The KubeVela Authors.
     3  
     4   Licensed under the Apache License, Version 2.0 (the "License");
     5   you may not use this file except in compliance with the License.
     6   You may obtain a copy of the License at
     7  
     8   	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   Unless required by applicable law or agreed to in writing, software
    11   distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  
    25  	"github.com/oam-dev/kubevela/apis/types"
    26  	"github.com/oam-dev/kubevela/hack/docgen/def/mods"
    27  	"github.com/oam-dev/kubevela/pkg/utils/common"
    28  	"github.com/oam-dev/kubevela/references/docgen"
    29  )
    30  
    31  func main() {
    32  
    33  	ctx := context.Background()
    34  	c, err := common.InitBaseRestConfig()
    35  	if err != nil {
    36  		fmt.Println(err)
    37  		os.Exit(1)
    38  	}
    39  
    40  	path := flag.String("path", "", "specify the path of output")
    41  	location := flag.String("location", "", "path of output")
    42  	defdir := flag.String("def-dir", "", "path of definition dir")
    43  	tp := flag.String("type", "", "choose one of the definition to print")
    44  	i18nfile := flag.String("i18n", "../kubevela.io/static/reference-i18n.json", "file path of i18n data")
    45  	forceExample := flag.Bool("force-example-doc", false, "example must be provided for definitions")
    46  	flag.Parse()
    47  
    48  	if *i18nfile != "" {
    49  		docgen.LoadI18nData(*i18nfile)
    50  	}
    51  
    52  	if *tp == "" && (*defdir != "" || *path != "") {
    53  		fmt.Println("you must specify a type with definition ref path specified ")
    54  		os.Exit(1)
    55  	}
    56  
    57  	opt := mods.Options{
    58  		Path:          *path,
    59  		Location:      *location,
    60  		DefDirs:       make([]string, 0),
    61  		ForceExamples: *forceExample,
    62  	}
    63  	if *defdir != "" {
    64  		opt.DefDirs = append(opt.DefDirs, *defdir)
    65  	}
    66  
    67  	fmt.Printf("creating docs with args path=%s, location=%s, defdir=%s, type=%s.\n", *path, *location, *defdir, *tp)
    68  	switch types.CapType(*tp) {
    69  	case types.TypeComponentDefinition, "component", "comp":
    70  		mods.ComponentDef(ctx, c, opt)
    71  	case types.TypeTrait:
    72  		mods.TraitDef(ctx, c, opt)
    73  	case types.TypePolicy:
    74  		mods.PolicyDef(ctx, c, opt)
    75  	case types.TypeWorkflowStep, "workflow", "wf":
    76  		mods.WorkflowDef(ctx, c, opt)
    77  	case "":
    78  		mods.ComponentDef(ctx, c, opt)
    79  		mods.TraitDef(ctx, c, opt)
    80  		mods.PolicyDef(ctx, c, opt)
    81  		mods.WorkflowDef(ctx, c, opt)
    82  	default:
    83  		fmt.Printf("type %s not supported\n", *tp)
    84  		os.Exit(1)
    85  	}
    86  
    87  }