Skip to content

Commit 913bd65

Browse files
committed
Fix nonadmin backup describe --details
Signed-off-by: Joseph <jvaikath@redhat.com>
1 parent 30297bd commit 913bd65

3 files changed

Lines changed: 28 additions & 76 deletions

File tree

cmd/non-admin/backup/describe.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
7272
}
7373

7474
// Print in Velero-style format
75-
printNonAdminBackupDetails(cmd, &nab, kbClient, backupName, userNamespace, effectiveTimeout)
75+
printNonAdminBackupDetails(cmd, &nab, kbClient, backupName, userNamespace, effectiveTimeout, details)
7676

7777
// Add detailed output if --details flag is set
7878
if details {
@@ -98,7 +98,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
9898
}
9999

100100
// printNonAdminBackupDetails prints backup details in Velero admin describe format
101-
func printNonAdminBackupDetails(cmd *cobra.Command, nab *nacv1alpha1.NonAdminBackup, kbClient kbclient.Client, backupName string, userNamespace string, timeout time.Duration) {
101+
func printNonAdminBackupDetails(cmd *cobra.Command, nab *nacv1alpha1.NonAdminBackup, kbClient kbclient.Client, backupName string, userNamespace string, timeout time.Duration, showDetails bool) {
102102
out := cmd.OutOrStdout()
103103

104104
// Get Velero backup reference if available
@@ -322,22 +322,24 @@ func printNonAdminBackupDetails(cmd *cobra.Command, nab *nacv1alpha1.NonAdminBac
322322

323323
fmt.Fprintf(out, "\n")
324324

325-
// Fetch and display Resource List
326-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
327-
defer cancel()
325+
// Fetch and display Resource List (only if showDetails is true)
326+
if showDetails {
327+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
328+
defer cancel()
328329

329-
resourceList, err := shared.ProcessDownloadRequest(ctx, kbClient, shared.DownloadRequestOptions{
330-
BackupName: backupName,
331-
DataType: "BackupResourceList",
332-
Namespace: userNamespace,
333-
HTTPTimeout: timeout,
334-
})
330+
resourceList, err := shared.ProcessDownloadRequest(ctx, kbClient, shared.DownloadRequestOptions{
331+
BackupName: backupName,
332+
DataType: "BackupResourceList",
333+
Namespace: userNamespace,
334+
HTTPTimeout: timeout,
335+
})
335336

336-
if err == nil && resourceList != "" {
337-
if formattedList := formatResourceList(resourceList); formattedList != "" {
338-
fmt.Fprintf(out, "Resource List:\n")
339-
fmt.Fprintf(out, "%s\n", formattedList)
340-
fmt.Fprintf(out, "\n")
337+
if err == nil && resourceList != "" {
338+
if formattedList := formatResourceList(resourceList); formattedList != "" {
339+
fmt.Fprintf(out, "Resource List:\n")
340+
fmt.Fprintf(out, "%s\n", formattedList)
341+
fmt.Fprintf(out, "\n")
342+
}
341343
}
342344
}
343345

cmd/non-admin/verbs/builder.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ func (vb *NonAdminVerbBuilder) runEFunc(verb string) func(cmd *cobra.Command, ar
8888
}
8989

9090
// Get the main command for the resource (e.g., "backup" command)
91+
// This creates a FRESH command instance each time, which is important
92+
// to avoid flag state persistence across invocations
9193
resourceCmd := handler.GetCommandFunc(vb.factory)
9294
if resourceCmd == nil {
9395
return fmt.Errorf("%s command not found for resource type %s", verb, resourceType)
9496
}
9597

96-
// Get the specific subcommand for the verb (e.g., "backup get" command)
98+
// Get the specific subcommand for the verb (e.g., "backup describe" command)
9799
subCmd := handler.GetSubCommandFunc(resourceCmd)
98100
if subCmd == nil {
99101
return fmt.Errorf("%s %s command not found", resourceType, verb)
@@ -102,11 +104,11 @@ func (vb *NonAdminVerbBuilder) runEFunc(verb string) func(cmd *cobra.Command, ar
102104
// Add flags to remaining args so they get passed to the delegated command
103105
remainingArgs = vb.addFlagsToArgs(cmd, remainingArgs)
104106

105-
// Create a new command instance to avoid argument inheritance
106-
newSubCmd := vb.createCommandInstance(subCmd)
107-
newSubCmd.SetArgs(remainingArgs)
107+
// Use the original subcommand directly (not a copy) and set its args
108+
// This ensures the RunE closure uses the correct flag variables
109+
subCmd.SetArgs(remainingArgs)
108110

109-
return newSubCmd.Execute()
111+
return subCmd.Execute()
110112
}
111113
}
112114

@@ -138,28 +140,10 @@ func (vb *NonAdminVerbBuilder) addFlagsToArgs(cmd *cobra.Command, remainingArgs
138140
return remainingArgs
139141
}
140142

141-
// createCommandInstance creates a new cobra.Command instance from an existing one to avoid argument/flag inheritance issues.
142-
func (vb *NonAdminVerbBuilder) createCommandInstance(originalCmd *cobra.Command) *cobra.Command {
143-
newCmd := &cobra.Command{
144-
Use: originalCmd.Use,
145-
Short: originalCmd.Short,
146-
Long: originalCmd.Long,
147-
Run: originalCmd.Run,
148-
RunE: originalCmd.RunE,
149-
}
150-
151-
// Copy flags from the original command
152-
originalCmd.Flags().VisitAll(func(flag *pflag.Flag) {
153-
newCmd.Flags().AddFlag(flag)
154-
})
155-
// Also copy persistent flags
156-
originalCmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
157-
newCmd.PersistentFlags().AddFlag(flag)
158-
})
159-
return newCmd
160-
}
161-
162143
// addFlagsFromResources adds flags from all registered resources to the verb command
144+
// Note: We add flag references (not copies) here for recognition at the verb level.
145+
// The actual command execution will use fresh command instances created by GetCommandFunc,
146+
// so flag state won't persist across invocations.
163147
func (vb *NonAdminVerbBuilder) addFlagsFromResources(verbCmd *cobra.Command, verb string) {
164148
addedFlags := make(map[string]bool)
165149

cmd/non-admin/verbs/builder_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -386,40 +386,6 @@ func TestNonAdminVerbBuilder_MultipleResourceTypes(t *testing.T) {
386386
}
387387
}
388388

389-
// TestNonAdminVerbBuilder_CreateCommandInstance tests command instance creation
390-
func TestNonAdminVerbBuilder_CreateCommandInstance(t *testing.T) {
391-
builder := NewNonAdminVerbBuilder(nil)
392-
393-
originalCmd := &cobra.Command{
394-
Use: "create NAME",
395-
Short: "Create a resource",
396-
Long: "Create a non-admin resource",
397-
}
398-
originalCmd.Flags().String("test-flag", "default", "test flag")
399-
originalCmd.PersistentFlags().String("persistent-flag", "default", "persistent flag")
400-
401-
newCmd := builder.createCommandInstance(originalCmd)
402-
403-
// Verify basic fields are copied
404-
if newCmd.Use != originalCmd.Use {
405-
t.Errorf("Expected Use to be %s, got %s", originalCmd.Use, newCmd.Use)
406-
}
407-
if newCmd.Short != originalCmd.Short {
408-
t.Errorf("Expected Short to be %s, got %s", originalCmd.Short, newCmd.Short)
409-
}
410-
if newCmd.Long != originalCmd.Long {
411-
t.Errorf("Expected Long to be %s, got %s", originalCmd.Long, newCmd.Long)
412-
}
413-
414-
// Verify flags are copied
415-
if newCmd.Flags().Lookup("test-flag") == nil {
416-
t.Error("Expected test-flag to be copied to new command")
417-
}
418-
if newCmd.PersistentFlags().Lookup("persistent-flag") == nil {
419-
t.Error("Expected persistent-flag to be copied to new command")
420-
}
421-
}
422-
423389
// TestNonAdminVerbBuilder_NilHandler tests handling of nil handlers
424390
func TestNonAdminVerbBuilder_NilHandler(t *testing.T) {
425391
builder := NewNonAdminVerbBuilder(nil)

0 commit comments

Comments
 (0)