github.com/oam-dev/kubevela@v1.9.11/hack/docgen/def/mods/policy.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 mods
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/kubevela/pkg/util/singleton"
    27  
    28  	"github.com/oam-dev/kubevela/apis/types"
    29  	"github.com/oam-dev/kubevela/pkg/utils/common"
    30  	"github.com/oam-dev/kubevela/references/docgen"
    31  )
    32  
    33  const (
    34  	// PolicyDefRefPath is the target path for kubevela.io policy ref docs
    35  	PolicyDefRefPath = "../kubevela.io/docs/end-user/policies/references.md"
    36  	// PolicyDefRefPathZh is the target path for kubevela.io policy ref docs in Chinese
    37  	PolicyDefRefPathZh = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/policies/references.md"
    38  )
    39  
    40  // PolicyDefDirs store inner CUE definition
    41  var PolicyDefDirs = []string{"./vela-templates/definitions/internal/policy/"}
    42  
    43  // CustomPolicyHeaderEN .
    44  var CustomPolicyHeaderEN = `---
    45  title: Built-in Policy Type
    46  ---
    47  
    48  This documentation will walk through all the built-in policy types sorted alphabetically.
    49  
    50  ` + fmt.Sprintf("> It was generated automatically by [scripts](../../contributor/cli-ref-doc), please don't update manually, last updated at %s.\n\n", time.Now().Format(time.RFC3339))
    51  
    52  // CustomPolicyHeaderZH .
    53  var CustomPolicyHeaderZH = `---
    54  title: 内置策略列表
    55  ---
    56  
    57  本文档将**按字典序**展示所有内置策略的参数列表。
    58  
    59  ` + fmt.Sprintf("> 本文档由[脚本](../../contributor/cli-ref-doc)自动生成,请勿手动修改,上次更新于 %s。\n\n", time.Now().Format(time.RFC3339))
    60  
    61  // PolicyDef generate policy def reference doc
    62  func PolicyDef(ctx context.Context, c common.Args, opt Options) {
    63  	if len(opt.DefDirs) == 0 {
    64  		opt.DefDirs = PolicyDefDirs
    65  	}
    66  	ref := &docgen.MarkdownReference{
    67  		AllInOne:     true,
    68  		ForceExample: opt.ForceExamples,
    69  		Filter: func(capability types.Capability) bool {
    70  			if capability.Type != types.TypePolicy || capability.Category != types.CUECategory {
    71  				return false
    72  			}
    73  			if capability.Labels != nil && (capability.Labels[types.LabelDefinitionHidden] == "true" || capability.Labels[types.LabelDefinitionDeprecated] == "true") {
    74  				return false
    75  			}
    76  			// only print capability which contained in cue def
    77  			for _, dir := range opt.DefDirs {
    78  				files, err := os.ReadDir(dir)
    79  				if err != nil {
    80  					fmt.Println("read dir err", opt.DefDirs, err)
    81  					return false
    82  				}
    83  				for _, f := range files {
    84  					if strings.Contains(f.Name(), capability.Name) {
    85  						return true
    86  					}
    87  				}
    88  			}
    89  			return false
    90  		},
    91  		CustomDocHeader: CustomPolicyHeaderEN,
    92  	}
    93  	ref.Local = &docgen.FromLocal{Paths: PolicyDefDirs}
    94  	ref.Client = singleton.KubeClient.Get()
    95  
    96  	if opt.Path != "" {
    97  		ref.I18N = &docgen.En
    98  		if strings.Contains(opt.Location, "zh") || strings.Contains(opt.Location, "chinese") {
    99  			ref.I18N = &docgen.Zh
   100  			ref.CustomDocHeader = CustomPolicyHeaderZH
   101  		}
   102  		if err := ref.GenerateReferenceDocs(ctx, c, opt.Path); err != nil {
   103  			fmt.Println(err)
   104  			os.Exit(1)
   105  		}
   106  		fmt.Printf("policy reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), opt.Path)
   107  		return
   108  	}
   109  	if opt.Location == "" || opt.Location == "en" {
   110  		ref.I18N = &docgen.En
   111  		if err := ref.GenerateReferenceDocs(ctx, c, PolicyDefRefPath); err != nil {
   112  			fmt.Println(err)
   113  			os.Exit(1)
   114  		}
   115  		fmt.Printf("policy reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), PolicyDefRefPath)
   116  	}
   117  	if opt.Location == "" || opt.Location == "zh" {
   118  		ref.I18N = &docgen.Zh
   119  		ref.CustomDocHeader = CustomPolicyHeaderZH
   120  		if err := ref.GenerateReferenceDocs(ctx, c, PolicyDefRefPathZh); err != nil {
   121  			fmt.Println(err)
   122  			os.Exit(1)
   123  		}
   124  		fmt.Printf("policy reference docs (%s) successfully generated in %s \n", ref.I18N.Language(), PolicyDefRefPathZh)
   125  	}
   126  }