-
Notifications
You must be signed in to change notification settings - Fork 1
/
sftp-client.go
51 lines (41 loc) · 1.02 KB
/
sftp-client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package sftpClient
import (
"github.com/pkg/sftp"
sshclient "github.com/bowlhat/ssh-client"
)
// SFTPClient is a proxy to sftp.Client
type SFTPClient struct {
client *sftp.Client
connection *sshclient.SSHConnection
}
// ErrorResponse is a response which can only ever be an error
type ErrorResponse struct {
Err error
}
// FileResponse is a reponse which contains a filename or error
type FileResponse struct {
File string
Err error
}
// FolderMapping maps local to remote folders
type FolderMapping struct {
Local string
Remote string
}
// New SFTP Connection
func New(hostname string, port int, username string, password string) (client *SFTPClient, err error) {
ssh, err := sshclient.New(hostname, port, username, password)
if err != nil {
return nil, err
}
sftpClient, err := sftp.NewClient(ssh.Client)
if err != nil {
ssh.Close()
return nil, err
}
return &SFTPClient{client: sftpClient, connection: ssh}, nil
}
// Close the SFTP session
func (c *SFTPClient) Close() error {
return c.connection.Close()
}