Skip to content

Commit e2702ce

Browse files
committed
feat(config): add ignore-existing for profile create
1 parent 2352383 commit e2702ce

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

docs/stackit_config_profile_create.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The profile name can be provided via the STACKIT_CLI_PROFILE environment variabl
99
The environment variable takes precedence over the argument.
1010
If you do not want to set the profile as active, use the --no-set flag.
1111
If you want to create the new profile with the initial default configurations, use the --empty flag.
12+
If you want to create the new profile and ignore the error for an already existing profile, use the --ignore-existing flag.
1213

1314
```
1415
stackit config profile create PROFILE [flags]
@@ -27,9 +28,10 @@ stackit config profile create PROFILE [flags]
2728
### Options
2829

2930
```
30-
--empty Create the profile with the initial default configurations
31-
-h, --help Help for "stackit config profile create"
32-
--no-set Do not set the profile as the active profile
31+
--empty Create the profile with the initial default configurations
32+
-h, --help Help for "stackit config profile create"
33+
--ignore-existing Suppress the error it the profile exists already
34+
--no-set Do not set the profile as the active profile
3335
```
3436

3537
### Options inherited from parent commands

internal/cmd/config/profile/create/create.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import (
1919
const (
2020
profileArg = "PROFILE"
2121

22-
noSetFlag = "no-set"
23-
fromEmptyProfile = "empty"
22+
noSetFlag = "no-set"
23+
ignoreExistingFlag = "ignore-existing"
24+
fromEmptyProfile = "empty"
2425
)
2526

2627
type inputModel struct {
2728
*globalflags.GlobalFlagModel
2829
NoSet bool
30+
IgnoreExisting bool
2931
FromEmptyProfile bool
3032
Profile string
3133
}
@@ -34,12 +36,13 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
3436
cmd := &cobra.Command{
3537
Use: fmt.Sprintf("create %s", profileArg),
3638
Short: "Creates a CLI configuration profile",
37-
Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s",
39+
Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s",
3840
"Creates a CLI configuration profile based on the currently active profile and sets it as active.",
3941
`The profile name can be provided via the STACKIT_CLI_PROFILE environment variable or as an argument in this command.`,
4042
"The environment variable takes precedence over the argument.",
4143
"If you do not want to set the profile as active, use the --no-set flag.",
4244
"If you want to create the new profile with the initial default configurations, use the --empty flag.",
45+
"If you want to create the new profile and ignore the error for an already existing profile, use the --ignore-existing flag.",
4346
),
4447
Args: args.SingleArg(profileArg, nil),
4548
Example: examples.Build(
@@ -56,7 +59,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5659
return err
5760
}
5861

59-
err = config.CreateProfile(params.Printer, model.Profile, !model.NoSet, model.FromEmptyProfile)
62+
err = config.CreateProfile(params.Printer, model.Profile, !model.NoSet, model.IgnoreExisting, model.FromEmptyProfile)
6063
if err != nil {
6164
return fmt.Errorf("create profile: %w", err)
6265
}
@@ -85,6 +88,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8588

8689
func configureFlags(cmd *cobra.Command) {
8790
cmd.Flags().Bool(noSetFlag, false, "Do not set the profile as the active profile")
91+
cmd.Flags().Bool(ignoreExistingFlag, false, "Suppress the error it the profile exists already")
8892
cmd.Flags().Bool(fromEmptyProfile, false, "Create the profile with the initial default configurations")
8993
}
9094

@@ -103,6 +107,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
103107
Profile: profile,
104108
FromEmptyProfile: flags.FlagToBoolValue(p, cmd, fromEmptyProfile),
105109
NoSet: flags.FlagToBoolValue(p, cmd, noSetFlag),
110+
IgnoreExisting: flags.FlagToBoolValue(p, cmd, ignoreExistingFlag),
106111
}
107112

108113
p.DebugInputModel(model)

internal/pkg/config/profiles.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func GetProfileFromEnv() (string, bool) {
8080
// If emptyProfile is true, it creates an empty profile. Otherwise, copies the config from the current profile to the new profile.
8181
// If setProfile is true, it sets the new profile as the active profile.
8282
// If the profile already exists, it returns an error.
83-
func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bool) error {
83+
func CreateProfile(p *print.Printer, profile string, setProfile, ignoreExising, emptyProfile bool) error {
8484
err := ValidateProfile(profile)
8585
if err != nil {
8686
return fmt.Errorf("validate profile: %w", err)
@@ -98,6 +98,9 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo
9898
// Error if the profile already exists
9999
_, err = os.Stat(configFolderPath)
100100
if err == nil {
101+
if ignoreExising {
102+
return nil
103+
}
101104
return fmt.Errorf("profile %q already exists", profile)
102105
}
103106

internal/pkg/config/profiles_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func TestExportProfile(t *testing.T) {
210210
// Create prerequisite profile
211211
p := print.NewPrinter()
212212
profileName := "export-profile-test"
213-
err = CreateProfile(p, profileName, true, false)
213+
err = CreateProfile(p, profileName, true, false, false)
214214
if err != nil {
215215
t.Fatalf("could not create prerequisite profile, %v", err)
216216
}

0 commit comments

Comments
 (0)