Skip to content

Commit

Permalink
Merge melbahja#14
Browse files Browse the repository at this point in the history
Fix for melbahja#13
  • Loading branch information
melbahja authored Dec 29, 2020
2 parents 9ad7313 + 7307f88 commit bce9642
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
type Client struct {
*ssh.Client
Config *Config
sftp *sftp.Client
}

// Config for Client.
Expand Down Expand Up @@ -133,11 +132,6 @@ func (c Client) NewSftp(opts ...sftp.ClientOption) (*sftp.Client, error) {

// Close client net connection.
func (c Client) Close() error {

if c.sftp != nil {
c.sftp.Close()
}

return c.Client.Close()
}

Expand All @@ -148,13 +142,13 @@ func (c Client) Upload(localPath string, remotePath string) (err error) {
if err != nil {
return
}

defer local.Close()

ftp, err := c.ftp()
ftp, err := c.NewSftp()
if err != nil {
return
}
defer ftp.Close()

remote, err := ftp.Create(remotePath)
if err != nil {
Expand All @@ -175,10 +169,11 @@ func (c Client) Download(remotePath string, localPath string) (err error) {
}
defer local.Close()

ftp, err := c.ftp()
ftp, err := c.NewSftp()
if err != nil {
return
}
defer ftp.Close()

remote, err := ftp.Open(remotePath)
if err != nil {
Expand All @@ -192,17 +187,3 @@ func (c Client) Download(remotePath string, localPath string) (err error) {

return local.Sync()
}

// get sftp client if not set.
func (c *Client) ftp() (*sftp.Client, error) {

if c.sftp == nil {
sftp, err := c.NewSftp()
if err != nil {
return nil, err
}
c.sftp = sftp
}

return c.sftp, nil
}

0 comments on commit bce9642

Please sign in to comment.