@@ -8,36 +8,76 @@ import (
88)
99
1010var deleteCmd = & cobra.Command {
11- Use : "delete <env>..." ,
11+ Use : "delete [ <env>...] " ,
1212 Short : "Delete environments and start fresh" ,
1313 Long : `Delete one or more environments and their associated resources.
1414This permanently removes the environment's branch and container state.
15- Use this when starting over with a different approach.` ,
16- Args : cobra .MinimumNArgs (1 ),
15+ Use this when starting over with a different approach.
16+
17+ Use --all to delete all environments at once.` ,
18+ Args : func (cmd * cobra.Command , args []string ) error {
19+ all , _ := cmd .Flags ().GetBool ("all" )
20+ if all && len (args ) > 0 {
21+ return fmt .Errorf ("cannot specify environment names when using --all flag" )
22+ }
23+ if ! all && len (args ) == 0 {
24+ return fmt .Errorf ("must specify at least one environment name or use --all flag" )
25+ }
26+ return nil
27+ },
1728 ValidArgsFunction : suggestEnvironments ,
1829 Example : `# Delete a single environment
1930cu delete fancy-mallard
2031
2132# Delete multiple environments at once
22- cu delete env1 env2 env3` ,
33+ cu delete env1 env2 env3
34+
35+ # Delete all environments
36+ cu delete --all` ,
2337 RunE : func (cmd * cobra.Command , args []string ) error {
2438 ctx := cmd .Context ()
39+ all , _ := cmd .Flags ().GetBool ("all" )
40+
41+ repo , err := repository .Open (ctx , "." )
42+ if err != nil {
43+ return fmt .Errorf ("failed to open repository: %w" , err )
44+ }
2545
26- for _ , envID := range args {
27- repo , err := repository .Open (ctx , "." )
46+ var envIDs []string
47+ if all {
48+ // Get all environment IDs
49+ envs , err := repo .List (ctx )
2850 if err != nil {
29- return fmt .Errorf ("failed to open repository : %w" , err )
51+ return fmt .Errorf ("failed to list environments : %w" , err )
3052 }
31- if err := repo .Delete (ctx , envID ); err != nil {
32- return fmt .Errorf ("failed to delete environment: %w" , err )
53+ if len (envs ) == 0 {
54+ fmt .Println ("No environments found to delete." )
55+ return nil
56+ }
57+ for _ , env := range envs {
58+ envIDs = append (envIDs , env .ID )
3359 }
60+ fmt .Printf ("Deleting %d environment(s)...\n " , len (envIDs ))
61+ } else {
62+ envIDs = args
63+ }
3464
65+ for _ , envID := range envIDs {
66+ if err := repo .Delete (ctx , envID ); err != nil {
67+ return fmt .Errorf ("failed to delete environment '%s': %w" , envID , err )
68+ }
3569 fmt .Printf ("Environment '%s' deleted successfully.\n " , envID )
3670 }
71+
72+ if all {
73+ fmt .Printf ("Successfully deleted %d environment(s).\n " , len (envIDs ))
74+ }
75+
3776 return nil
3877 },
3978}
4079
4180func init () {
4281 rootCmd .AddCommand (deleteCmd )
82+ deleteCmd .Flags ().Bool ("all" , false , "Delete all environments" )
4383}
0 commit comments