Skip to content

Commit eea1ba6

Browse files
authored
Merge pull request #1266 from HassanAlsamahi/change-incus-file-delete-operatio-to-sftp
Change incus file delete operation to sftp
2 parents 3b1ed12 + bd16f87 commit eea1ba6

File tree

14 files changed

+1023
-937
lines changed

14 files changed

+1023
-937
lines changed

cmd/incus/file.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strconv"
1919
"strings"
2020

21+
"github.com/pkg/sftp"
2122
"github.com/spf13/cobra"
2223
"golang.org/x/crypto/ssh"
2324

@@ -302,6 +303,8 @@ func (c *cmdFileCreate) Run(cmd *cobra.Command, args []string) error {
302303
type cmdFileDelete struct {
303304
global *cmdGlobal
304305
file *cmdFile
306+
307+
flagForce bool
305308
}
306309

307310
func (c *cmdFileDelete) Command() *cobra.Command {
@@ -312,6 +315,8 @@ func (c *cmdFileDelete) Command() *cobra.Command {
312315
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
313316
`Delete files in instances`))
314317

318+
cmd.Flags().BoolVarP(&c.flagForce, "force", "f", false, i18n.G("Force deleting files, directories, and subdirectories")+"``")
319+
315320
cmd.RunE = c.Run
316321

317322
return cmd
@@ -330,14 +335,41 @@ func (c *cmdFileDelete) Run(cmd *cobra.Command, args []string) error {
330335
return err
331336
}
332337

338+
// Store clients.
339+
sftpClients := map[string]*sftp.Client{}
340+
341+
defer func() {
342+
for _, sftpClient := range sftpClients {
343+
_ = sftpClient.Close()
344+
}
345+
}()
346+
333347
for _, resource := range resources {
334348
pathSpec := strings.SplitN(resource.name, "/", 2)
335349
if len(pathSpec) != 2 {
336350
return fmt.Errorf(i18n.G("Invalid path %s"), resource.name)
337351
}
338352

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])
341373
if err != nil {
342374
return err
343375
}

0 commit comments

Comments
 (0)