Skip to content

Commit 6baf1ae

Browse files
committed
Fix: Linter and tests
1 parent deb660c commit 6baf1ae

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

internal/cmd/config/profile/export/export.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package export
22

33
import (
44
"fmt"
5-
"github.com/spf13/cobra"
5+
66
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
77
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
88
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
99
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1010
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1111
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
13+
"github.com/spf13/cobra"
1214
)
1315

1416
const (

internal/cmd/config/profile/export/export_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package export
22

33
import (
4-
"github.com/google/go-cmp/cmp"
4+
"testing"
5+
56
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
7-
"testing"
8+
9+
"github.com/google/go-cmp/cmp"
810
)
911

1012
const (
@@ -77,7 +79,10 @@ func TestParseInput(t *testing.T) {
7779
description: "no flags",
7880
argsValues: fixtureArgValues(),
7981
flagValues: map[string]string{},
80-
isValid: false,
82+
isValid: true,
83+
expectedModel: fixtureInputModel(func(inputModel *inputModel) {
84+
inputModel.ExportPath = ""
85+
}),
8186
},
8287
}
8388

internal/pkg/config/profiles.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package config
33
import (
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

1416
const 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

internal/pkg/config/profiles_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ func TestExportProfile(t *testing.T) {
190190
if err != nil {
191191
t.Fatal(err)
192192
}
193-
defer os.RemoveAll(testDir)
193+
defer func(path string) {
194+
_ = os.RemoveAll(path)
195+
}(testDir)
194196

195197
tests := []struct {
196198
description string

0 commit comments

Comments
 (0)