Skip to content

Commit 83a10c2

Browse files
committed
Changed export --file-path behaviour.
- When --file-path is set, it does not add a default name and the user needs to provide the whole export path - When --file-path is empty, it adds a default name
1 parent 217af01 commit 83a10c2

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

docs/stackit_config_profile_export.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ stackit config profile export PROFILE_NAME [flags]
1313
### Examples
1414

1515
```
16-
Export a profile with name "PROFILE_NAME" to the current path
16+
Export a profile with name "PROFILE_NAME" to a file in your current directory
1717
$ stackit config profile export PROFILE_NAME
1818
1919
Export a profile with name "PROFILE_NAME"" to a specific file path FILE_PATH
@@ -23,7 +23,7 @@ stackit config profile export PROFILE_NAME [flags]
2323
### Options
2424

2525
```
26-
--file-path string Path where the config should be saved. E.g. '--file-path ~/config.json', '--file-path ~/'
26+
-f, --file-path string If set, writes the config to the given. If unset, writes the config to you current directory with the name of the profile. E.g. '--file-path ~/my-config.json', '--file-path ~/'
2727
-h, --help Help for "stackit config profile export"
2828
```
2929

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package export
33
import (
44
"fmt"
55
"path/filepath"
6-
"strings"
76

87
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
98
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
@@ -66,7 +65,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6665
}
6766

6867
func configureFlags(cmd *cobra.Command) {
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 ~/'")
68+
cmd.Flags().StringP(filePathFlag, "f", "", "If set, writes the config to the given. If unset, writes the config to you current directory with the name of the profile. E.g. '--file-path ~/my-config.json', '--file-path ~/'")
7069
}
7170

7271
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
@@ -80,7 +79,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
8079
}
8180

8281
// If filePath contains does not contain a file name, then add a default name
83-
if !strings.HasSuffix(model.FilePath, fmt.Sprintf(".%s", configFileExtension)) {
82+
if model.FilePath == "" {
8483
exportFileName := fmt.Sprintf("%s.%s", model.ProfileName, configFileExtension)
8584
model.FilePath = filepath.Join(model.FilePath, exportFileName)
8685
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ func TestParseInput(t *testing.T) {
8585
inputModel.FilePath = fmt.Sprintf("%s.json", testProfileArg)
8686
}),
8787
},
88+
{
89+
description: "custom file-path without file extension",
90+
argsValues: fixtureArgValues(),
91+
flagValues: fixtureFlagValues(
92+
func(flagValues map[string]string) {
93+
flagValues[filePathFlag] = "./my-exported-config"
94+
}),
95+
isValid: true,
96+
expectedModel: fixtureInputModel(func(inputModel *inputModel) {
97+
inputModel.FilePath = "./my-exported-config"
98+
}),
99+
},
88100
}
89101

90102
for _, tt := range tests {

internal/pkg/config/profiles.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"path/filepath"
88
"regexp"
9-
"strings"
109

1110
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1211
"github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
@@ -400,7 +399,7 @@ func ImportProfile(p *print.Printer, profileName, config string, setAsActive boo
400399
}
401400

402401
// ExportProfile exports a profile configuration
403-
// Is exports the profile to the exportPath. The exportPath must contain in the suffix `.json` as file extension
402+
// Is exports the profile to the exportPath. The exportPath must contain the filename.
404403
func ExportProfile(p *print.Printer, profile, exportPath string) error {
405404
err := ValidateProfile(profile)
406405
if err != nil {
@@ -418,10 +417,6 @@ func ExportProfile(p *print.Printer, profile, exportPath string) error {
418417
profilePath := GetProfileFolderPath(profile)
419418
configFile := getConfigFilePath(profilePath)
420419

421-
if !strings.HasSuffix(exportPath, fmt.Sprintf(".%s", configFileExtension)) {
422-
return fmt.Errorf("export file name must end with '.%s'", configFileExtension)
423-
}
424-
425420
_, err = os.Stat(exportPath)
426421
if err == nil {
427422
return &errors.FileAlreadyExistsError{Filename: exportPath}

internal/pkg/config/profiles_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ func TestExportProfile(t *testing.T) {
249249
filePath: filepath.Join(testDir, "invalid", "path", fmt.Sprintf("custom-name.%s", configFileExtension)),
250250
isValid: false,
251251
},
252+
{
253+
description: "export without file extension",
254+
profile: profileName,
255+
filePath: filepath.Join(testDir, "file-without-extension"),
256+
isValid: true,
257+
},
252258
}
253259

254260
for _, tt := range tests {

0 commit comments

Comments
 (0)