Skip to content

Commit 467c778

Browse files
tjmoore4benjaminjb
andcommitted
Update CLI help template function
Adds a function for formatting CLI help output to remove markdown formatting for more than one header before printing to terminal. Co-authored-by: Ben Blattberg <[email protected]>
1 parent ff97909 commit 467c778

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

internal/cmd/pgo.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"io"
1919
"os"
2020
"path/filepath"
21-
"strings"
21+
"regexp"
2222

2323
"github.com/spf13/cobra"
2424
"k8s.io/cli-runtime/pkg/genericclioptions"
@@ -82,9 +82,9 @@ func NewPGOCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
8282
SilenceErrors: true,
8383
}
8484

85-
cobra.AddTemplateFunc("replaceAll", strings.ReplaceAll)
85+
cobra.AddTemplateFunc("formatHeader", formatHeader)
8686

87-
root.SetHelpTemplate(`{{with .Long}}{{ replaceAll . "#### RBAC Requirements" "RBAC Requirements:" | trimTrailingWhitespaces }}
87+
root.SetHelpTemplate(`{{with .Long}}{{ formatHeader . | trimTrailingWhitespaces }}
8888
8989
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`)
9090

@@ -107,3 +107,9 @@ func NewPGOCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
107107

108108
return root
109109
}
110+
111+
// formatHeader removes markdown header syntax for CLI help output
112+
func formatHeader(s string) string {
113+
re := regexp.MustCompile(`#### (.*)\n`)
114+
return re.ReplaceAllString(s, "$1:\n")
115+
}

internal/cmd/pgo_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2021 - 2023 Crunchy Data Solutions, 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 cmd
16+
17+
import (
18+
"testing"
19+
20+
"gotest.tools/v3/assert"
21+
)
22+
23+
func TestFormatHeader(t *testing.T) {
24+
testsCases := []struct {
25+
desc string
26+
input string
27+
output string
28+
}{
29+
{"Expected", "#### Some Header\n", "Some Header:\n"},
30+
{"One valid, one not", "#### Some Header\n#### Another Header", "Some Header:\n#### Another Header"},
31+
{"Two expected", "#### Some Header\n#### Another Header\n", "Some Header:\nAnother Header:\n"},
32+
{"No newline", "#### Some Header", "#### Some Header"},
33+
{"3 hashes", "### Some Header\n", "### Some Header\n"},
34+
{"leading space", " #### Some Header\n", " Some Header:\n"},
35+
}
36+
37+
for _, tc := range testsCases {
38+
t.Run(tc.desc, func(t *testing.T) {
39+
assert.Equal(t, formatHeader(tc.input), tc.output)
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)