Skip to content

Commit

Permalink
feat: Adds an output to docs files (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahsimpers authored Mar 16, 2023
1 parent 3703581 commit 973c80b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
6 changes: 3 additions & 3 deletions cobra2snooty.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ func GenDocs(cmd *cobra.Command, w io.Writer) error {
buf.WriteString("\n" + long + "\n")
}

requiredRole(buf, cmd)
buf.WriteString("\n")

if cmd.Runnable() {
buf.WriteString(syntaxHeader)
buf.WriteString(fmt.Sprintf("\n %s\n\n", strings.ReplaceAll(cmd.UseLine(), "[flags]", "[options]")))
Expand All @@ -115,6 +112,9 @@ func GenDocs(cmd *cobra.Command, w io.Writer) error {
}
printOptions(buf, cmd)

printOutputCreate(buf, cmd)
buf.WriteString("\n")

if cmd.Example != "" {
printExamples(buf, cmd)
}
Expand Down
66 changes: 66 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2022 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cobra2snooty

import (
"bytes"
"fmt"
"strings"
"text/tabwriter"

"github.com/spf13/cobra"
)

const (
outputHeader = `Output
------
`
)

const (
tabwriterMinWidth = 6
tabwriterWidth = 4
tabwriterPadding = 3
tabwriterPadChar = ' '
)

// This function can return the output for all commands when the output template is added as an annotation in the command file

func printOutputCreate(buf *bytes.Buffer, cmd *cobra.Command) {
if cmd.Annotations["output"] == "" {
return
}

output := strings.ReplaceAll(cmd.Annotations["output"], "{{range .Results}}", "")
output = strings.ReplaceAll(output, "{{end}}", "")
output = strings.ReplaceAll(output, "{{.", "<")
output = strings.ReplaceAll(output, "}}", ">")
output = strings.ReplaceAll(output, "%s", "<Name>")
output = strings.Replace(output, " ", "", 1)
output = strings.ReplaceAll(output, "\n", "\n ")
w := new(tabwriter.Writer)
w.Init(buf, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, 0)

buf.WriteString(outputHeader)
buf.WriteString(`
If the command succeeds, the CLI returns output similar to the following sample. Values in brackets represent your values.
.. code-block::
`)
fmt.Fprintln(w, " "+output)
w.Flush()
buf.WriteString("\n")
}
28 changes: 0 additions & 28 deletions required_role.go

This file was deleted.

0 comments on commit 973c80b

Please sign in to comment.