-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsftp_util.go
More file actions
162 lines (121 loc) · 3.24 KB
/
sftp_util.go
File metadata and controls
162 lines (121 loc) · 3.24 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"os"
"log"
"path"
"io/ioutil"
"fmt"
"time"
"net"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
// Connect
func connect(user, password, host string, port int) (*sftp.Client, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
sshClient *ssh.Client
sftpClient *sftp.Client
err error
)
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(password))
clientConfig = &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: 30 * time.Second,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
addr = fmt.Sprintf("%s:%d", host, port)
if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}
if sftpClient, err = sftp.NewClient(sshClient); err != nil {
return nil, err
}
return sftpClient, nil
}
// UploadFile
func uploadFile(localFilePath, remoteDir string) {
srcFile, err := os.Open(localFilePath)
if err != nil {
log.Fatal(err)
}
defer srcFile.Close()
var remoteFileName = getChangedFileName(localFilePath)
dstFile, err := sftpClient.Create(path.Join(remoteDir, remoteFileName))
if err != nil {
log.Fatal("dst error: ", err, path.Join(remoteDir, remoteFileName))
}
defer dstFile.Close()
buf, err := ioutil.ReadAll(srcFile)
if err != nil {
panic(err)
}
dstFile.Write(buf)
log.Print("uploaded file: ", localFilePath)
}
// UploadDirectory
func uploadDirectory(localPath, remotePath string) {
localFiles, err := ioutil.ReadDir(localPath)
if err != nil {
panic(err)
}
changedFileName := getChangedFileName(localPath)
sftpClient.Mkdir(path.Join(remotePath, changedFileName))
log.Print("make remote dir: ", path.Join(remotePath, getChangedFileName(localPath)))
for _, backupDir := range localFiles {
localFilePath := path.Join(localPath, backupDir.Name())
remoteFilePath := path.Join(remotePath, changedFileName, backupDir.Name())
if backupDir.IsDir() {
sftpClient.Mkdir(remoteFilePath)
uploadDirectory(localFilePath, remotePath)
} else {
uploadFile(path.Join(localPath, backupDir.Name()), remotePath)
}
}
}
// Remove whatever a file or directory
func remove(filename, remoteDir string) {
remoteFileName := path.Join(remoteDir, getChangedFileName(filename))
fileInfo, err := sftpClient.Stat(remoteFileName)
if err != nil {
log.Println(err)
return
}
if fileInfo.IsDir() {
removeDirectory(remoteFileName, remoteDir)
} else {
removeFile(remoteFileName)
}
}
// Remove file
func removeFile(filename string) {
err := sftpClient.Remove(filename)
if err != nil {
log.Fatal("Can' remove file :", filename)
} else {
log.Print("Removed file: ", filename)
}
}
// remove Directory
func removeDirectory(dir , remoteDir string) {
fileInfos, err := sftpClient.ReadDir(dir)
if err != nil {
log.Fatal("Readdir err: ", err)
}
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
fmt.Println("remove directory: ", path.Join(dir, fileInfo.Name()))
removeDirectory(path.Join(dir, fileInfo.Name()), remoteDir)
} else {
fmt.Println(path.Join(remoteDir, getChangedFileName(dir)))
removeFile(path.Join(remoteDir, getChangedFileName(dir), fileInfo.Name()))
}
}
sftpClient.RemoveDirectory(dir)
}