github.com/argoproj/argo-cd/v3@v3.2.1/hack/gen-docs/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"sort"
     9  	"strings"
    10  
    11  	"gopkg.in/yaml.v2"
    12  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    13  
    14  	"github.com/argoproj/notifications-engine/pkg/docs"
    15  )
    16  
    17  func main() {
    18  	generateNotificationsDocs()
    19  }
    20  
    21  func generateNotificationsDocs() {
    22  	_ = os.RemoveAll("./docs/operator-manual/notifications/services")
    23  	_ = os.MkdirAll("./docs/operator-manual/notifications/services", 0o755)
    24  	files, err := docs.CopyServicesDocs("./docs/operator-manual/notifications/services")
    25  	if err != nil {
    26  		log.Fatal(err)
    27  	}
    28  	if files != nil {
    29  		if e := updateMkDocsNav("Operator Manual", "Notifications", "Notification Services", files); e != nil {
    30  			log.Fatal(e)
    31  		}
    32  	}
    33  }
    34  
    35  func updateMkDocsNav(parent string, child string, subchild string, files []string) error {
    36  	trimPrefixes(files, "docs/")
    37  	sort.Strings(files)
    38  	data, err := os.ReadFile("mkdocs.yml")
    39  	if err != nil {
    40  		return fmt.Errorf("error reading mkdocs.yml: %w", err)
    41  	}
    42  	var un unstructured.Unstructured
    43  	if e := yaml.Unmarshal(data, &un.Object); e != nil {
    44  		return e
    45  	}
    46  	nav := un.Object["nav"].([]any)
    47  	rootitem, _ := findNavItem(nav, parent)
    48  	if rootitem == nil {
    49  		return fmt.Errorf("can't find '%s' root item in mkdoc.yml", parent)
    50  	}
    51  	rootnavitemmap := rootitem.(map[any]any)
    52  	childnav, _ := findNavItem(rootnavitemmap[parent].([]any), child)
    53  	if childnav == nil {
    54  		return fmt.Errorf("can't find '%s' chile item under '%s' parent item in mkdoc.yml", child, parent)
    55  	}
    56  
    57  	childnavmap := childnav.(map[any]any)
    58  	childnavitems := childnavmap[child].([]any)
    59  
    60  	childnavitems = removeNavItem(childnavitems, subchild)
    61  	commands := make(map[string]any)
    62  	commands[subchild] = files
    63  	childnavmap[child] = append(childnavitems, commands)
    64  	newmkdocs, err := yaml.Marshal(un.Object)
    65  	if err != nil {
    66  		return fmt.Errorf("error in marshaling final configmap: %w", err)
    67  	}
    68  
    69  	// The marshaller drops custom tags, so re-add this one. Turns out this is much less invasive than trying to handle
    70  	// it at the YAML parser level.
    71  	newmkdocs = bytes.Replace(newmkdocs, []byte("site_url: READTHEDOCS_CANONICAL_URL"), []byte("site_url: !ENV READTHEDOCS_CANONICAL_URL"), 1)
    72  
    73  	return os.WriteFile("mkdocs.yml", newmkdocs, 0o644)
    74  }
    75  
    76  func trimPrefixes(files []string, prefix string) {
    77  	for i, f := range files {
    78  		files[i] = strings.TrimPrefix(f, prefix)
    79  	}
    80  }
    81  
    82  func findNavItem(nav []any, key string) (any, int) {
    83  	for i, item := range nav {
    84  		o, ismap := item.(map[any]any)
    85  		if ismap {
    86  			if _, ok := o[key]; ok {
    87  				return o, i
    88  			}
    89  		}
    90  	}
    91  	return nil, -1
    92  }
    93  
    94  func removeNavItem(nav []any, key string) []any {
    95  	_, i := findNavItem(nav, key)
    96  	if i != -1 {
    97  		nav = append(nav[:i], nav[i+1:]...)
    98  	}
    99  	return nav
   100  }