@@ -3,12 +3,14 @@ package config
33import (
44 "encoding/json"
55 "fmt"
6- "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
7- "github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
8- "github.com/stackitcloud/stackit-cli/internal/pkg/print"
96 "os"
107 "path/filepath"
118 "regexp"
9+ "strings"
10+
11+ "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
12+ "github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
13+ "github.com/stackitcloud/stackit-cli/internal/pkg/print"
1214)
1315
1416const ProfileEnvVar = "STACKIT_CLI_PROFILE"
@@ -415,32 +417,25 @@ func ExportProfile(p *print.Printer, profile, filePath string) error {
415417
416418 profilePath := GetProfileFolderPath (profile )
417419 configFile := getConfigFilePath (profilePath )
418- exportFilePath := filePath
419420
420- // Handle if exportFilePath is a directory
421- stats , err := os .Stat (exportFilePath )
422- if err == nil {
423- // If exportFilePath exists, and it is not a directory, then return an error
424- if ! stats .IsDir () {
425- return & errors.FileAlreadyExistsError {Filename : exportFilePath }
426- }
427-
428- exportFileName := fmt .Sprintf ("%s.%s" , profile , configFileExtension )
421+ exportFileName := fmt .Sprintf ("%s.%s" , profile , configFileExtension )
422+ exportFilePath := filePath
423+ if ! strings .HasSuffix (exportFilePath , fmt .Sprintf (".%s" , configFileExtension )) {
429424 exportFilePath = filepath .Join (filePath , exportFileName )
425+ }
430426
431- _ , err = os .Stat (exportFilePath )
432- if err == nil {
433- return & errors.FileAlreadyExistsError {Filename : exportFilePath }
434- }
427+ _ , err = os .Stat (exportFilePath )
428+ if err == nil {
429+ return fmt .Errorf ("file %q already exists in the export path. Delete the existing file or define a different export path" , exportFileName )
435430 }
436431
437432 err = fileutils .CopyFile (configFile , exportFilePath )
438433 if err != nil {
439- return fmt .Errorf ("export config file: %w" , err )
434+ return fmt .Errorf ("export config file to %q : %w" , exportFilePath , err )
440435 }
441436
442437 if p != nil {
443- p .Debug (print .DebugLevel , "exported profile %q" , profile )
438+ p .Debug (print .DebugLevel , "exported profile %q to %q " , profile , exportFilePath )
444439 }
445440
446441 return nil
0 commit comments