From 4af93c3b6296b46da77a331da4a9d3fea712fef9 Mon Sep 17 00:00:00 2001 From: Alexandre Mclean Date: Tue, 22 Nov 2022 22:16:01 -0500 Subject: [PATCH] fix: parse correctly upgrade cmd force flag It was using value of a variable boud to another flag. Signed-off-by: Alexandre Mclean Signed-off-by: Andrey Smirnov (cherry picked from commit a9e9d71b242ff13927589fa741eee0325e0cfe5d) --- cmd/talosctl/cmd/talos/kubeconfig.go | 22 +++++++++++----------- cmd/talosctl/cmd/talos/upgrade.go | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/talosctl/cmd/talos/kubeconfig.go b/cmd/talosctl/cmd/talos/kubeconfig.go index 86f65eaea1..37ab5e2d03 100644 --- a/cmd/talosctl/cmd/talos/kubeconfig.go +++ b/cmd/talosctl/cmd/talos/kubeconfig.go @@ -22,11 +22,11 @@ import ( "github.com/talos-systems/talos/pkg/machinery/client" ) -var ( +var kubeconfigFlags struct { force bool forceContextName string merge bool -) +} // kubeconfigCmd represents the kubeconfig command. var kubeconfigCmd = &cobra.Command{ @@ -48,7 +48,7 @@ Otherwise kubeconfig will be written to PWD or [local-path] if specified.`, // no path given, use defaults var err error - if merge { + if kubeconfigFlags.merge { localPath, err = kubeconfig.SinglePath() if err != nil { return err @@ -81,12 +81,12 @@ Otherwise kubeconfig will be written to PWD or [local-path] if specified.`, } _, err = os.Stat(localPath) - if err == nil && !(force || merge) { + if err == nil && !(kubeconfigFlags.force || kubeconfigFlags.merge) { return fmt.Errorf("kubeconfig file already exists, use --force to overwrite: %q", localPath) } else if err != nil { if os.IsNotExist(err) { // merge doesn't make sense if target path doesn't exist - merge = false + kubeconfigFlags.merge = false } else { return fmt.Errorf("error checking path %q: %w", localPath, err) } @@ -115,7 +115,7 @@ Otherwise kubeconfig will be written to PWD or [local-path] if specified.`, return err } - if merge { + if kubeconfigFlags.merge { return extractAndMerge(data, localPath) } @@ -139,10 +139,10 @@ func extractAndMerge(data []byte, localPath string) error { err = merger.Merge(config, kubeconfig.MergeOptions{ ActivateContext: true, - ForceContextName: forceContextName, + ForceContextName: kubeconfigFlags.forceContextName, OutputWriter: os.Stdout, ConflictHandler: func(component kubeconfig.ConfigComponent, name string) (kubeconfig.ConflictDecision, error) { - if force { + if kubeconfigFlags.force { return kubeconfig.OverwriteDecision, nil } @@ -182,8 +182,8 @@ func askOverwriteOrRename(prompt string) (kubeconfig.ConflictDecision, error) { } func init() { - kubeconfigCmd.Flags().BoolVarP(&force, "force", "f", false, "Force overwrite of kubeconfig if already present, force overwrite on kubeconfig merge") - kubeconfigCmd.Flags().StringVar(&forceContextName, "force-context-name", "", "Force context name for kubeconfig merge") - kubeconfigCmd.Flags().BoolVarP(&merge, "merge", "m", true, "Merge with existing kubeconfig") + kubeconfigCmd.Flags().BoolVarP(&kubeconfigFlags.force, "force", "f", false, "Force overwrite of kubeconfig if already present, force overwrite on kubeconfig merge") + kubeconfigCmd.Flags().StringVar(&kubeconfigFlags.forceContextName, "force-context-name", "", "Force context name for kubeconfig merge") + kubeconfigCmd.Flags().BoolVarP(&kubeconfigFlags.merge, "merge", "m", true, "Merge with existing kubeconfig") addCommand(kubeconfigCmd) } diff --git a/cmd/talosctl/cmd/talos/upgrade.go b/cmd/talosctl/cmd/talos/upgrade.go index f804c7bd99..04930e9087 100644 --- a/cmd/talosctl/cmd/talos/upgrade.go +++ b/cmd/talosctl/cmd/talos/upgrade.go @@ -76,7 +76,7 @@ func runUpgradeNoWait() error { upgradeCmdFlags.upgradeImage, upgradeCmdFlags.preserve, upgradeCmdFlags.stage, - force, + upgradeCmdFlags.force, grpc.Peer(&remotePeer), ) if err != nil { @@ -118,7 +118,7 @@ func upgradeGetActorID(ctx context.Context, c *client.Client) (string, error) { upgradeCmdFlags.upgradeImage, upgradeCmdFlags.preserve, upgradeCmdFlags.stage, - force, + upgradeCmdFlags.force, ) if err != nil { return "", err