From b6dc464193d152730698c8180701643e4dd49be4 Mon Sep 17 00:00:00 2001 From: Sarah Simpers Date: Sun, 12 Mar 2023 15:17:58 -0400 Subject: [PATCH 1/6] Adds an output to docs files --- cobra2snooty.go | 3 +++ output.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 output.go diff --git a/cobra2snooty.go b/cobra2snooty.go index 9959e38..acf6a99 100644 --- a/cobra2snooty.go +++ b/cobra2snooty.go @@ -115,6 +115,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) } diff --git a/output.go b/output.go new file mode 100644 index 0000000..cca5236 --- /dev/null +++ b/output.go @@ -0,0 +1,64 @@ +// 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 +------ +` +) + +// This function can return the output for create and delete commands when the output template is added as an annotation in the command file + +func printOutputCreate(buf *bytes.Buffer, cmd *cobra.Command) { + removerange := strings.ReplaceAll(cmd.Annotations["output"], "{{range .Results}}", "") + removeend := strings.ReplaceAll(removerange, "{{end}}", "") + bracketsremoved1 := strings.ReplaceAll(removeend, "{{.", "<") + bracketsremoved2 := strings.ReplaceAll(bracketsremoved1, "}}", ">") + replacevariable := strings.ReplaceAll(bracketsremoved2, "%s", "") + replacevariable2 := strings.ReplaceAll(replacevariable, "\n", "\n ") + w := new(tabwriter.Writer) + w.Init(buf, 0, 0, 0, ' ', tabwriter.Debug|tabwriter.AlignRight) + + if cmd.Annotations["output"] == "" { + return + } + + buf.WriteString(outputHeader) + buf.WriteString(` +If the command succeeds, the CLI prints a message similar to the following and replaces the values in brackets with your values. The | symbol represents a horizontal tab. + +.. code-block:: + +`) + if strings.HasSuffix(cmd.Annotations["output"], "}"+"\n") { + fmt.Fprintln(w, replacevariable2) + w.Flush() + } else { + buf.WriteString(" ") + fmt.Fprintln(w, replacevariable2) + w.Flush() + } + buf.WriteString("\n") +} From bd3c26cf2f942e7b90f33596dcfdf5ce2b8d2b4b Mon Sep 17 00:00:00 2001 From: Sarah Simpers Date: Sun, 12 Mar 2023 15:34:46 -0400 Subject: [PATCH 2/6] Removes unneeded requiredRole function --- cobra2snooty.go | 3 --- required_role.go | 28 ---------------------------- 2 files changed, 31 deletions(-) delete mode 100644 required_role.go diff --git a/cobra2snooty.go b/cobra2snooty.go index acf6a99..fd78cfb 100644 --- a/cobra2snooty.go +++ b/cobra2snooty.go @@ -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]"))) diff --git a/required_role.go b/required_role.go deleted file mode 100644 index 3cccb95..0000000 --- a/required_role.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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" - - "github.com/spf13/cobra" -) - -func requiredRole(buf *bytes.Buffer, cmd *cobra.Command) { - if cmd.Annotations["requiredRole"] == "" { - return - } - buf.WriteString("\nTo use this command, the requesting user or API key must have the " + cmd.Annotations["requiredRole"] + " role.\n") -} From d95db00025f8234c2f56a0533ed9079ee1fb42c4 Mon Sep 17 00:00:00 2001 From: Sarah Simpers Date: Sun, 12 Mar 2023 15:38:29 -0400 Subject: [PATCH 3/6] Updates note in code --- output.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/output.go b/output.go index cca5236..cf68d30 100644 --- a/output.go +++ b/output.go @@ -29,7 +29,7 @@ const ( ` ) -// This function can return the output for create and delete commands when the output template is added as an annotation in the command file +// 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) { removerange := strings.ReplaceAll(cmd.Annotations["output"], "{{range .Results}}", "") From cef967d6969d572086b9bcaf4a833d7674ae34e7 Mon Sep 17 00:00:00 2001 From: Sarah Simpers Date: Tue, 14 Mar 2023 09:32:36 -0400 Subject: [PATCH 4/6] Includes changes from tech review --- output.go | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/output.go b/output.go index cf68d30..8a96571 100644 --- a/output.go +++ b/output.go @@ -32,33 +32,27 @@ const ( // 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) { - removerange := strings.ReplaceAll(cmd.Annotations["output"], "{{range .Results}}", "") - removeend := strings.ReplaceAll(removerange, "{{end}}", "") - bracketsremoved1 := strings.ReplaceAll(removeend, "{{.", "<") - bracketsremoved2 := strings.ReplaceAll(bracketsremoved1, "}}", ">") - replacevariable := strings.ReplaceAll(bracketsremoved2, "%s", "") - replacevariable2 := strings.ReplaceAll(replacevariable, "\n", "\n ") - w := new(tabwriter.Writer) - w.Init(buf, 0, 0, 0, ' ', tabwriter.Debug|tabwriter.AlignRight) - 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", "") + output = strings.ReplaceAll(output, "\n", "\n ") + w := new(tabwriter.Writer) + w.Init(buf, 6, 4, 3, ' ', 0) + buf.WriteString(outputHeader) buf.WriteString(` -If the command succeeds, the CLI prints a message similar to the following and replaces the values in brackets with your values. The | symbol represents a horizontal tab. +If the command succeeds, the CLI prints a message similar to the following and replaces the values in brackets with your values: .. code-block:: -`) - if strings.HasSuffix(cmd.Annotations["output"], "}"+"\n") { - fmt.Fprintln(w, replacevariable2) - w.Flush() - } else { - buf.WriteString(" ") - fmt.Fprintln(w, replacevariable2) - w.Flush() - } + `) + fmt.Fprintln(w, output) + w.Flush() buf.WriteString("\n") } From 73b2ffcad85bccea792009a5cbf0e6112b1f0ea1 Mon Sep 17 00:00:00 2001 From: Sarah Simpers Date: Tue, 14 Mar 2023 09:37:11 -0400 Subject: [PATCH 5/6] Fixes magic number error --- output.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/output.go b/output.go index 8a96571..ec40674 100644 --- a/output.go +++ b/output.go @@ -29,6 +29,13 @@ const ( ` ) +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) { @@ -43,7 +50,7 @@ func printOutputCreate(buf *bytes.Buffer, cmd *cobra.Command) { output = strings.ReplaceAll(output, "%s", "") output = strings.ReplaceAll(output, "\n", "\n ") w := new(tabwriter.Writer) - w.Init(buf, 6, 4, 3, ' ', 0) + w.Init(buf, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, 0) buf.WriteString(outputHeader) buf.WriteString(` From f868d30458deedea92b2e2789409f1273041b4f4 Mon Sep 17 00:00:00 2001 From: Sarah Simpers Date: Wed, 15 Mar 2023 09:28:56 -0400 Subject: [PATCH 6/6] Fixes alignment, copy review changes --- output.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/output.go b/output.go index ec40674..2ab8cbe 100644 --- a/output.go +++ b/output.go @@ -48,18 +48,19 @@ func printOutputCreate(buf *bytes.Buffer, cmd *cobra.Command) { output = strings.ReplaceAll(output, "{{.", "<") output = strings.ReplaceAll(output, "}}", ">") output = strings.ReplaceAll(output, "%s", "") + 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 prints a message similar to the following and replaces the values in brackets with your values: +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) +`) + fmt.Fprintln(w, " "+output) w.Flush() buf.WriteString("\n") }