Skip to content

Commit 814a289

Browse files
Part of #1176
define defer outside of loop Signed-off-by: HassanAlsamahi <[email protected]>
1 parent 437503e commit 814a289

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

cmd/incus/file.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -561,18 +561,29 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error {
561561
return err
562562
}
563563

564+
sftpClients := map[string]*sftp.Client{}
565+
566+
defer func() {
567+
for _, sftpClient := range sftpClients {
568+
_ = sftpClient.Close()
569+
}
570+
}()
571+
564572
for _, resource := range resources {
565573
pathSpec := strings.SplitN(resource.name, "/", 2)
566574
if len(pathSpec) != 2 {
567575
return fmt.Errorf(i18n.G("Invalid source %s"), resource.name)
568576
}
569577

570-
sftpConn, err := resource.server.GetInstanceFileSFTP(pathSpec[0])
571-
if err != nil {
572-
return err
573-
}
578+
sftpConn, ok := sftpClients[pathSpec[0]]
579+
if !ok {
580+
sftpConn, err = resource.server.GetInstanceFileSFTP(pathSpec[0])
581+
if err != nil {
582+
return err
583+
}
574584

575-
defer func() { _ = sftpConn.Close() }()
585+
sftpClients[pathSpec[0]] = sftpConn
586+
}
576587

577588
src, err := sftpConn.Open(pathSpec[1])
578589
if err != nil {

0 commit comments

Comments
 (0)