Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change incus file delete operation to sftp #1266

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions cmd/incus/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"strconv"
"strings"

"github.com/pkg/sftp"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"

Expand Down Expand Up @@ -302,6 +303,8 @@ func (c *cmdFileCreate) Run(cmd *cobra.Command, args []string) error {
type cmdFileDelete struct {
global *cmdGlobal
file *cmdFile

flagForce bool
}

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

cmd.Flags().BoolVarP(&c.flagForce, "force", "f", false, i18n.G("Force deleting files, directories, and subdirectories")+"``")

cmd.RunE = c.Run

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

// Store clients.
sftpClients := map[string]*sftp.Client{}

defer func() {
for _, sftpClient := range sftpClients {
_ = sftpClient.Close()
}
}()

for _, resource := range resources {
pathSpec := strings.SplitN(resource.name, "/", 2)
if len(pathSpec) != 2 {
return fmt.Errorf(i18n.G("Invalid path %s"), resource.name)
}

// Delete the file
err = resource.server.DeleteInstanceFile(pathSpec[0], pathSpec[1])
sftpConn, ok := sftpClients[pathSpec[0]]
if !ok {
sftpConn, err = resource.server.GetInstanceFileSFTP(pathSpec[0])
if err != nil {
return err
}

sftpClients[pathSpec[0]] = sftpConn
}

if c.flagForce {
err = sftpConn.RemoveAll(pathSpec[1])
if err != nil {
return err
}

return nil
}

err = sftpConn.Remove(pathSpec[1])
if err != nil {
return err
}
Expand Down
Loading
Loading