Skip to content

Commit

Permalink
task: organization
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn committed Jun 28, 2021
1 parent 0bb3b5d commit 9927b6b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
51 changes: 13 additions & 38 deletions cobra2snooty.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package rest was mostly inspired by github.com/spf13/cobra/doc
// Package cobra2snooty was mostly inspired by https://github.com/spf13/cobra/tree/master/doc
// but with some changes to match the expected formats and styles of our writers and tools.
package rest
package cobra2snooty

import (
"bytes"
Expand All @@ -34,13 +34,13 @@ const (
defaultExtension = ".txt"
)

// GenReSTTree generates the docs for the full tree of commands.
func GenReSTTree(cmd *cobra.Command, dir string) error {
// GenSnootyTree generates the docs for the full tree of commands.
func GenSnootyTree(cmd *cobra.Command, dir string) error {
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
if err := GenReSTTree(c, dir); err != nil {
if err := GenSnootyTree(c, dir); err != nil {
return err
}
}
Expand All @@ -53,7 +53,7 @@ func GenReSTTree(cmd *cobra.Command, dir string) error {
}
defer f.Close()

return GenReSTCustom(cmd, f)
return GenSnootyCustom(cmd, f)
}

const toc = `
Expand Down Expand Up @@ -83,9 +83,9 @@ const tocHeader = `
:titlesonly:
`

// GenReSTCustom creates custom reStructured Text output.
// Adapted from github.com/spf13/cobra/doc to match MongoDB tooling and style.
func GenReSTCustom(cmd *cobra.Command, w io.Writer) error {
// GenSnootyCustom creates custom reStructured Text output.
// Adapted from https://github.com/spf13/cobra/tree/master/doc to match MongoDB tooling and style.
func GenSnootyCustom(cmd *cobra.Command, w io.Writer) error {
cmd.InitDefaultHelpCmd()
cmd.InitDefaultHelpFlag()

Expand All @@ -109,8 +109,8 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer) error {
buf.WriteString(syntaxHeader)
buf.WriteString(fmt.Sprintf("\n %s\n\n", strings.ReplaceAll(cmd.UseLine(), "[flags]", "[options]")))
}
printArgsReST(buf, cmd)
printOptionsReST(buf, cmd)
printArgsSnooty(buf, cmd)
printOptionsSnooty(buf, cmd)

if len(cmd.Example) > 0 {
buf.WriteString(examplesHeader)
Expand Down Expand Up @@ -187,7 +187,7 @@ const optionsHeader = `.. list-table::
- Description
`

func printArgsReST(buf *bytes.Buffer, cmd *cobra.Command) {
func printArgsSnooty(buf *bytes.Buffer, cmd *cobra.Command) {
if args, ok := cmd.Annotations["args"]; ok {
buf.WriteString("Arguments\n")
buf.WriteString("---------\n\n")
Expand All @@ -207,7 +207,7 @@ func printArgsReST(buf *bytes.Buffer, cmd *cobra.Command) {
}
}

func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command) {
func printOptionsSnooty(buf *bytes.Buffer, cmd *cobra.Command) {
flags := cmd.NonInheritedFlags()
if flags.HasAvailableFlags() {
buf.WriteString("Options\n")
Expand All @@ -226,28 +226,3 @@ func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command) {
buf.WriteString("\n")
}
}

// adapted from: https://github.com/kr/text/blob/main/indent.go
func indentString(s, p string) string {
var res []byte
b := []byte(s)
prefix := []byte(p)
bol := true
for _, c := range b {
if bol && c != '\n' {
res = append(res, prefix...)
}
res = append(res, c)
bol = c == '\n'
}
return string(res)
}

func stringInSlice(a []string, x string) bool {
for _, b := range a {
if b == x {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package rest
package cobra2snooty

import (
"bytes"
Expand Down
26 changes: 26 additions & 0 deletions strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cobra2snooty

// adapted from: https://github.com/kr/text/blob/main/indent.go
func indentString(s, p string) string {
var res []byte
b := []byte(s)
prefix := []byte(p)
bol := true
for _, c := range b {
if bol && c != '\n' {
res = append(res, prefix...)
}
res = append(res, c)
bol = c == '\n'
}
return string(res)
}

func stringInSlice(a []string, x string) bool {
for _, b := range a {
if b == x {
return true
}
}
return false
}

0 comments on commit 9927b6b

Please sign in to comment.