Skip to content

Commit 3598c75

Browse files
Merge pull request #244 from openshift-cherrypick-robot/cherry-pick-243-to-oadp-1.3
[oadp-1.3] OADP-8508: Fix schedule create examples showing velero instead of oc oadp
2 parents eb9c47c + 958990a commit 3598c75

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

cmd/root.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ var veleroCommandPattern = regexp.MustCompile(`(?m)(?:^|[\s\x60])velero\s+(?:` +
166166
`(?:version|install|uninstall|plugin|snapshot-location|backup-location|restic|repo|client|completion|bug|debug|datamover)` +
167167
`)`)
168168

169+
// veleroCreateVerbFirstPattern matches upstream Velero's "velero create <resource>" example
170+
// text (verb-first), which some commands hardcode instead of the actual resource-first
171+
// invocation. For example, velero's schedule create command is invoked as
172+
// "velero schedule create NAME", but its upstream Example text says "velero create schedule
173+
// NAME". veleroCommandPattern above doesn't catch this ordering, so it's normalized to
174+
// resource-first form here before the general replacement runs.
175+
var veleroCreateVerbFirstPattern = regexp.MustCompile(`(^|[\s\x60\n])velero\s+create\s+(backup|restore|schedule)\b`)
176+
169177
// replaceVeleroCommandWithOADP performs context-aware replacement of "velero" with "oadp".
170178
// It only replaces "velero" when it's being used as a CLI command, not when referring to
171179
// the Velero project, server, or components.
@@ -174,6 +182,11 @@ func replaceVeleroCommandWithOADP(text string) string {
174182
// Use "oc" as the CLI prefix since OADP is primarily used on OpenShift
175183
cliPrefix := "oc"
176184

185+
// Normalize verb-first "velero create <resource>" examples to resource-first
186+
// "oc oadp <resource> create" before the general pattern runs, since the actual
187+
// command is always invoked resource-first.
188+
text = veleroCreateVerbFirstPattern.ReplaceAllString(text, "${1}"+cliPrefix+" oadp ${2} create")
189+
177190
// Replace "velero <command>" patterns with "oc/kubectl oadp <command>"
178191
result := veleroCommandPattern.ReplaceAllStringFunc(text, func(match string) string {
179192
// Preserve leading whitespace or backtick

cmd/root_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,34 @@ Use velero backup logs to check status`,
219219
}
220220
}
221221

222+
// TestReplaceVeleroWithOADP_ScheduleCreateVerbFirst tests that upstream Velero's
223+
// verb-first "velero create schedule NAME" example text (a known upstream quirk in
224+
// schedule/create.go) is normalized to the resource-first form that actually matches
225+
// how the command is invoked, consistent with "backup create" and "restore create".
226+
func TestReplaceVeleroWithOADP_ScheduleCreateVerbFirst(t *testing.T) {
227+
cmd := &cobra.Command{
228+
Use: "test",
229+
Example: ` # Create a backup every 6 hours.
230+
velero create schedule NAME --schedule="0 */6 * * *"
231+
232+
# Create a backup every 6 hours with the @every notation.
233+
velero create schedule NAME --schedule="@every 6h"`,
234+
}
235+
236+
replaceVeleroWithOADP(cmd)
237+
238+
if strings.Contains(cmd.Example, "velero create schedule") {
239+
t.Errorf("Expected verb-first 'velero create schedule' to be normalized, got: %s", cmd.Example)
240+
}
241+
if strings.Contains(cmd.Example, "velero") {
242+
t.Errorf("Expected 'velero' to be fully replaced, got: %s", cmd.Example)
243+
}
244+
wantCount := strings.Count(cmd.Example, "oc oadp schedule create NAME")
245+
if wantCount != 2 {
246+
t.Errorf("Expected 2 occurrences of 'oc oadp schedule create NAME', got %d\nActual output:\n%s", wantCount, cmd.Example)
247+
}
248+
}
249+
222250
// TestReplaceVeleroWithOADP_RunFunctionWrapper tests stdout capture and replacement
223251
func TestReplaceVeleroWithOADP_RunFunctionWrapper(t *testing.T) {
224252
outputCaptured := false

0 commit comments

Comments
 (0)