Skip to content

Commit 389f4cd

Browse files
committed
incus/file/delete: Cache the SFTP client
Signed-off-by: Stéphane Graber <[email protected]>
1 parent c953583 commit 389f4cd

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

cmd/incus/file.go

+18-5
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

@@ -334,18 +335,30 @@ func (c *cmdFileDelete) Run(cmd *cobra.Command, args []string) error {
334335
return err
335336
}
336337

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

343-
sftpConn, err := resource.server.GetInstanceFileSFTP(pathSpec[0])
344-
if err != nil {
345-
return err
346-
}
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+
}
347359

348-
defer func() { _ = sftpConn.Close() }()
360+
sftpClients[pathSpec[0]] = sftpConn
361+
}
349362

350363
if c.flagForce {
351364
err = sftpConn.RemoveAll(pathSpec[1])

0 commit comments

Comments
 (0)