@@ -18,6 +18,7 @@ import (
18
18
"strconv"
19
19
"strings"
20
20
21
+ "github.com/pkg/sftp"
21
22
"github.com/spf13/cobra"
22
23
"golang.org/x/crypto/ssh"
23
24
@@ -302,6 +303,8 @@ func (c *cmdFileCreate) Run(cmd *cobra.Command, args []string) error {
302
303
type cmdFileDelete struct {
303
304
global * cmdGlobal
304
305
file * cmdFile
306
+
307
+ flagForce bool
305
308
}
306
309
307
310
func (c * cmdFileDelete ) Command () * cobra.Command {
@@ -312,6 +315,8 @@ func (c *cmdFileDelete) Command() *cobra.Command {
312
315
cmd .Long = cli .FormatSection (i18n .G ("Description" ), i18n .G (
313
316
`Delete files in instances` ))
314
317
318
+ cmd .Flags ().BoolVarP (& c .flagForce , "force" , "f" , false , i18n .G ("Force deleting files, directories, and subdirectories" )+ "``" )
319
+
315
320
cmd .RunE = c .Run
316
321
317
322
return cmd
@@ -330,14 +335,41 @@ func (c *cmdFileDelete) Run(cmd *cobra.Command, args []string) error {
330
335
return err
331
336
}
332
337
338
+ // Store clients.
339
+ sftpClients := map [string ]* sftp.Client {}
340
+
341
+ defer func () {
342
+ for _ , sftpClient := range sftpClients {
343
+ _ = sftpClient .Close ()
344
+ }
345
+ }()
346
+
333
347
for _ , resource := range resources {
334
348
pathSpec := strings .SplitN (resource .name , "/" , 2 )
335
349
if len (pathSpec ) != 2 {
336
350
return fmt .Errorf (i18n .G ("Invalid path %s" ), resource .name )
337
351
}
338
352
339
- // Delete the file
340
- err = resource .server .DeleteInstanceFile (pathSpec [0 ], pathSpec [1 ])
353
+ sftpConn , ok := sftpClients [pathSpec [0 ]]
354
+ if ! ok {
355
+ sftpConn , err = resource .server .GetInstanceFileSFTP (pathSpec [0 ])
356
+ if err != nil {
357
+ return err
358
+ }
359
+
360
+ sftpClients [pathSpec [0 ]] = sftpConn
361
+ }
362
+
363
+ if c .flagForce {
364
+ err = sftpConn .RemoveAll (pathSpec [1 ])
365
+ if err != nil {
366
+ return err
367
+ }
368
+
369
+ return nil
370
+ }
371
+
372
+ err = sftpConn .Remove (pathSpec [1 ])
341
373
if err != nil {
342
374
return err
343
375
}
0 commit comments