Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/stackit_config_profile_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions internal/cmd/config/profile/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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(
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 if the profile exists already")
cmd.Flags().Bool(fromEmptyProfile, false, "Create the profile with the initial default configurations")
}

Expand All @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions internal/pkg/config/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func GetProfileFromEnv() (string, bool) {
// CreateProfile creates a new profile.
// 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 {
// If the profile already exists and ignoreExisting is false, it returns an error.
func CreateProfile(p *print.Printer, profile string, setProfile, ignoreExising, emptyProfile bool) error {
Comment thread
dergeberl marked this conversation as resolved.
Outdated
err := ValidateProfile(profile)
if err != nil {
return fmt.Errorf("validate profile: %w", err)
Expand All @@ -98,6 +98,15 @@ 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 {
Comment thread
marceljk marked this conversation as resolved.
Outdated
if setProfile {
err = SetProfile(p, profile)
if err != nil {
return fmt.Errorf("set profile: %w", err)
}
}
return nil
Comment thread
dergeberl marked this conversation as resolved.
}
return fmt.Errorf("profile %q already exists", profile)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/config/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading