Skip to content

Commit cf01b74

Browse files
Working on getting command data output as a nice json string
1 parent bd5a0eb commit cf01b74

376 files changed

Lines changed: 70 additions & 11104 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/index.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

docs/main.go

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"fmt"
55
"os"
66
"strings"
7-
"sort"
7+
"encoding/json"
88
"io/ioutil"
99
"path/filepath"
1010
// "sort"
1111
"github.com/spf13/cobra/doc"
1212
"github.com/spf13/cobra"
13+
"github.com/spf13/pflag"
1314
// "github.com/IBM-Cloud/ibm-cloud-cli-sdk/plugin"
1415
sl_plugin "github.ibm.com/SoftLayer/softlayer-cli/plugin"
1516
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
@@ -35,48 +36,81 @@ func main() {
3536
return
3637
}
3738

39+
// For top level commands, like `sl account` or `sl hardware`
40+
type SlCmdGroup struct {
41+
Name string
42+
Commands []SlCmdDoc
43+
Help string
44+
}
45+
46+
// For specific commands
47+
type SlCmdDoc struct {
48+
Name string
49+
Use string
50+
Flags []SlCmdFlag
51+
Help string
52+
LongHelp string
53+
}
54+
55+
// For a commands flags
56+
type SlCmdFlag struct {
57+
Name string
58+
Help string
59+
}
60+
3861
// This function builds the documentation for IBMCLOUD docs
3962
func CliDocs() {
40-
fmt.Printf("IBMCLOUD SL Command Directory\n")
63+
// fmt.Printf("IBMCLOUD SL Command Directory\n")
64+
SlCommands := sl_plugin.GetTopCobraCommand(nil, nil)
65+
CmdGroups := []SlCmdGroup{}
66+
for _, iCmd := range SlCommands.Commands() {
67+
thisCmdGroup := SlCmdGroup{
68+
Name: iCmd.Name(),
69+
Commands: nil,
70+
Help: iCmd.Short,
71+
}
72+
if len(iCmd.Commands()) > 0 {
73+
thisCmdGroup.Commands = buildSlCmdDoc(iCmd)
74+
}
75+
CmdGroups = append(CmdGroups, thisCmdGroup)
76+
}
77+
jOut, err := json.Marshal(CmdGroups)
78+
if err != nil {
79+
fmt.Println(err)
80+
return
81+
}
82+
fmt.Println(string(jOut))
83+
}
4184

42-
slPlugin := new(sl_plugin.SoftlayerPlugin)
43-
slMeta := slPlugin.GetMetadata()
44-
sort.Slice(slMeta.Commands, func(i, j int) bool {
45-
one := fmt.Sprintf("%s %s", slMeta.Commands[i].Namespace, slMeta.Commands[i].Name)
46-
two := fmt.Sprintf("%s %s", slMeta.Commands[j].Namespace, slMeta.Commands[j].Name)
47-
return one < two
48-
})
49-
fmt.Printf("==============================================================\n")
50-
fileName := ""
51-
fileContent := ""
52-
// TODO: call-api, version and metadata need a special case or something for filename...
53-
for _, slCmd := range slMeta.Commands {
54-
thisFileName := fmt.Sprintf("cli_%s.md", slCmd.Namespace)
55-
thisFileName = strings.ReplaceAll(thisFileName, " ", "_")
56-
if thisFileName != fileName {
57-
fileName = thisFileName
58-
fmt.Printf("NameSpace: %s Name: %s FIleName: %s\n", slCmd.Namespace, slCmd.Name, fileName)
59-
if fileContent != "" {
60-
fmt.Printf("Here is where I would write out to a file...\n")
61-
fileContent = ""
62-
}
63-
}
64-
65-
sort.Slice(slCmd.Flags, func(i, j int) bool {
66-
return slCmd.Flags[i].Name < slCmd.Flags[j].Name
67-
})
68-
// for _, slCmdFlag := range slCmd.Flags {
69-
// fmt.Printf("\tFlag: %s: %s\n", slCmdFlag.Name, slCmdFlag.Description)
85+
func buildSlCmdDoc(topCommand *cobra.Command) []SlCmdDoc {
86+
docs := []SlCmdDoc{}
87+
for _, iCmd := range topCommand.Commands() {
88+
thisDoc := SlCmdDoc{
89+
Name: iCmd.Name(),
90+
Use: iCmd.Use,
91+
Flags: nil,
92+
Help: iCmd.Short,
93+
LongHelp: iCmd.Long,
94+
}
95+
thisDoc.Flags = buildSlCmdFlag(iCmd)
7096

71-
// }
72-
// fmt.Printf("\t--------------------------------\n")
73-
// fmt.Printf("\tDescription: %s\n", slCmd.Description)
74-
// fmt.Printf("\t--------------------------------\n")
75-
// fmt.Printf("\tUsage: %s\n", slCmd.Usage)
76-
// fmt.Printf("==============================================================\n")
97+
docs = append(docs, thisDoc)
7798
}
99+
return docs
78100
}
79101

102+
func buildSlCmdFlag(topCommand *cobra.Command) []SlCmdFlag {
103+
flags := []SlCmdFlag{}
104+
flagSet := topCommand.Flags()
105+
flagSet.VisitAll(func(pflag *pflag.Flag) {
106+
thisFlag := SlCmdFlag{
107+
Name: pflag.Name,
108+
Help: pflag.Usage,
109+
}
110+
flags = append(flags, thisFlag)
111+
})
112+
return flags
113+
}
80114

81115
// This function uses the build in Cobra documentation generator, its fine.
82116
func CobraDocs() {

docs/sl.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

docs/sl_account.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

docs/sl_account_billing-items.md

Lines changed: 0 additions & 28 deletions
This file was deleted.

docs/sl_account_cancel-item.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

docs/sl_account_event-detail.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

docs/sl_account_events.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)