Skip to content

Commit 33b6a70

Browse files
author
Ciprian Tibulca
authored
add support {{if}} control struct for output templates (#45)
1 parent 4fc7578 commit 33b6a70

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

output.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cobra2snooty
1717
import (
1818
"bytes"
1919
"fmt"
20+
"regexp"
2021
"strings"
2122
"text/tabwriter"
2223

@@ -26,6 +27,12 @@ import (
2627
const (
2728
outputHeader = `Output
2829
------
30+
`
31+
outputDescription = `
32+
If the command succeeds, the CLI returns output similar to the following sample. Values in brackets represent your values.
33+
34+
.. code-block::
35+
2936
`
3037
)
3138

@@ -36,14 +43,18 @@ const (
3643
tabwriterPadChar = ' '
3744
)
3845

46+
// regex for one or more characters except right curly bracket '}'.
47+
const charsExceptRightCurlyBracket = "[^}]+"
48+
3949
// This function can return the output for all commands when the output template is added as an annotation in the command file
4050

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

46-
output := strings.ReplaceAll(cmd.Annotations["output"], "{{range .Results}}", "")
56+
output := removeRange(cmd.Annotations["output"])
57+
output = replaceWithValueOrDefault(output)
4758
output = strings.ReplaceAll(output, "{{end}}", "")
4859
output = strings.ReplaceAll(output, "{{.", "<")
4960
output = strings.ReplaceAll(output, "}}", ">")
@@ -54,13 +65,24 @@ func printOutputCreate(buf *bytes.Buffer, cmd *cobra.Command) {
5465
w.Init(buf, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, 0)
5566

5667
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-
`)
68+
buf.WriteString(outputDescription)
6369
fmt.Fprintln(w, " "+output)
6470
w.Flush()
6571
buf.WriteString("\n")
6672
}
73+
74+
func removeRange(text string) string {
75+
// remove {{range}} control structure. Examples: {{range .}}, {{range .Results}}
76+
re := `{{range ` + charsExceptRightCurlyBracket + `}}`
77+
return regexp.MustCompile(re).ReplaceAllString(text, "")
78+
}
79+
80+
func replaceWithValueOrDefault(text string) string {
81+
// replaces {{if .field}}{{.field}}{{else}}defaultValue{{end}} with {{.field}}
82+
re := `{{if` + charsExceptRightCurlyBracket + `}}` +
83+
`({{` + charsExceptRightCurlyBracket + `}})` +
84+
`{{else}}` + charsExceptRightCurlyBracket + `{{end}}`
85+
86+
// $1 is the first group (surrounded by round brackets in the regex expression)
87+
return regexp.MustCompile(re).ReplaceAllString(text, "$1")
88+
}

output_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2023 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+
"testing"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
func TestPrintOutputCreate(t *testing.T) {
25+
t.Run("replaceWithValueOrDefault", func(t *testing.T) {
26+
outputTemplate := `ID NAME DATABASE COLLECTION TYPE
27+
{{range .}}{{.IndexID}} %s {{.Database}} {{.CollectionName}} {{if .Type }}{{.Type}}{{else}}defaultValue{{end}}
28+
{{end}}`
29+
30+
expected := outputHeader + outputDescription +
31+
` ID NAME DATABASE COLLECTION TYPE
32+
<IndexID> <Name> <Database> <CollectionName> <Type>
33+
34+
35+
`
36+
37+
cmd := &cobra.Command{
38+
Annotations: map[string]string{
39+
"output": outputTemplate,
40+
},
41+
}
42+
43+
buf := new(bytes.Buffer)
44+
printOutputCreate(buf, cmd)
45+
result := buf.String()
46+
47+
if result != expected {
48+
t.Errorf("expected:\n[%s]\ngot:\n[%s]\n", expected, result)
49+
}
50+
})
51+
}

0 commit comments

Comments
 (0)