sigs.k8s.io/cluster-api-provider-aws@v1.5.5/docs/book/cmd/clusterawsadmdocs/main.go (about) 1 /* 2 Copyright 2021 The Kubernetes 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 "fmt" 21 "log" 22 "sort" 23 "strings" 24 25 "github.com/spf13/cobra" 26 "github.com/spf13/cobra/doc" 27 28 "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd" 29 ) 30 31 type byName []*cobra.Command 32 33 func (s byName) Len() int { return len(s) } 34 func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 35 func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() } 36 37 type commandLeaf struct { 38 name string 39 link string 40 subcommands map[string]commandLeaf 41 } 42 43 func main() { 44 root := cmd.RootCmd() 45 err := doc.GenMarkdownTree(root, "./src/clusterawsadm") 46 if err != nil { 47 log.Fatal(err) 48 } 49 50 tree := commandLeaf{ 51 name: "clusterawsadm command reference", 52 subcommands: make(map[string]commandLeaf), 53 link: "clusterawsadm/clusterawsadm", 54 } 55 buildCommandTree(tree, root) 56 commandSummary(tree, 0) 57 } 58 59 func commandSummary(tree commandLeaf, prefix int) { 60 prefixStr := strings.Repeat(" ", prefix) + "- " 61 title := "[" + tree.name + "]" 62 link := "(" + tree.link + ".md)" 63 fmt.Println(prefixStr + title + link) 64 for _, cmds := range tree.subcommands { 65 commandSummary(cmds, prefix+2) 66 } 67 } 68 69 func buildCommandTree(tree commandLeaf, cmd *cobra.Command) { 70 children := cmd.Commands() 71 72 sort.Sort(byName(children)) 73 for _, child := range children { 74 if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() { 75 continue 76 } 77 name := child.Name() 78 components := strings.Split(name, " ") 79 leaf := tree 80 for _, c := range components { 81 newLeaf, ok := leaf.subcommands[c] 82 if !ok { 83 newLeaf = commandLeaf{ 84 name: c, 85 subcommands: make(map[string]commandLeaf), 86 link: leaf.link + "_" + c, 87 } 88 leaf.subcommands[c] = newLeaf 89 } 90 leaf = newLeaf 91 buildCommandTree(leaf, child) 92 } 93 } 94 }