From bd29af5803f24b4bb9eee6d4b3600223b8c4d1a3 Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Fri, 29 May 2026 15:47:49 -0400 Subject: [PATCH 01/10] Hides --namespace flag from nonadmin commands --- cmd/root.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 429827a4..9070b819 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -451,7 +451,12 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { c.AddCommand(nabsl.NewNABSLRequestCommand(f)) // Custom subcommands - use NonAdmin factory - c.AddCommand(nonadmin.NewNonAdminCommand(f)) + nonadminCmd := nonadmin.NewNonAdminCommand(f) + c.AddCommand(nonadminCmd) + + // Hide --namespace flag from nonadmin commands + // NonAdmin operations are namespace-scoped to the user's current context for security + hideNamespaceFlagFromCommand(nonadminCmd) // Must-gather command - diagnostic tool c.AddCommand(mustgather.NewMustGatherCommand(f)) @@ -501,3 +506,18 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { c.PersistentFlags().AddGoFlagSet(flag.CommandLine) return c } + +// hideNamespaceFlagFromCommand recursively hides the --namespace flag from a command and all its subcommands +func hideNamespaceFlagFromCommand(cmd *cobra.Command) { + // Hide from both local and inherited (persistent) flags + if flag := cmd.LocalFlags().Lookup("namespace"); flag != nil { + flag.Hidden = true + } + if flag := cmd.InheritedFlags().Lookup("namespace"); flag != nil { + flag.Hidden = true + } + // Recursively hide from all subcommands + for _, subCmd := range cmd.Commands() { + hideNamespaceFlagFromCommand(subCmd) + } +} From 11cb0cefba3943076074609c34377799281b16ed Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Mon, 1 Jun 2026 09:07:05 -0400 Subject: [PATCH 02/10] Hiding namespace flag when used in nonadmin help --- cmd/root.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 9070b819..1292dcb5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -509,14 +509,19 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { // hideNamespaceFlagFromCommand recursively hides the --namespace flag from a command and all its subcommands func hideNamespaceFlagFromCommand(cmd *cobra.Command) { - // Hide from both local and inherited (persistent) flags - if flag := cmd.LocalFlags().Lookup("namespace"); flag != nil { - flag.Hidden = true - } - if flag := cmd.InheritedFlags().Lookup("namespace"); flag != nil { - flag.Hidden = true - } - // Recursively hide from all subcommands + // For each command, we need to hide the inherited namespace flag in its help output + // We do this by overriding the HelpFunc to filter out the namespace flag + originalHelpFunc := cmd.HelpFunc() + cmd.SetHelpFunc(func(c *cobra.Command, args []string) { + // Temporarily hide the namespace flag for this help output + if flag := c.InheritedFlags().Lookup("namespace"); flag != nil { + flag.Hidden = true + defer func() { flag.Hidden = false }() + } + originalHelpFunc(c, args) + }) + + // Recursively apply to all subcommands for _, subCmd := range cmd.Commands() { hideNamespaceFlagFromCommand(subCmd) } From 3cddc5d874bc917a6efae11109535855b6c2ee1a Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Mon, 1 Jun 2026 10:57:18 -0400 Subject: [PATCH 03/10] Fixed --namespace flag in nonadmin command --- cmd/root.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 1292dcb5..2246e440 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -515,8 +515,9 @@ func hideNamespaceFlagFromCommand(cmd *cobra.Command) { cmd.SetHelpFunc(func(c *cobra.Command, args []string) { // Temporarily hide the namespace flag for this help output if flag := c.InheritedFlags().Lookup("namespace"); flag != nil { + originalHidden := flag.Hidden flag.Hidden = true - defer func() { flag.Hidden = false }() + defer func() { flag.Hidden = originalHidden }() } originalHelpFunc(c, args) }) From 3d30709e774cdbfa5a9994a550b0ae1e05ea7572 Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Mon, 1 Jun 2026 12:25:52 -0400 Subject: [PATCH 04/10] Hiding --namespace flag in nonadmin command --- cmd/root.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 2246e440..f3d3d65f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -458,6 +458,14 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { // NonAdmin operations are namespace-scoped to the user's current context for security hideNamespaceFlagFromCommand(nonadminCmd) + // Reject --namespace flag if used with nonadmin commands + nonadminCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + if cmd.Flags().Changed("namespace") { + return fmt.Errorf("-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context") + } + return nil + } + // Must-gather command - diagnostic tool c.AddCommand(mustgather.NewMustGatherCommand(f)) From 6c7c6e33bce72560cf5445581a25529c1c16165b Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Mon, 1 Jun 2026 13:22:36 -0400 Subject: [PATCH 05/10] Hiding --namespace flag in nonadmin command --- cmd/root.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f3d3d65f..693a10d1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -517,9 +517,11 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { // hideNamespaceFlagFromCommand recursively hides the --namespace flag from a command and all its subcommands func hideNamespaceFlagFromCommand(cmd *cobra.Command) { - // For each command, we need to hide the inherited namespace flag in its help output - // We do this by overriding the HelpFunc to filter out the namespace flag - originalHelpFunc := cmd.HelpFunc() + // Get the default Cobra help function before we start modifying anything + // This avoids infinite recursion if HelpFunc gets called multiple times + defaultHelpFunc := cmd.HelpFunc() + + // Set custom help function that hides namespace flag cmd.SetHelpFunc(func(c *cobra.Command, args []string) { // Temporarily hide the namespace flag for this help output if flag := c.InheritedFlags().Lookup("namespace"); flag != nil { @@ -527,7 +529,8 @@ func hideNamespaceFlagFromCommand(cmd *cobra.Command) { flag.Hidden = true defer func() { flag.Hidden = originalHidden }() } - originalHelpFunc(c, args) + // Call the default help function, not cmd.HelpFunc() which would cause recursion + defaultHelpFunc(c, args) }) // Recursively apply to all subcommands From 93dfcc40e3dff5b2904ecfb3e1c008276dbd9747 Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Mon, 1 Jun 2026 14:47:41 -0400 Subject: [PATCH 06/10] Hiding --namespace flag in nonadmin command --- cmd/root.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 693a10d1..b95ca4f2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -517,21 +517,9 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { // hideNamespaceFlagFromCommand recursively hides the --namespace flag from a command and all its subcommands func hideNamespaceFlagFromCommand(cmd *cobra.Command) { - // Get the default Cobra help function before we start modifying anything - // This avoids infinite recursion if HelpFunc gets called multiple times - defaultHelpFunc := cmd.HelpFunc() - - // Set custom help function that hides namespace flag - cmd.SetHelpFunc(func(c *cobra.Command, args []string) { - // Temporarily hide the namespace flag for this help output - if flag := c.InheritedFlags().Lookup("namespace"); flag != nil { - originalHidden := flag.Hidden - flag.Hidden = true - defer func() { flag.Hidden = originalHidden }() - } - // Call the default help function, not cmd.HelpFunc() which would cause recursion - defaultHelpFunc(c, args) - }) + // Mark the command so we can identify it needs the namespace flag hidden + // We'll handle this in a PersistentPreRun hook to avoid modifying the shared flag object + cmd.Annotations = map[string]string{"hide-namespace-flag": "true"} // Recursively apply to all subcommands for _, subCmd := range cmd.Commands() { From 2b7242a9b91775f9cf277a033a30f77d976efacf Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Mon, 15 Jun 2026 11:59:10 -0400 Subject: [PATCH 07/10] Move nonadmin namespace logic into cmd/non-admin --- cmd/non-admin/namespace.go | 53 ++++++++++++++++++++++++++++++++++++++ cmd/non-admin/nonadmin.go | 2 ++ cmd/root.go | 27 +------------------ 3 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 cmd/non-admin/namespace.go diff --git a/cmd/non-admin/namespace.go b/cmd/non-admin/namespace.go new file mode 100644 index 00000000..0c823dc7 --- /dev/null +++ b/cmd/non-admin/namespace.go @@ -0,0 +1,53 @@ +/* +Copyright 2025 The OADP CLI Contributors. + +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 nonadmin + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// ConfigureNamespaceBehavior hides the inherited --namespace flag from nonadmin +// help output and rejects runtime use of -n/--namespace. Nonadmin operations are +// scoped to the user's current context namespace for security. +func ConfigureNamespaceBehavior(cmd *cobra.Command) { + hideNamespaceFlagFromCommand(cmd) + + cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error { + if c.Flags().Changed("namespace") { + return fmt.Errorf("-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context") + } + return nil + } +} + +func hideNamespaceFlagFromCommand(cmd *cobra.Command) { + originalHelpFunc := cmd.HelpFunc() + cmd.SetHelpFunc(func(c *cobra.Command, args []string) { + if flag := c.InheritedFlags().Lookup("namespace"); flag != nil { + originalHidden := flag.Hidden + flag.Hidden = true + defer func() { flag.Hidden = originalHidden }() + } + originalHelpFunc(c, args) + }) + + for _, subCmd := range cmd.Commands() { + hideNamespaceFlagFromCommand(subCmd) + } +} diff --git a/cmd/non-admin/nonadmin.go b/cmd/non-admin/nonadmin.go index dd6c08e5..ec15c979 100644 --- a/cmd/non-admin/nonadmin.go +++ b/cmd/non-admin/nonadmin.go @@ -50,5 +50,7 @@ func NewNonAdminCommand(f client.Factory) *cobra.Command { c.AddCommand(verbs.NewDescribeCommand(f)) c.AddCommand(verbs.NewLogsCommand(f)) + ConfigureNamespaceBehavior(c) + return c } diff --git a/cmd/root.go b/cmd/root.go index b95ca4f2..429827a4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -451,20 +451,7 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { c.AddCommand(nabsl.NewNABSLRequestCommand(f)) // Custom subcommands - use NonAdmin factory - nonadminCmd := nonadmin.NewNonAdminCommand(f) - c.AddCommand(nonadminCmd) - - // Hide --namespace flag from nonadmin commands - // NonAdmin operations are namespace-scoped to the user's current context for security - hideNamespaceFlagFromCommand(nonadminCmd) - - // Reject --namespace flag if used with nonadmin commands - nonadminCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { - if cmd.Flags().Changed("namespace") { - return fmt.Errorf("-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context") - } - return nil - } + c.AddCommand(nonadmin.NewNonAdminCommand(f)) // Must-gather command - diagnostic tool c.AddCommand(mustgather.NewMustGatherCommand(f)) @@ -514,15 +501,3 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { c.PersistentFlags().AddGoFlagSet(flag.CommandLine) return c } - -// hideNamespaceFlagFromCommand recursively hides the --namespace flag from a command and all its subcommands -func hideNamespaceFlagFromCommand(cmd *cobra.Command) { - // Mark the command so we can identify it needs the namespace flag hidden - // We'll handle this in a PersistentPreRun hook to avoid modifying the shared flag object - cmd.Annotations = map[string]string{"hide-namespace-flag": "true"} - - // Recursively apply to all subcommands - for _, subCmd := range cmd.Commands() { - hideNamespaceFlagFromCommand(subCmd) - } -} From ce8241717840dc8bc362c0186b10084e7e432d88 Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Mon, 15 Jun 2026 13:49:08 -0400 Subject: [PATCH 08/10] Added Changes from copilot and added to the tests --- cmd/non-admin/namespace.go | 36 +++++++++++++++++++---- cmd/non-admin/namespace_test.go | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 cmd/non-admin/namespace_test.go diff --git a/cmd/non-admin/namespace.go b/cmd/non-admin/namespace.go index 0c823dc7..0757aa22 100644 --- a/cmd/non-admin/namespace.go +++ b/cmd/non-admin/namespace.go @@ -28,10 +28,19 @@ import ( func ConfigureNamespaceBehavior(cmd *cobra.Command) { hideNamespaceFlagFromCommand(cmd) + existingPersistentPreRunE := cmd.PersistentPreRunE + existingPersistentPreRun := cmd.PersistentPreRun + cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error { if c.Flags().Changed("namespace") { return fmt.Errorf("-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context") } + if existingPersistentPreRunE != nil { + return existingPersistentPreRunE(c, args) + } + if existingPersistentPreRun != nil { + existingPersistentPreRun(c, args) + } return nil } } @@ -39,15 +48,30 @@ func ConfigureNamespaceBehavior(cmd *cobra.Command) { func hideNamespaceFlagFromCommand(cmd *cobra.Command) { originalHelpFunc := cmd.HelpFunc() cmd.SetHelpFunc(func(c *cobra.Command, args []string) { - if flag := c.InheritedFlags().Lookup("namespace"); flag != nil { - originalHidden := flag.Hidden - flag.Hidden = true - defer func() { flag.Hidden = originalHidden }() - } - originalHelpFunc(c, args) + withHiddenNamespaceFlag(c, func() { + originalHelpFunc(c, args) + }) + }) + + originalUsageFunc := cmd.UsageFunc() + cmd.SetUsageFunc(func(c *cobra.Command) error { + var err error + withHiddenNamespaceFlag(c, func() { + err = originalUsageFunc(c) + }) + return err }) for _, subCmd := range cmd.Commands() { hideNamespaceFlagFromCommand(subCmd) } } + +func withHiddenNamespaceFlag(cmd *cobra.Command, fn func()) { + if flag := cmd.InheritedFlags().Lookup("namespace"); flag != nil { + originalHidden := flag.Hidden + flag.Hidden = true + defer func() { flag.Hidden = originalHidden }() + } + fn() +} diff --git a/cmd/non-admin/namespace_test.go b/cmd/non-admin/namespace_test.go new file mode 100644 index 00000000..06a90965 --- /dev/null +++ b/cmd/non-admin/namespace_test.go @@ -0,0 +1,51 @@ +/* +Copyright 2025 The OADP CLI Contributors. + +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 nonadmin + +import ( + "context" + "os/exec" + "strings" + "testing" + + "github.com/migtools/oadp-cli/internal/testutil" +) + +func TestNonAdminNamespaceFlagBehavior(t *testing.T) { + binaryPath := testutil.BuildCLIBinary(t) + + t.Run("help hides namespace flag", func(t *testing.T) { + output, _ := testutil.RunCommand(t, binaryPath, "nonadmin", "backup", "get", "--help") + if strings.Contains(output, "--namespace") { + t.Errorf("Expected help output not to contain --namespace\nFull output:\n%s", output) + } + }) + + t.Run("rejects namespace flag at runtime", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), testutil.TestTimeout) + defer cancel() + + output, err := exec.CommandContext(ctx, binaryPath, "nonadmin", "backup", "get", "-n", "other-namespace").CombinedOutput() + if err == nil { + t.Fatalf("Expected error when -n/--namespace is provided, got output:\n%s", string(output)) + } + expected := "-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context" + if !strings.Contains(string(output), expected) { + t.Errorf("Expected output to contain %q, got:\n%s", expected, string(output)) + } + }) +} From b220cc50f05400f1eab2098a7d439bdd6902be51 Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Tue, 16 Jun 2026 09:21:22 -0400 Subject: [PATCH 09/10] Updated If statement to look for -n and --namespace string --- cmd/non-admin/namespace_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/non-admin/namespace_test.go b/cmd/non-admin/namespace_test.go index 06a90965..59410afd 100644 --- a/cmd/non-admin/namespace_test.go +++ b/cmd/non-admin/namespace_test.go @@ -30,8 +30,8 @@ func TestNonAdminNamespaceFlagBehavior(t *testing.T) { t.Run("help hides namespace flag", func(t *testing.T) { output, _ := testutil.RunCommand(t, binaryPath, "nonadmin", "backup", "get", "--help") - if strings.Contains(output, "--namespace") { - t.Errorf("Expected help output not to contain --namespace\nFull output:\n%s", output) + if strings.Contains(output, "-n, --namespace") || strings.Contains(output, "--namespace string") { + t.Errorf("Expected help output not to contain the namespace flag\nFull output:\n%s", output) } }) From d030e336b99524a8f855546f632ccdca4b5426ca Mon Sep 17 00:00:00 2001 From: Nicholas Yancey Date: Thu, 18 Jun 2026 11:37:19 -0400 Subject: [PATCH 10/10] Chain root persistent setup into nonadmin namespace hook --- cmd/non-admin/namespace.go | 8 +++++++- cmd/non-admin/namespace_test.go | 18 +++++++++++++++++ cmd/non-admin/nonadmin.go | 2 -- cmd/root.go | 36 ++++++++++++++++++++++----------- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/cmd/non-admin/namespace.go b/cmd/non-admin/namespace.go index 0757aa22..0c7a2efc 100644 --- a/cmd/non-admin/namespace.go +++ b/cmd/non-admin/namespace.go @@ -25,7 +25,10 @@ import ( // ConfigureNamespaceBehavior hides the inherited --namespace flag from nonadmin // help output and rejects runtime use of -n/--namespace. Nonadmin operations are // scoped to the user's current context namespace for security. -func ConfigureNamespaceBehavior(cmd *cobra.Command) { +func ConfigureNamespaceBehavior( + cmd *cobra.Command, + runGlobalSetup func(cmd *cobra.Command, args []string), +) { hideNamespaceFlagFromCommand(cmd) existingPersistentPreRunE := cmd.PersistentPreRunE @@ -35,6 +38,9 @@ func ConfigureNamespaceBehavior(cmd *cobra.Command) { if c.Flags().Changed("namespace") { return fmt.Errorf("-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context") } + if runGlobalSetup != nil { + runGlobalSetup(c, args) + } if existingPersistentPreRunE != nil { return existingPersistentPreRunE(c, args) } diff --git a/cmd/non-admin/namespace_test.go b/cmd/non-admin/namespace_test.go index 59410afd..1e2dcff5 100644 --- a/cmd/non-admin/namespace_test.go +++ b/cmd/non-admin/namespace_test.go @@ -23,6 +23,7 @@ import ( "testing" "github.com/migtools/oadp-cli/internal/testutil" + "github.com/spf13/cobra" ) func TestNonAdminNamespaceFlagBehavior(t *testing.T) { @@ -35,6 +36,23 @@ func TestNonAdminNamespaceFlagBehavior(t *testing.T) { } }) + t.Run("calls global persistent setup", func(t *testing.T) { + var globalSetupCalled bool + globalSetup := func(cmd *cobra.Command, args []string) { + globalSetupCalled = true + } + + cmd := &cobra.Command{Use: "nonadmin"} + ConfigureNamespaceBehavior(cmd, globalSetup) + + if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil { + t.Fatalf("PersistentPreRunE returned error: %v", err) + } + if !globalSetupCalled { + t.Error("Expected global persistent setup to run for nonadmin command") + } + }) + t.Run("rejects namespace flag at runtime", func(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), testutil.TestTimeout) defer cancel() diff --git a/cmd/non-admin/nonadmin.go b/cmd/non-admin/nonadmin.go index ec15c979..dd6c08e5 100644 --- a/cmd/non-admin/nonadmin.go +++ b/cmd/non-admin/nonadmin.go @@ -50,7 +50,5 @@ func NewNonAdminCommand(f client.Factory) *cobra.Command { c.AddCommand(verbs.NewDescribeCommand(f)) c.AddCommand(verbs.NewLogsCommand(f)) - ConfigureNamespaceBehavior(c) - return c } diff --git a/cmd/root.go b/cmd/root.go index 429827a4..74e43f18 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -375,6 +375,24 @@ func isNonadminEnabled(config clientcmd.VeleroConfig) bool { } } +func configureGlobalCommandBehavior( + config clientcmd.VeleroConfig, + cmdFeatures veleroflag.StringArray, + cmdColorized veleroflag.OptionalBool, +) func(cmd *cobra.Command, args []string) { + return func(cmd *cobra.Command, args []string) { + features.Enable(config.Features()...) + features.Enable(cmdFeatures...) + + switch { + case cmdColorized.Value != nil: + color.NoColor = !*cmdColorized.Value + default: + color.NoColor = !config.Colorized() + } + } +} + // NewVeleroRootCommand returns a root command with all Velero CLI subcommands attached. func NewVeleroRootCommand(baseName string) *cobra.Command { @@ -400,6 +418,8 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { var cmdFeatures veleroflag.StringArray var cmdColorzied veleroflag.OptionalBool + globalPreRun := configureGlobalCommandBehavior(config, cmdFeatures, cmdColorzied) + c := &cobra.Command{ Use: baseName, Short: "OADP CLI commands", @@ -407,17 +427,7 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { // Default action when no subcommand is provided fmt.Println("Welcome to the OADP CLI! Use --help to see available commands.") }, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - features.Enable(config.Features()...) - features.Enable(cmdFeatures...) - - switch { - case cmdColorzied.Value != nil: - color.NoColor = !*cmdColorzied.Value - default: - color.NoColor = !config.Colorized() - } - }, + PersistentPreRun: globalPreRun, } // Create Velero client factory for regular Velero commands @@ -451,7 +461,9 @@ func NewVeleroRootCommand(baseName string) *cobra.Command { c.AddCommand(nabsl.NewNABSLRequestCommand(f)) // Custom subcommands - use NonAdmin factory - c.AddCommand(nonadmin.NewNonAdminCommand(f)) + nonadminCmd := nonadmin.NewNonAdminCommand(f) + nonadmin.ConfigureNamespaceBehavior(nonadminCmd, globalPreRun) + c.AddCommand(nonadminCmd) // Must-gather command - diagnostic tool c.AddCommand(mustgather.NewMustGatherCommand(f))