github.com/oam-dev/kubevela@v1.9.11/references/docgen/embddoc.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 docgen
    18  
    19  import (
    20  	"embed"
    21  	"io/fs"
    22  	"strings"
    23  
    24  	"k8s.io/klog/v2"
    25  )
    26  
    27  //go:embed def-doc
    28  var defdoc embed.FS
    29  
    30  // DefinitionDocSamples stores the configuration yaml sample for capabilities
    31  var DefinitionDocSamples = map[string]string{}
    32  
    33  // DefinitionDocDescription stores the description for capabilities
    34  var DefinitionDocDescription = map[string]string{}
    35  
    36  // DefinitionDocParameters stores the parameters for capabilities, it will override the generated one
    37  var DefinitionDocParameters = map[string]string{}
    38  
    39  const (
    40  	suffixSample      = ".eg.md"
    41  	suffixParameter   = ".param.md"
    42  	suffixDescription = ".desc.md"
    43  )
    44  
    45  func init() {
    46  	err := fs.WalkDir(defdoc, "def-doc", func(path string, d fs.DirEntry, err error) error {
    47  		if d.IsDir() {
    48  			return nil
    49  		}
    50  		switch {
    51  		case strings.HasSuffix(d.Name(), suffixSample):
    52  			data, err := defdoc.ReadFile(path)
    53  			if err != nil {
    54  				klog.ErrorS(err, "ignore this embed built-in definition sample", "path", path)
    55  				return nil
    56  			}
    57  			DefinitionDocSamples[strings.TrimSuffix(d.Name(), suffixSample)] = strings.TrimSpace(string(data))
    58  		case strings.HasSuffix(d.Name(), suffixDescription):
    59  			data, err := defdoc.ReadFile(path)
    60  			if err != nil {
    61  				klog.ErrorS(err, "ignore this embed built-in definition description", "path", path)
    62  				return nil
    63  			}
    64  			DefinitionDocDescription[strings.TrimSuffix(d.Name(), suffixDescription)] = strings.TrimSpace(string(data))
    65  		case strings.HasSuffix(d.Name(), suffixParameter):
    66  			data, err := defdoc.ReadFile(path)
    67  			if err != nil {
    68  				klog.ErrorS(err, "ignore this embed built-in definition parameter", "path", path)
    69  				return nil
    70  			}
    71  			DefinitionDocParameters[strings.TrimSuffix(d.Name(), suffixParameter)] = strings.TrimSpace(string(data))
    72  		}
    73  		return nil
    74  	})
    75  	if err != nil {
    76  		klog.ErrorS(err, "unable to read embed built-in definition documentation")
    77  		return
    78  	}
    79  }