github.com/iasthc/atlas/cmd/atlas@v0.0.0-20230523071841-73246df3f88d/doc.go (about)

     1  // Copyright 2021-present The Atlas Authors. All rights reserved.
     2  // This source code is licensed under the Apache 2.0 license found
     3  // in the LICENSE file in the root directory of this source tree.
     4  
     5  //go:build ignore
     6  // +build ignore
     7  
     8  package main
     9  
    10  import (
    11  	"log"
    12  	"os"
    13  	"strings"
    14  	"text/template"
    15  
    16  	"github.com/iasthc/atlas/cmd/atlas/internal/cmdapi"
    17  
    18  	"github.com/spf13/cobra"
    19  )
    20  
    21  func main() {
    22  	f, err := os.Create("../../doc/md/reference.md")
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  	t, err := template.New("").
    27  		Funcs(template.FuncMap{
    28  			"header": func(depth int) string {
    29  				return strings.Repeat("#", depth+1)
    30  			},
    31  			"subheader": func(depth int) string {
    32  				return strings.Repeat("#", depth+2)
    33  			},
    34  		}).
    35  		ParseFiles("doc.tmpl")
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	if err := t.ExecuteTemplate(f, "header", nil); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  	blocks := prepare(cmdapi.Root, make([]*block, 0), 0)
    43  	if err := t.ExecuteTemplate(f, "body", struct {
    44  		Blocks []*block
    45  	}{Blocks: blocks}); err != nil {
    46  		log.Fatal(err)
    47  	}
    48  }
    49  
    50  type block struct {
    51  	Depth int
    52  	*cobra.Command
    53  }
    54  
    55  func prepare(cmd *cobra.Command, existing []*block, depth int) []*block {
    56  	if depth > 0 {
    57  		existing = append(existing, &block{
    58  			Depth:   depth,
    59  			Command: cmd,
    60  		})
    61  	}
    62  	for _, child := range cmd.Commands() {
    63  		existing = prepare(child, existing, depth+1)
    64  	}
    65  	return existing
    66  }