Skip to content

Commit 25ef347

Browse files
working on generating documentation markdown
1 parent cf01b74 commit 25ef347

1 file changed

Lines changed: 60 additions & 19 deletions

File tree

docs/main.go

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"io/ioutil"
99
"path/filepath"
10+
"text/template"
1011
// "sort"
1112
"github.com/spf13/cobra/doc"
1213
"github.com/spf13/cobra"
@@ -30,22 +31,28 @@ var rootCmd = &cobra.Command{
3031

3132
func main() {
3233
err := rootCmd.Execute()
34+
checkError(err)
35+
return
36+
}
37+
38+
func checkError(err error) {
3339
if err != nil {
34-
fmt.Printf(err.Error())
40+
panic(err)
3541
}
36-
return
3742
}
3843

3944
// For top level commands, like `sl account` or `sl hardware`
4045
type SlCmdGroup struct {
4146
Name string
47+
CommandShortLink string
4248
Commands []SlCmdDoc
4349
Help string
4450
}
4551

4652
// For specific commands
4753
type SlCmdDoc struct {
4854
Name string
55+
CommandShortLink string
4956
Use string
5057
Flags []SlCmdFlag
5158
Help string
@@ -58,35 +65,77 @@ type SlCmdFlag struct {
5865
Help string
5966
}
6067

68+
69+
6170
// This function builds the documentation for IBMCLOUD docs
6271
func CliDocs() {
6372
// fmt.Printf("IBMCLOUD SL Command Directory\n")
6473
SlCommands := sl_plugin.GetTopCobraCommand(nil, nil)
6574
CmdGroups := []SlCmdGroup{}
6675
for _, iCmd := range SlCommands.Commands() {
76+
shortName := strings.ReplaceAll(iCmd.Name(), " ", "_")
77+
shortName = strings.ReplaceAll(iCmd.Name(), "-", "_")
6778
thisCmdGroup := SlCmdGroup{
6879
Name: iCmd.Name(),
80+
CommandShortLink: fmt.Sprintf("sl_%v", shortName),
6981
Commands: nil,
7082
Help: iCmd.Short,
7183
}
7284
if len(iCmd.Commands()) > 0 {
7385
thisCmdGroup.Commands = buildSlCmdDoc(iCmd)
7486
}
87+
PrintMakrdown(thisCmdGroup)
7588
CmdGroups = append(CmdGroups, thisCmdGroup)
7689
}
7790
jOut, err := json.Marshal(CmdGroups)
78-
if err != nil {
79-
fmt.Println(err)
80-
return
81-
}
82-
fmt.Println(string(jOut))
91+
os.WriteFile("sl.json", jOut, 0755)
92+
checkError(err)
93+
// fmt.Println(string(jOut))
94+
}
95+
96+
// Generates the Markdown
97+
func PrintMakrdown(cmd SlCmdGroup) {
98+
99+
var cmdTemplate = `
100+
## {{.Name}}
101+
{: #{{.CommandShortLink}}}
102+
103+
{{.Help}}
104+
105+
{{range .Commands}}
106+
#### {{.Name}}
107+
{: #{{.CommandShortLink}}}
108+
109+
{{.Help}}
110+
111+
**Command options**:
112+
{{range .Flags}}
113+
--{{.Name}} {{.Help}}
114+
{{end}}
115+
116+
{{.LongHelp}}
117+
{{end}}
118+
119+
`
120+
mdTemplate, err := template.New("cmd template").Parse(cmdTemplate)
121+
checkError(err)
122+
filename := fmt.Sprintf("%v.md", cmd.CommandShortLink)
123+
outfile, err := os.Create(filename)
124+
defer outfile.Close()
125+
err = mdTemplate.Execute(outfile, cmd)
126+
checkError(err)
127+
83128
}
84129

130+
85131
func buildSlCmdDoc(topCommand *cobra.Command) []SlCmdDoc {
86132
docs := []SlCmdDoc{}
87133
for _, iCmd := range topCommand.Commands() {
134+
shortName := strings.ReplaceAll(iCmd.Name(), " ", "_")
135+
shortName = strings.ReplaceAll(iCmd.Name(), "-", "_")
88136
thisDoc := SlCmdDoc{
89137
Name: iCmd.Name(),
138+
CommandShortLink: shortName,
90139
Use: iCmd.Use,
91140
Flags: nil,
92141
Help: iCmd.Short,
@@ -123,31 +172,23 @@ func CobraDocs() {
123172
slMeta := sl_plugin.GetTopCobraCommand(fakeUI, fakeSession)
124173

125174
cwd, err := os.Getwd()
126-
if err != nil {
127-
fmt.Printf(err.Error())
128-
}
175+
checkError(err)
129176
if !strings.HasSuffix(filepath.ToSlash(cwd), "softlayer-cli/docs") {
130177
fmt.Printf("%v is the wrong directory, you need to run this command in the softlayer-cli/docs directory.\n", cwd)
131178

132179
return
133180
}
134181
err = doc.GenMarkdownTree(slMeta, "./")
135-
if err != nil {
136-
fmt.Printf(err.Error())
137-
}
182+
checkError(err)
138183
// err = os.Rename("./sl.md", "./index.md")
139184
// if err != nil {
140185
// fmt.Errorf(err.Error())
141186
// }
142187
// Need to make sure we have an index file
143188
bytesRead, err := ioutil.ReadFile("./sl.md")
144-
if err != nil {
145-
fmt.Printf(err.Error())
146-
}
189+
checkError(err)
147190
err = ioutil.WriteFile("./index.md", bytesRead, 0755)
148-
if err != nil {
149-
fmt.Printf(err.Error())
150-
}
191+
checkError(err)
151192
fmt.Printf("Jobs done.\n")
152193

153194
}

0 commit comments

Comments
 (0)