|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "context" |
5 | 6 | "fmt" |
6 | 7 | "os" |
7 | 8 | "sort" |
| 9 | + "strings" |
8 | 10 | "text/tabwriter" |
9 | 11 |
|
10 | 12 | "github.com/fatih/color" |
@@ -61,6 +63,23 @@ If the --preview flag is set, only deployment targets for preview environments w |
61 | 63 | listTargetCmd.Flags().BoolVar(&includePreviews, "preview", false, "List preview environments") |
62 | 64 | targetCmd.AddCommand(listTargetCmd) |
63 | 65 |
|
| 66 | + deleteTargetCmd := &cobra.Command{ |
| 67 | + Use: "delete", |
| 68 | + Short: "Deletes a deployment target", |
| 69 | + Long: `Deletes a deployment target in the project. Currently, this command only supports the deletion of preview environments.`, |
| 70 | + Run: func(cmd *cobra.Command, args []string) { |
| 71 | + err := checkLoginAndRunWithConfig(cmd, cliConf, args, deleteTarget) |
| 72 | + if err != nil { |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + deleteTargetCmd.Flags().StringVar(&targetName, "name", "", "Name of deployment target") |
| 79 | + deleteTargetCmd.Flags().BoolP("force", "f", false, "Force deletion without confirmation") |
| 80 | + deleteTargetCmd.MarkFlagRequired("name") // nolint:errcheck,gosec |
| 81 | + targetCmd.AddCommand(deleteTargetCmd) |
| 82 | + |
64 | 83 | return targetCmd |
65 | 84 | } |
66 | 85 |
|
@@ -126,6 +145,57 @@ func listTargets(ctx context.Context, user *types.GetAuthenticatedUserResponse, |
126 | 145 | return nil |
127 | 146 | } |
128 | 147 |
|
| 148 | +func deleteTarget(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error { |
| 149 | + name, err := cmd.Flags().GetString("name") |
| 150 | + if err != nil { |
| 151 | + return fmt.Errorf("error finding name flag: %w", err) |
| 152 | + } |
| 153 | + if name == "" { |
| 154 | + return fmt.Errorf("name flag must be set") |
| 155 | + } |
| 156 | + |
| 157 | + force, err := cmd.Flags().GetBool("force") |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("error finding force flag: %w", err) |
| 160 | + } |
| 161 | + |
| 162 | + var confirmed bool |
| 163 | + if !force { |
| 164 | + confirmed, err = confirmAction(fmt.Sprintf("Are you sure you want to delete target '%s'?", name)) |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("error confirming action: %w", err) |
| 167 | + } |
| 168 | + } |
| 169 | + if !confirmed && !force { |
| 170 | + color.New(color.FgYellow).Println("Deletion aborted") // nolint:errcheck,gosec |
| 171 | + return nil |
| 172 | + } |
| 173 | + |
| 174 | + err = client.DeleteDeploymentTarget(ctx, cliConf.Project, name) |
| 175 | + if err != nil { |
| 176 | + return fmt.Errorf("error deleting target: %w", err) |
| 177 | + } |
| 178 | + |
| 179 | + color.New(color.FgGreen).Printf("Deleted target '%s'\n", name) // nolint:errcheck,gosec |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func confirmAction(prompt string) (bool, error) { |
| 185 | + reader := bufio.NewReader(os.Stdin) |
| 186 | + fmt.Printf("%s [Y/n]: ", prompt) |
| 187 | + |
| 188 | + response, err := reader.ReadString('\n') |
| 189 | + if err != nil { |
| 190 | + return false, fmt.Errorf("error reading input: %w", err) |
| 191 | + } |
| 192 | + |
| 193 | + response = strings.TrimSpace(response) |
| 194 | + confirmed := strings.ToLower(response) == "y" || response == "" |
| 195 | + |
| 196 | + return confirmed, nil |
| 197 | +} |
| 198 | + |
129 | 199 | func checkmark(b bool) string { |
130 | 200 | if b { |
131 | 201 | return "✓" |
|
0 commit comments