Skip to content

Commit cddb138

Browse files
committed
fix(config) export profile: write default cfg to exportPath when cfg does not exist
#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.
1 parent fad8e6d commit cddb138

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

internal/pkg/config/profiles.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"regexp"
99

10+
"github.com/spf13/viper"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
@@ -434,7 +435,13 @@ func ExportProfile(p *print.Printer, profile, exportPath string) error {
434435
return &errors.FileAlreadyExistsError{Filename: exportPath}
435436
}
436437

437-
err = fileutils.CopyFile(configFile, exportPath)
438+
_, err = os.Stat(configFile)
439+
if os.IsNotExist(err) {
440+
// viper.SafeWriteConfigAs would not overwrite the target, so we use WriteConfigAs for the same behavior as CopyFile
441+
err = viper.WriteConfigAs(exportPath)
442+
} else {
443+
err = fileutils.CopyFile(configFile, exportPath)
444+
}
438445
if err != nil {
439446
return fmt.Errorf("export config file to %q: %w", exportPath, err)
440447
}

0 commit comments

Comments
 (0)