Skip to content

Commit

Permalink
fix: add --force flag to talosctl gen config
Browse files Browse the repository at this point in the history
Only overwrite existing files if explicitly demanded.

Closes #6847

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Feb 20, 2023
1 parent 660b887 commit 3d55bd8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/talosctl/cmd/mgmt/gen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var genConfigCmdFlags struct {
withDocs bool
withClusterDiscovery bool
withKubeSpan bool
force bool
withSecrets string
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/cli/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
1 change: 1 addition & 0 deletions website/content/v1.4/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3d55bd8

Please sign in to comment.