-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.go
185 lines (148 loc) · 4.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2023, Unikraft GmbH, The Cobra Authors and KraftKit Authors.
// Licensed under the Apache-2.0 License (the "License").
// You may not use this file except in compliance with the License.
package main
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
kraft "kraftkit.sh/internal/cli/kraft"
"github.com/spf13/cobra"
)
func main() {
if len(os.Args[1:]) == 0 {
fmt.Printf("usage: %s outdir\n", os.Args[0])
os.Exit(1)
}
outdir := os.Args[1]
if err := os.MkdirAll(outdir, 0o775); err != nil {
fmt.Println("could not create parent directories: %w", err)
os.Exit(1)
}
if err := generateMarkdown(kraft.NewCmd(), outdir); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func generateMarkdown(cmd *cobra.Command, dir string) error {
for _, c := range cmd.Commands() {
if !c.HasSubCommands() && (!c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand()) {
continue
}
if err := generateMarkdown(c, dir); err != nil {
return err
}
}
cmd.InitDefaultHelpCmd()
cmd.InitDefaultHelpFlag()
buf := new(bytes.Buffer)
name := cmd.CommandPath()
buf.WriteString("---\n")
buf.WriteString("title: \"" + name + "\"\n")
buf.WriteString("description: " + cmd.Short + "\n")
buf.WriteString("---\n\n")
if len(cmd.Long) > 0 {
if cmd == cmd.Root() {
buf.WriteString("```\n")
}
buf.WriteString(cmd.Long + "\n\n")
if cmd == cmd.Root() {
buf.WriteString("```\n")
}
}
if cmd.Runnable() {
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))
}
if len(cmd.Example) > 0 {
buf.WriteString("### Examples\n\n")
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.Example))
}
if err := printOptions(buf, cmd); err != nil {
return err
}
if hasSeeAlso(cmd) {
buf.WriteString("## See Also\n\n")
if cmd.HasParent() {
parent := cmd.Parent()
pname := parent.CommandPath()
link := "/docs/cli/reference/" + strings.ReplaceAll(pname, " ", "/")
buf.WriteString(fmt.Sprintf("* [`%s`](%s): %s\n", pname, link, parent.Short))
cmd.VisitParents(func(c *cobra.Command) {
if c.DisableAutoGenTag {
cmd.DisableAutoGenTag = c.DisableAutoGenTag
}
})
}
children := cmd.Commands()
sort.Sort(byName(children))
for _, child := range children {
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
continue
}
cname := name + " " + child.Name()
link := "/docs/cli/reference/" + cname
link = strings.ReplaceAll(link, " ", "/")
buf.WriteString(fmt.Sprintf("* [`%s`](%s): %s\n", cname, link, child.Short))
}
buf.WriteString("\n")
}
basename := strings.ReplaceAll(cmd.CommandPath(), " ", "/")
if cmd.HasSubCommands() {
basename += "/index"
}
basename += ".mdx"
filename := filepath.Join(dir, basename)
fmt.Printf("mkdir: %s\n", filepath.Dir(filename))
if err := os.MkdirAll(filepath.Dir(filename), fs.ModeSetgid|0o775); err != nil {
return err
}
fmt.Printf("write: %s\n", filename)
w, err := os.Create(filename)
if err != nil {
return err
}
defer w.Close()
_, err = buf.WriteTo(w)
return err
}
func printOptions(buf *bytes.Buffer, cmd *cobra.Command) error {
flags := cmd.NonInheritedFlags()
flags.SetOutput(buf)
if flags.HasAvailableFlags() {
buf.WriteString("## Options\n\n```\n")
flags.PrintDefaults()
buf.WriteString("```\n\n")
}
parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(buf)
if parentFlags.HasAvailableFlags() {
buf.WriteString("## Options inherited from parent commands\n\n```\n")
parentFlags.PrintDefaults()
buf.WriteString("```\n\n")
}
return nil
}
// Test to see if we have a reason to print See Also information in docs
// Basically this is a test for a parent command or a subcommand which is
// both not deprecated and not the autogenerated help command.
func hasSeeAlso(cmd *cobra.Command) bool {
if cmd.HasParent() {
return true
}
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
return true
}
return false
}
type byName []*cobra.Command
func (s byName) Len() int { return len(s) }
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }