Skip to content

Commit 217af01

Browse files
committed
Refinements in tests and defining filePath
1 parent 76113cd commit 217af01

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package export
22

33
import (
44
"fmt"
5+
"path/filepath"
6+
"strings"
57

68
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
79
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
@@ -17,12 +19,14 @@ const (
1719
profileNameArg = "PROFILE_NAME"
1820

1921
filePathFlag = "file-path"
22+
23+
configFileExtension = "json"
2024
)
2125

2226
type inputModel struct {
2327
*globalflags.GlobalFlagModel
2428
ProfileName string
25-
ExportPath string
29+
FilePath string
2630
}
2731

2832
func NewCmd(p *print.Printer) *cobra.Command {
@@ -32,7 +36,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
3236
Long: "Exports a CLI configuration profile.",
3337
Example: examples.Build(
3438
examples.NewExample(
35-
`Export a profile with name "PROFILE_NAME" to the current path`,
39+
`Export a profile with name "PROFILE_NAME" to a file in your current directory`,
3640
"$ stackit config profile export PROFILE_NAME",
3741
),
3842
examples.NewExample(
@@ -47,12 +51,12 @@ func NewCmd(p *print.Printer) *cobra.Command {
4751
return err
4852
}
4953

50-
err = config.ExportProfile(p, model.ProfileName, model.ExportPath)
54+
err = config.ExportProfile(p, model.ProfileName, model.FilePath)
5155
if err != nil {
5256
return fmt.Errorf("could not export profile: %w", err)
5357
}
5458

55-
p.Info("Exported profile %q\n", model.ProfileName)
59+
p.Info("Exported profile %q to %q\n", model.ProfileName, model.FilePath)
5660

5761
return nil
5862
},
@@ -62,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6266
}
6367

6468
func configureFlags(cmd *cobra.Command) {
65-
cmd.Flags().String(filePathFlag, "", "Path where the config should be saved. E.g. '--file-path ~/config.json', '--file-path ~/'")
69+
cmd.Flags().StringP(filePathFlag, "f", "", "If set, writes the payload to the given. If unset, writes the payload to you current directory with the name of the profile. E.g. '--file-path ~/my-config.json', '--file-path ~/'")
6670
}
6771

6872
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
@@ -72,7 +76,13 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
7276
model := inputModel{
7377
GlobalFlagModel: globalFlags,
7478
ProfileName: profileName,
75-
ExportPath: flags.FlagToStringValue(p, cmd, filePathFlag),
79+
FilePath: flags.FlagToStringValue(p, cmd, filePathFlag),
80+
}
81+
82+
// If filePath contains does not contain a file name, then add a default name
83+
if !strings.HasSuffix(model.FilePath, fmt.Sprintf(".%s", configFileExtension)) {
84+
exportFileName := fmt.Sprintf("%s.%s", model.ProfileName, configFileExtension)
85+
model.FilePath = filepath.Join(model.FilePath, exportFileName)
7686
}
7787

7888
if p.IsVerbosityDebug() {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package export
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
@@ -11,7 +12,7 @@ import (
1112

1213
const (
1314
testProfileArg = "default"
14-
testExportPath = "/tmp/stackit-profiles"
15+
testExportPath = "/tmp/stackit-profiles/" + testProfileArg + ".json"
1516
)
1617

1718
func fixtureArgValues(mods ...func(args []string)) []string {
@@ -40,7 +41,7 @@ func fixtureInputModel(mods ...func(inputModel *inputModel)) *inputModel {
4041
Verbosity: globalflags.VerbosityDefault,
4142
},
4243
ProfileName: testProfileArg,
43-
ExportPath: testExportPath,
44+
FilePath: testExportPath,
4445
}
4546
for _, mod := range mods {
4647
mod(model)
@@ -81,7 +82,7 @@ func TestParseInput(t *testing.T) {
8182
flagValues: map[string]string{},
8283
isValid: true,
8384
expectedModel: fixtureInputModel(func(inputModel *inputModel) {
84-
inputModel.ExportPath = ""
85+
inputModel.FilePath = fmt.Sprintf("%s.json", testProfileArg)
8586
}),
8687
},
8788
}

internal/pkg/config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ var configFolderPath string
105105
var profileFilePath string
106106

107107
func InitConfig() {
108-
defaultConfigFolderPath = getInitialConfigDir()
108+
initConfig(getInitialConfigDir())
109+
}
110+
111+
func initConfig(configPath string) {
112+
defaultConfigFolderPath = configPath
109113
profileFilePath = getInitialProfileFilePath() // Profile file path is in the default config folder
110114

111115
configProfile, err := GetProfile()

internal/pkg/config/profiles.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ func ImportProfile(p *print.Printer, profileName, config string, setAsActive boo
400400
}
401401

402402
// ExportProfile exports a profile configuration
403-
// Is exports the profile to the filePath.
404-
func ExportProfile(p *print.Printer, profile, filePath string) error {
403+
// Is exports the profile to the exportPath. The exportPath must contain in the suffix `.json` as file extension
404+
func ExportProfile(p *print.Printer, profile, exportPath string) error {
405405
err := ValidateProfile(profile)
406406
if err != nil {
407407
return fmt.Errorf("validate profile: %w", err)
@@ -418,24 +418,22 @@ func ExportProfile(p *print.Printer, profile, filePath string) error {
418418
profilePath := GetProfileFolderPath(profile)
419419
configFile := getConfigFilePath(profilePath)
420420

421-
exportFileName := fmt.Sprintf("%s.%s", profile, configFileExtension)
422-
exportFilePath := filePath
423-
if !strings.HasSuffix(exportFilePath, fmt.Sprintf(".%s", configFileExtension)) {
424-
exportFilePath = filepath.Join(filePath, exportFileName)
421+
if !strings.HasSuffix(exportPath, fmt.Sprintf(".%s", configFileExtension)) {
422+
return fmt.Errorf("export file name must end with '.%s'", configFileExtension)
425423
}
426424

427-
_, err = os.Stat(exportFilePath)
425+
_, err = os.Stat(exportPath)
428426
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)
427+
return &errors.FileAlreadyExistsError{Filename: exportPath}
430428
}
431429

432-
err = fileutils.CopyFile(configFile, exportFilePath)
430+
err = fileutils.CopyFile(configFile, exportPath)
433431
if err != nil {
434-
return fmt.Errorf("export config file to %q: %w", exportFilePath, err)
432+
return fmt.Errorf("export config file to %q: %w", exportPath, err)
435433
}
436434

437435
if p != nil {
438-
p.Debug(print.DebugLevel, "exported profile %q to %q", profile, exportFilePath)
436+
p.Debug(print.DebugLevel, "exported profile %q to %q", profile, exportPath)
439437
}
440438

441439
return nil

internal/pkg/config/profiles_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,20 @@ func TestExportProfile(t *testing.T) {
199199
}(testDir)
200200
})
201201

202+
defaultConfigFolderPath = filepath.Join(testDir, "config")
203+
err = os.Mkdir(defaultConfigFolderPath, 0o750)
204+
if err != nil {
205+
t.Fatal(err)
206+
}
207+
202208
// Create prerequisite profile
203209
p := print.NewPrinter()
204210
profileName := "export-profile-test"
205211
err = CreateProfile(p, profileName, true, true)
206212
if err != nil {
207213
t.Fatalf("could not create prerequisite profile, %v", err)
208214
}
209-
InitConfig()
215+
initConfig(defaultConfigFolderPath)
210216
err = Write()
211217
if err != nil {
212218
t.Fatalf("could not write profile, %v", err)
@@ -229,24 +235,18 @@ func TestExportProfile(t *testing.T) {
229235
{
230236
description: "valid profile",
231237
profile: profileName,
232-
filePath: testDir,
238+
filePath: filepath.Join(testDir, fmt.Sprintf("custom-name.%s", configFileExtension)),
233239
isValid: true,
234240
},
235241
{
236242
description: "invalid profile",
237243
profile: "invalid-my-profile",
238244
isValid: false,
239245
},
240-
{
241-
description: "custom file name",
242-
profile: profileName,
243-
filePath: filepath.Join(testDir, fmt.Sprintf("custom-name.%s", configFileExtension)),
244-
isValid: true,
245-
},
246246
{
247247
description: "not existing path",
248248
profile: profileName,
249-
filePath: filepath.Join(testDir, "invalid", "path"),
249+
filePath: filepath.Join(testDir, "invalid", "path", fmt.Sprintf("custom-name.%s", configFileExtension)),
250250
isValid: false,
251251
},
252252
}

0 commit comments

Comments
 (0)