|
| 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 | +} |
0 commit comments