Skip to content

Commit c0d902e

Browse files
improvements to the doc builder
1 parent 46cd10e commit c0d902e

1 file changed

Lines changed: 19 additions & 44 deletions

File tree

docs/main.go

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ import (
55
"os"
66
"strings"
77
"encoding/json"
8-
"io/ioutil"
9-
"path/filepath"
8+
109
"text/template"
1110
// "sort"
12-
"github.com/spf13/cobra/doc"
1311
"github.com/spf13/cobra"
1412
"github.com/spf13/pflag"
1513
// "github.com/IBM-Cloud/ibm-cloud-cli-sdk/plugin"
1614
sl_plugin "github.ibm.com/SoftLayer/softlayer-cli/plugin"
17-
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
18-
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
19-
"github.com/softlayer/softlayer-go/session"
2015
)
2116

2217
var fileName string
@@ -65,6 +60,7 @@ type SlCmdDoc struct {
6560
type SlCmdFlag struct {
6661
Name string
6762
Help string
63+
Default string
6864
}
6965

7066

@@ -90,7 +86,7 @@ func CliDocs() {
9086
PrintMakrdown(thisCmdGroup)
9187
CmdGroups = append(CmdGroups, thisCmdGroup)
9288
}
93-
jOut, err := json.Marshal(CmdGroups)
89+
jOut, err := json.MarshalIndent(CmdGroups, "", " ")
9490
os.WriteFile("sl.json", jOut, 0755)
9591
checkError(err)
9692
// fmt.Println(string(jOut))
@@ -119,9 +115,10 @@ ibmcloud {{.Use}}
119115
{: codeblock}
120116
121117
{{if .Flags}}
122-
**Flags**:
118+
**Command options**:
123119
{{range .Flags}}
124-
--{{.Name}} {{.Help}}
120+
--{{.Name}}
121+
: {{.Help}}
125122
{{end}}
126123
{{end}}
127124
{{end}}
@@ -137,22 +134,27 @@ ibmcloud {{.Use}}
137134

138135
}
139136

137+
func getLongHelp(helpString string) string {
138+
helpString = strings.ReplaceAll(helpString, "${COMMAND_NAME}", "ibmcloud")
139+
helpString = strings.ReplaceAll(helpString, "EXAMPLE:", "**Examples**:\n")
140+
return helpString
141+
}
140142

141143
func buildSlCmdDoc(topCommand *cobra.Command) []SlCmdDoc {
142144
docs := []SlCmdDoc{}
143145
for _, iCmd := range topCommand.Commands() {
144146
shortName := fmt.Sprintf("sl_%s_%s", topCommand.Name(), iCmd.Name())
145147
shortName = strings.ReplaceAll(shortName, " ", "_")
146148
shortName = strings.ReplaceAll(shortName, "-", "_")
147-
149+
longHelp := getLongHelp(iCmd.Long)
148150
thisDoc := SlCmdDoc{
149151
Name: iCmd.Name(),
150152
CommandShortLink: shortName,
151153
CommandPath: iCmd.CommandPath(),
152154
Use: iCmd.UseLine(),
153155
Flags: nil,
154156
Help: iCmd.Short,
155-
LongHelp: strings.ReplaceAll(iCmd.Long, "${COMMAND_NAME}", "ibmcloud"),
157+
LongHelp: longHelp,
156158
Backtick: "```",
157159
}
158160
thisDoc.Flags = buildSlCmdFlag(iCmd)
@@ -166,43 +168,16 @@ func buildSlCmdFlag(topCommand *cobra.Command) []SlCmdFlag {
166168
flags := []SlCmdFlag{}
167169
flagSet := topCommand.Flags()
168170
flagSet.VisitAll(func(pflag *pflag.Flag) {
171+
flagName := pflag.Name
172+
if pflag.Shorthand != "" {
173+
flagName = fmt.Sprintf("%s, %s", pflag.Shorthand, flagName)
174+
}
169175
thisFlag := SlCmdFlag{
170-
Name: pflag.Name,
176+
Name:flagName,
171177
Help: pflag.Usage,
178+
Default: pflag.DefValue,
172179
}
173180
flags = append(flags, thisFlag)
174181
})
175182
return flags
176183
}
177-
178-
// This function uses the build in Cobra documentation generator, its fine.
179-
func CobraDocs() {
180-
fmt.Printf("Generating Documentation\n")
181-
182-
var fakeUI *terminal.FakeUI
183-
var fakeSession *session.Session
184-
fakeUI = terminal.NewFakeUI()
185-
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
186-
slMeta := sl_plugin.GetTopCobraCommand(fakeUI, fakeSession)
187-
188-
cwd, err := os.Getwd()
189-
checkError(err)
190-
if !strings.HasSuffix(filepath.ToSlash(cwd), "softlayer-cli/docs") {
191-
fmt.Printf("%v is the wrong directory, you need to run this command in the softlayer-cli/docs directory.\n", cwd)
192-
193-
return
194-
}
195-
err = doc.GenMarkdownTree(slMeta, "./")
196-
checkError(err)
197-
// err = os.Rename("./sl.md", "./index.md")
198-
// if err != nil {
199-
// fmt.Errorf(err.Error())
200-
// }
201-
// Need to make sure we have an index file
202-
bytesRead, err := ioutil.ReadFile("./sl.md")
203-
checkError(err)
204-
err = ioutil.WriteFile("./index.md", bytesRead, 0755)
205-
checkError(err)
206-
fmt.Printf("Jobs done.\n")
207-
208-
}

0 commit comments

Comments
 (0)