Skip to content

Commit 973c80b

Browse files
authored
feat: Adds an output to docs files (#33)
1 parent 3703581 commit 973c80b

File tree

3 files changed

+69
-31
lines changed

3 files changed

+69
-31
lines changed

cobra2snooty.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ func GenDocs(cmd *cobra.Command, w io.Writer) error {
102102
buf.WriteString("\n" + long + "\n")
103103
}
104104

105-
requiredRole(buf, cmd)
106-
buf.WriteString("\n")
107-
108105
if cmd.Runnable() {
109106
buf.WriteString(syntaxHeader)
110107
buf.WriteString(fmt.Sprintf("\n %s\n\n", strings.ReplaceAll(cmd.UseLine(), "[flags]", "[options]")))
@@ -115,6 +112,9 @@ func GenDocs(cmd *cobra.Command, w io.Writer) error {
115112
}
116113
printOptions(buf, cmd)
117114

115+
printOutputCreate(buf, cmd)
116+
buf.WriteString("\n")
117+
118118
if cmd.Example != "" {
119119
printExamples(buf, cmd)
120120
}

output.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2022 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cobra2snooty
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"strings"
21+
"text/tabwriter"
22+
23+
"github.com/spf13/cobra"
24+
)
25+
26+
const (
27+
outputHeader = `Output
28+
------
29+
`
30+
)
31+
32+
const (
33+
tabwriterMinWidth = 6
34+
tabwriterWidth = 4
35+
tabwriterPadding = 3
36+
tabwriterPadChar = ' '
37+
)
38+
39+
// This function can return the output for all commands when the output template is added as an annotation in the command file
40+
41+
func printOutputCreate(buf *bytes.Buffer, cmd *cobra.Command) {
42+
if cmd.Annotations["output"] == "" {
43+
return
44+
}
45+
46+
output := strings.ReplaceAll(cmd.Annotations["output"], "{{range .Results}}", "")
47+
output = strings.ReplaceAll(output, "{{end}}", "")
48+
output = strings.ReplaceAll(output, "{{.", "<")
49+
output = strings.ReplaceAll(output, "}}", ">")
50+
output = strings.ReplaceAll(output, "%s", "<Name>")
51+
output = strings.Replace(output, " ", "", 1)
52+
output = strings.ReplaceAll(output, "\n", "\n ")
53+
w := new(tabwriter.Writer)
54+
w.Init(buf, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, 0)
55+
56+
buf.WriteString(outputHeader)
57+
buf.WriteString(`
58+
If the command succeeds, the CLI returns output similar to the following sample. Values in brackets represent your values.
59+
60+
.. code-block::
61+
62+
`)
63+
fmt.Fprintln(w, " "+output)
64+
w.Flush()
65+
buf.WriteString("\n")
66+
}

required_role.go

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

0 commit comments

Comments
 (0)