|
| 1 | +package delete |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/auth" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/config" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + profileArg = "PROFILE" |
| 19 | +) |
| 20 | + |
| 21 | +type inputModel struct { |
| 22 | + *globalflags.GlobalFlagModel |
| 23 | + Profile string |
| 24 | +} |
| 25 | + |
| 26 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 27 | + cmd := &cobra.Command{ |
| 28 | + Use: fmt.Sprintf("delete %s", profileArg), |
| 29 | + Short: "Delete a CLI configuration profile", |
| 30 | + Long: fmt.Sprintf("%s\n%s", |
| 31 | + "Delete a CLI configuration profile.", |
| 32 | + "If the deleted profile is the active profile, the default profile will be set to active.", |
| 33 | + ), |
| 34 | + Args: args.SingleArg(profileArg, nil), |
| 35 | + Example: examples.Build( |
| 36 | + examples.NewExample( |
| 37 | + `Delete the configuration profile "my-profile"`, |
| 38 | + "$ stackit config profile delete my-profile"), |
| 39 | + ), |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + model, err := parseInput(p, cmd, args) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + profileExists, err := config.ProfileExists(model.Profile) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("check if profile exists: %w", err) |
| 49 | + } |
| 50 | + if !profileExists { |
| 51 | + return &errors.DeleteInexistentProfile{Profile: model.Profile} |
| 52 | + } |
| 53 | + |
| 54 | + if !model.AssumeYes { |
| 55 | + prompt := fmt.Sprintf("Are you sure you want to delete profile %q? (This cannot be undone)", model.Profile) |
| 56 | + err = p.PromptForConfirmation(prompt) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + err = config.DeleteProfile(p, model.Profile) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("delete profile: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + err = auth.DeleteProfileFromKeyring(model.Profile) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("delete profile from keyring: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + p.Info("Successfully deleted profile %q\n", model.Profile) |
| 73 | + |
| 74 | + return nil |
| 75 | + }, |
| 76 | + } |
| 77 | + return cmd |
| 78 | +} |
| 79 | + |
| 80 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 81 | + profile := inputArgs[0] |
| 82 | + |
| 83 | + err := config.ValidateProfile(profile) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + globalFlags := globalflags.Parse(p, cmd) |
| 89 | + |
| 90 | + model := inputModel{ |
| 91 | + GlobalFlagModel: globalFlags, |
| 92 | + Profile: profile, |
| 93 | + } |
| 94 | + |
| 95 | + if p.IsVerbosityDebug() { |
| 96 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 97 | + if err != nil { |
| 98 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 99 | + } else { |
| 100 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return &model, nil |
| 105 | +} |
0 commit comments