diff --git a/docs/stackit_config_profile_create.md b/docs/stackit_config_profile_create.md index 595c96fad..49141a2b0 100644 --- a/docs/stackit_config_profile_create.md +++ b/docs/stackit_config_profile_create.md @@ -9,6 +9,7 @@ The profile name can be provided via the STACKIT_CLI_PROFILE environment variabl The environment variable takes precedence over the argument. If you do not want to set the profile as active, use the --no-set flag. If you want to create the new profile with the initial default configurations, use the --empty flag. +If you want to create the new profile and ignore the error for an already existing profile, use the --ignore-existing flag. ``` stackit config profile create PROFILE [flags] @@ -27,9 +28,10 @@ stackit config profile create PROFILE [flags] ### Options ``` - --empty Create the profile with the initial default configurations - -h, --help Help for "stackit config profile create" - --no-set Do not set the profile as the active profile + --empty Create the profile with the initial default configurations + -h, --help Help for "stackit config profile create" + --ignore-existing Suppress the error it the profile exists already + --no-set Do not set the profile as the active profile ``` ### Options inherited from parent commands diff --git a/internal/cmd/config/profile/create/create.go b/internal/cmd/config/profile/create/create.go index 608fe85da..75a71f45f 100644 --- a/internal/cmd/config/profile/create/create.go +++ b/internal/cmd/config/profile/create/create.go @@ -19,13 +19,15 @@ import ( const ( profileArg = "PROFILE" - noSetFlag = "no-set" - fromEmptyProfile = "empty" + noSetFlag = "no-set" + ignoreExistingFlag = "ignore-existing" + fromEmptyProfile = "empty" ) type inputModel struct { *globalflags.GlobalFlagModel NoSet bool + IgnoreExisting bool FromEmptyProfile bool Profile string } @@ -34,12 +36,13 @@ func NewCmd(params *params.CmdParams) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("create %s", profileArg), Short: "Creates a CLI configuration profile", - Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s", + Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s", "Creates a CLI configuration profile based on the currently active profile and sets it as active.", `The profile name can be provided via the STACKIT_CLI_PROFILE environment variable or as an argument in this command.`, "The environment variable takes precedence over the argument.", "If you do not want to set the profile as active, use the --no-set flag.", "If you want to create the new profile with the initial default configurations, use the --empty flag.", + "If you want to create the new profile and ignore the error for an already existing profile, use the --ignore-existing flag.", ), Args: args.SingleArg(profileArg, nil), Example: examples.Build( @@ -56,7 +59,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command { return err } - err = config.CreateProfile(params.Printer, model.Profile, !model.NoSet, model.FromEmptyProfile) + err = config.CreateProfile(params.Printer, model.Profile, !model.NoSet, model.IgnoreExisting, model.FromEmptyProfile) if err != nil { return fmt.Errorf("create profile: %w", err) } @@ -85,6 +88,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command { func configureFlags(cmd *cobra.Command) { cmd.Flags().Bool(noSetFlag, false, "Do not set the profile as the active profile") + cmd.Flags().Bool(ignoreExistingFlag, false, "Suppress the error it the profile exists already") cmd.Flags().Bool(fromEmptyProfile, false, "Create the profile with the initial default configurations") } @@ -103,6 +107,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu Profile: profile, FromEmptyProfile: flags.FlagToBoolValue(p, cmd, fromEmptyProfile), NoSet: flags.FlagToBoolValue(p, cmd, noSetFlag), + IgnoreExisting: flags.FlagToBoolValue(p, cmd, ignoreExistingFlag), } p.DebugInputModel(model) diff --git a/internal/pkg/config/profiles.go b/internal/pkg/config/profiles.go index db47ce5d3..c64b4c1f0 100644 --- a/internal/pkg/config/profiles.go +++ b/internal/pkg/config/profiles.go @@ -80,7 +80,7 @@ func GetProfileFromEnv() (string, bool) { // If emptyProfile is true, it creates an empty profile. Otherwise, copies the config from the current profile to the new profile. // If setProfile is true, it sets the new profile as the active profile. // If the profile already exists, it returns an error. -func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bool) error { +func CreateProfile(p *print.Printer, profile string, setProfile, ignoreExising, emptyProfile bool) error { err := ValidateProfile(profile) if err != nil { return fmt.Errorf("validate profile: %w", err) @@ -98,6 +98,9 @@ func CreateProfile(p *print.Printer, profile string, setProfile, emptyProfile bo // Error if the profile already exists _, err = os.Stat(configFolderPath) if err == nil { + if ignoreExising { + return nil + } return fmt.Errorf("profile %q already exists", profile) } diff --git a/internal/pkg/config/profiles_test.go b/internal/pkg/config/profiles_test.go index 327c9dcf8..0039dc024 100644 --- a/internal/pkg/config/profiles_test.go +++ b/internal/pkg/config/profiles_test.go @@ -210,7 +210,7 @@ func TestExportProfile(t *testing.T) { // Create prerequisite profile p := print.NewPrinter() profileName := "export-profile-test" - err = CreateProfile(p, profileName, true, false) + err = CreateProfile(p, profileName, true, false, false) if err != nil { t.Fatalf("could not create prerequisite profile, %v", err) }