Skip to content

Commit

Permalink
p-env: deprecate force flag in favor of -y
Browse files Browse the repository at this point in the history
  • Loading branch information
petersutter committed May 31, 2023
1 parent b449961 commit e7ea5b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
25 changes: 20 additions & 5 deletions pkg/cmd/providerenv/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ type options struct {
// Template is the script template
Template env.Template
// Force generates the script even if there are access restrictions to be confirmed
// Deprecated: Use ConfirmAccessRestriction instead
Force bool
// ConfirmAccessRestriction, when set to true, implies the user's understanding of the access restrictions for the targeted shoot.
// When set to false and access restrictions are present, the command will terminate with an error.
ConfirmAccessRestriction bool
}

// Complete adapts from the command line args to the data required.
func (o *options) Complete(f util.Factory, cmd *cobra.Command, _ []string) error {
ctx := f.Context()

logger := klog.FromContext(ctx)

if cmd.Name() != "provider-env" {
o.Shell = cmd.Name()
}
Expand All @@ -71,6 +79,12 @@ func (o *options) Complete(f util.Factory, cmd *cobra.Command, _ []string) error
o.SessionDir = manager.SessionDir()
o.TargetFlags = f.TargetFlags()

if o.Force {
o.ConfirmAccessRestriction = true

logger.Info("The --force flag is deprecated and will be removed in a future gardenctl version. Please use the --confirm-access-restriction flag instead.")
}

return nil
}

Expand All @@ -93,7 +107,8 @@ func (o *options) Validate() error {

// AddFlags binds the command options to a given flagset.
func (o *options) AddFlags(flags *pflag.FlagSet) {
flags.BoolVarP(&o.Force, "force", "f", false, "Generate the script even if there are access restrictions to be confirmed")
flags.BoolVarP(&o.Force, "force", "f", false, "Deprecated. Use --confirm-access-restriction instead. Generate the script even if there are access restrictions to be confirmed.")
flags.BoolVarP(&o.ConfirmAccessRestriction, "confirm-access-restriction", "y", o.ConfirmAccessRestriction, "Confirm any access restrictions. Set this flag only if you are completely aware of the access restrictions.")
flags.BoolVarP(&o.Unset, "unset", "u", o.Unset, fmt.Sprintf("Generate the script to unset the cloud provider CLI environment variables and logout for %s", o.Shell))
}

Expand Down Expand Up @@ -186,12 +201,12 @@ func printProviderEnv(o *options, shoot *gardencorev1beta1.Shoot, secret *corev1
metadata := generateMetadata(o, cli)

if len(messages) > 0 {
if o.TargetFlags.ShootName() == "" || o.Force {
if o.TargetFlags.ShootName() == "" || o.ConfirmAccessRestriction {
metadata["notification"] = messages.String()
} else {
if o.Output != "" {
return errors.New(
"the cloud provider CLI configuration script can only be generated if you confirm the access despite the existing restrictions. Use the --force flag to confirm the access",
"the cloud provider CLI configuration script can only be generated if you confirm the access despite the existing restrictions. Use the --confirm-access-restriction flag to confirm the access",
)
}

Expand All @@ -200,8 +215,8 @@ func printProviderEnv(o *options, shoot *gardencorev1beta1.Shoot, secret *corev1
"format": messages.String() + "\n%s %s\n%s\n",
"arguments": []string{
"The cloud provider CLI configuration script can only be generated if you confirm the access despite the existing restrictions.",
"Use the --force flag to confirm the access.",
s.Prompt(runtime.GOOS) + s.EvalCommand(fmt.Sprintf("%s --force %s", o.CmdPath, o.Shell)),
"Use the --confirm-access-restriction flag to confirm the access.",
s.Prompt(runtime.GOOS) + s.EvalCommand(fmt.Sprintf("%s --confirm-access-restriction %s", o.CmdPath, o.Shell)),
},
})
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/providerenv/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,22 @@ var _ = Describe("Env Commands - Options", func() {
})

Describe("completing the command options", func() {
var root,
var (
root,
parent,
child *cobra.Command
ctx context.Context
)

BeforeEach(func() {
ctx = context.Background()
root = &cobra.Command{Use: "root"}
parent = &cobra.Command{Use: "parent", Aliases: []string{"alias"}}
child = &cobra.Command{Use: "child"}
parent.AddCommand(child)
root.AddCommand(parent)
factory.EXPECT().GardenHomeDir().Return(gardenHomeDir)
factory.EXPECT().Context().Return(ctx)
root.SetArgs([]string{"alias", "child"})
Expect(root.Execute()).To(Succeed())
baseTemplate = nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/providerenv/providerenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ var _ = Describe("Env Commands", func() {
factory.EXPECT().GardenHomeDir().Return(gardenHomeDir)

ctx = context.Background()
factory.EXPECT().Context().Return(ctx)
factory.EXPECT().Context().Return(ctx).AnyTimes()

secretBindingName = "secret-binding"
cloudProfileName = "cloud-profile"
Expand Down

0 comments on commit e7ea5b8

Please sign in to comment.