fix: add --force flag to talosctl gen config

Only overwrite existing files if explicitly demanded.

Closes #6847

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
This commit is contained in:
Dmitriy Matrenichev 2023-02-20 21:57:52 +03:00
parent 660b8874da
commit 3d55bd80f4
No known key found for this signature in database
GPG Key ID: D3363CF894E68892
3 changed files with 10 additions and 1 deletions

View File

@ -70,6 +70,7 @@ var genConfigCmdFlags struct {
withDocs bool
withClusterDiscovery bool
withKubeSpan bool
force bool
withSecrets string
}
@ -325,6 +326,12 @@ func writeToDestination(data []byte, destination string, permissions os.FileMode
return err
}
if !genConfigCmdFlags.force {
if _, err := os.Stat(destination); err == nil {
return fmt.Errorf("%s already exists, use --force to overwrite", destination)
}
}
parentDir := filepath.Dir(destination)
// Create dir path, ignoring "already exists" messages
@ -431,6 +438,7 @@ func init() {
genConfigCmd.Flags().BoolVarP(&genConfigCmdFlags.withDocs, "with-docs", "", true, "renders all machine configs adding the documentation for each field")
genConfigCmd.Flags().BoolVarP(&genConfigCmdFlags.withClusterDiscovery, "with-cluster-discovery", "", true, "enable cluster discovery feature")
genConfigCmd.Flags().BoolVarP(&genConfigCmdFlags.withKubeSpan, "with-kubespan", "", false, "enable KubeSpan feature")
genConfigCmd.Flags().BoolVarP(&genConfigCmdFlags.force, "force", "", false, "\"gen\" will overwrite existing files")
genConfigCmd.Flags().StringVar(&genConfigCmdFlags.withSecrets, "with-secrets", "", "use a secrets file generated using 'gen secrets'")
genConfigCmd.Flags().StringSliceVarP(&genConfigCmdFlags.outputTypes, "output-types", "t", allOutputTypes, fmt.Sprintf("types of outputs to be generated. valid types are: %q", allOutputTypes))

View File

@ -186,7 +186,7 @@ func (suite *GenSuite) testGenConfigPatch(patch []byte) {
tt := tt
suite.Run(tt.flag, func() {
suite.RunCLI([]string{"gen", "config", "foo", "https://192.168.0.1:6443", "--" + tt.flag, string(patch)},
suite.RunCLI([]string{"gen", "config", "--force", "foo", "https://192.168.0.1:6443", "--" + tt.flag, string(patch)},
base.StdoutEmpty(),
base.StderrNotEmpty(),
base.StderrShouldMatch(regexp.MustCompile("generating PKI and tokens")))

View File

@ -1313,6 +1313,7 @@ talosctl gen config <cluster name> <cluster endpoint> [flags]
--config-patch-control-plane stringArray patch generated machineconfigs (applied to 'init' and 'controlplane' types)
--config-patch-worker stringArray patch generated machineconfigs (applied to 'worker' type)
--dns-domain string the dns domain to use for cluster (default "cluster.local")
--force "gen" will overwrite existing files
-h, --help help for config
--install-disk string the disk to install to (default "/dev/sda")
--install-image string the image used to perform an installation (default "ghcr.io/siderolabs/installer:latest")