44 "context"
55 "fmt"
66
7- "github.com/sirupsen/logrus"
87 "github.com/skevetter/devpod/cmd/completion"
98 "github.com/skevetter/devpod/cmd/flags"
109 client2 "github.com/skevetter/devpod/pkg/client"
@@ -31,27 +30,8 @@ func NewDeleteCmd(flags *flags.GlobalFlags) *cobra.Command {
3130 Short : "Deletes an existing workspace" ,
3231 Long : `Deletes an existing workspace. You can specify the workspace by its path or name.
3332If the workspace is not found, you can use the --ignore-not-found flag to treat it as a successful delete.` ,
34- RunE : func (_ * cobra.Command , args []string ) error {
35- _ , err := clientimplementation .DecodeOptionsFromEnv (
36- clientimplementation .DevPodFlagsDelete ,
37- & cmd .DeleteOptions ,
38- )
39- if err != nil {
40- return fmt .Errorf ("decode up options: %w" , err )
41- }
42-
43- ctx := context .Background ()
44- devPodConfig , err := config .LoadConfig (cmd .Context , cmd .Provider )
45- if err != nil {
46- return err
47- }
48-
49- err = clientimplementation .DecodePlatformOptionsFromEnv (& cmd .Platform )
50- if err != nil {
51- return fmt .Errorf ("decode platform options: %w" , err )
52- }
53-
54- return cmd .Run (ctx , devPodConfig , args )
33+ RunE : func (cobraCmd * cobra.Command , args []string ) error {
34+ return cmd .Run (cobraCmd , args )
5535 },
5636 ValidArgsFunction : func (
5737 rootCmd * cobra.Command , args []string , toComplete string ,
@@ -78,50 +58,91 @@ If the workspace is not found, you can use the --ignore-not-found flag to treat
7858}
7959
8060// Run runs the command logic.
81- func (cmd * DeleteCmd ) Run (ctx context.Context , devPodConfig * config.Config , args []string ) error {
82- if len (args ) == 0 {
83- workspaceName , err := workspace .Delete (
84- ctx ,
85- devPodConfig ,
86- args ,
87- cmd .IgnoreNotFound ,
88- cmd .Force ,
89- cmd .DeleteOptions ,
90- cmd .Owner ,
91- log .Default ,
92- )
93- if err != nil {
94- return err
95- }
96- log .WithFields (logrus.Fields {
97- "workspace" : workspaceName ,
98- })
99- log .Default .Donef ("deleted workspace" )
100- return nil
61+ func (cmd * DeleteCmd ) Run (cobraCmd * cobra.Command , args []string ) error {
62+ devPodConfig , err := cmd .loadConfig ()
63+ if err != nil {
64+ return err
10165 }
10266
67+ ctx := cobraCmd .Context ()
68+ if len (args ) <= 1 {
69+ return cmd .deleteSingle (ctx , devPodConfig , args )
70+ }
71+
72+ return cmd .deleteMultiple (ctx , devPodConfig , args )
73+ }
74+
75+ func (cmd * DeleteCmd ) loadConfig () (* config.Config , error ) {
76+ _ , err := clientimplementation .DecodeOptionsFromEnv (
77+ clientimplementation .DevPodFlagsDelete ,
78+ & cmd .DeleteOptions ,
79+ )
80+ if err != nil {
81+ return nil , fmt .Errorf ("decode delete options: %w" , err )
82+ }
83+
84+ if err := clientimplementation .DecodePlatformOptionsFromEnv (& cmd .Platform ); err != nil {
85+ return nil , fmt .Errorf ("decode platform options: %w" , err )
86+ }
87+
88+ return config .LoadConfig (cmd .Context , cmd .Provider )
89+ }
90+
91+ func (cmd * DeleteCmd ) deleteSingle (
92+ ctx context.Context ,
93+ devPodConfig * config.Config ,
94+ args []string ,
95+ ) error {
96+ name , err := cmd .deleteWorkspace (ctx , devPodConfig , args )
97+ if err != nil {
98+ return err
99+ }
100+
101+ log .Default .Donef ("deleted workspace %s" , name )
102+
103+ return nil
104+ }
105+
106+ func (cmd * DeleteCmd ) deleteMultiple (
107+ ctx context.Context ,
108+ devPodConfig * config.Config ,
109+ args []string ,
110+ ) error {
111+ var errs []error
103112 for _ , arg := range args {
104- workspaceName , err := workspace .Delete (
105- ctx ,
106- devPodConfig ,
107- []string {arg },
108- cmd .IgnoreNotFound ,
109- cmd .Force ,
110- cmd .DeleteOptions ,
111- cmd .Owner ,
112- log .Default ,
113- )
113+ name , err := cmd .deleteWorkspace (ctx , devPodConfig , []string {arg })
114114 if err != nil {
115- log .WithFields (logrus.Fields {
116- "workspace" : arg ,
117- "err" : err ,
118- }).Error ("failed to delete workspace" )
115+ errs = append (errs , fmt .Errorf ("failed to delete workspace %s: %w" , arg , err ))
116+
119117 continue
120118 }
121- log .WithFields (logrus.Fields {
122- "workspace" : workspaceName ,
123- })
124- log .Default .Donef ("deleted workspace" )
119+
120+ log .Default .Donef ("deleted workspace %s" , name )
121+ }
122+
123+ if len (errs ) > 0 {
124+ return fmt .Errorf (
125+ "%d workspace(s) failed to delete: %v" ,
126+ len (errs ),
127+ errs ,
128+ )
125129 }
130+
126131 return nil
127132}
133+
134+ func (cmd * DeleteCmd ) deleteWorkspace (
135+ ctx context.Context ,
136+ devPodConfig * config.Config ,
137+ args []string ,
138+ ) (string , error ) {
139+ return workspace .Delete (ctx , workspace.DeleteOptions {
140+ DevPodConfig : devPodConfig ,
141+ Args : args ,
142+ IgnoreNotFound : cmd .IgnoreNotFound ,
143+ Force : cmd .Force ,
144+ ClientDelete : cmd .DeleteOptions ,
145+ Owner : cmd .Owner ,
146+ Log : log .Default ,
147+ })
148+ }
0 commit comments