From cddb13892124f665b67a699c18a9ad3e09e50a1e Mon Sep 17 00:00:00 2001 From: Carlo Goetz Date: Wed, 25 Feb 2026 17:21:57 +0100 Subject: [PATCH] fix(config) export profile: write default cfg to exportPath when cfg does not exist https://github.com/stackitcloud/stackit-cli/pull/214 removed writing the default config to file on start. So no config file exists for the default profile if user did never set a value. This lead to an error when exporting the non-existent config of the default profile. This fix writes the default config to exportPath in this case. --- internal/pkg/config/profiles.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/pkg/config/profiles.go b/internal/pkg/config/profiles.go index 17d2fd352..cf96d222e 100644 --- a/internal/pkg/config/profiles.go +++ b/internal/pkg/config/profiles.go @@ -7,6 +7,7 @@ import ( "path/filepath" "regexp" + "github.com/spf13/viper" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/fileutils" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -434,7 +435,13 @@ func ExportProfile(p *print.Printer, profile, exportPath string) error { return &errors.FileAlreadyExistsError{Filename: exportPath} } - err = fileutils.CopyFile(configFile, exportPath) + _, err = os.Stat(configFile) + if os.IsNotExist(err) { + // viper.SafeWriteConfigAs would not overwrite the target, so we use WriteConfigAs for the same behavior as CopyFile + err = viper.WriteConfigAs(exportPath) + } else { + err = fileutils.CopyFile(configFile, exportPath) + } if err != nil { return fmt.Errorf("export config file to %q: %w", exportPath, err) }