forked from wal-g/storages
/
folder.go
187 lines (151 loc) · 3.82 KB
/
folder.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package sh
import (
"github.com/pkg/sftp"
"github.com/wal-g/storages/storage"
"github.com/wal-g/tracelog"
"golang.org/x/crypto/ssh"
"io"
"fmt"
"path/filepath"
)
type Folder struct {
client SftpClient
path string
}
const (
Port = "SSH_PORT"
Password = "SSH_PASSWORD"
Username = "SSH_USERNAME"
)
var SettingsList = []string{
Port,
Password,
Username,
};
func NewFolderError(err error, format string, args ...interface{}) storage.Error {
return storage.NewError(err, "SSH", format, args...)
}
func ConfigureFolder(prefix string, settings map[string]string) (storage.Folder, error) {
host, path, err := storage.ParsePrefixAsURL(prefix)
if err != nil {
return nil, err
}
user := settings[Username]
pass := settings[Password]
port := settings[Port]
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(pass),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
address := fmt.Sprint(host, ":", port)
sshClient, err := ssh.Dial("tcp", address, config)
if err != nil {
return nil, NewFolderError(err, "Fail connect via ssh. Address: %s", address)
}
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
return nil, NewFolderError(err, "Fail connect via sftp. Address: %s", address)
}
return &Folder{
extend(sftpClient), path,
}, nil
}
// TODO close ssh and sftp connection
func closeConnection(client io.Closer) {
err := client.Close()
if err != nil {
tracelog.WarningLogger.FatalOnError(err)
}
}
func (folder *Folder) GetPath() string {
return folder.path
}
func (folder *Folder) ListFolder() (objects []storage.Object, subFolders []storage.Folder, err error) {
client := folder.client
path := folder.path
filesInfo, err := client.ReadDir(folder.path)
if err != nil {
return nil, nil,
NewFolderError(err, "Fail read folder '%s'", path)
}
for _, fileInfo := range filesInfo {
if fileInfo.IsDir() {
folder := &Folder{
folder.client,
client.Join(path, fileInfo.Name()),
}
subFolders = append(subFolders, folder)
}
object := storage.NewLocalObject(
fileInfo.Name(),
fileInfo.ModTime(),
)
objects = append(objects, object)
}
return
}
func (folder *Folder) DeleteObjects(objectRelativePaths []string) error {
client := folder.client
for _, relativePath := range objectRelativePaths {
path := client.Join(folder.path, relativePath)
err := client.Remove(path)
if err != nil {
return NewFolderError(err, "Fail delete object '%s'", path)
}
}
return nil
}
func (folder *Folder) Exists(objectRelativePath string) (bool, error) {
path := filepath.Join(folder.path, objectRelativePath)
_, err := folder.client.Stat(path)
if err != nil {
return false, NewFolderError(
err, "Fail check object existence '%s'", path,
)
}
return true, nil
}
func (folder *Folder) GetSubFolder(subFolderRelativePath string) storage.Folder {
return &Folder{
folder.client,
folder.client.Join(folder.path, subFolderRelativePath),
}
}
func (folder *Folder) ReadObject(objectRelativePath string) (io.ReadCloser, error) {
path := folder.client.Join(folder.path, objectRelativePath)
file, err := folder.client.OpenFile(path)
if err != nil {
return nil, NewFolderError(err, "Fail open file '%s'", path)
}
return file, nil
}
func (folder *Folder) PutObject(name string, content io.Reader) error {
client := folder.client
absolutePath := filepath.Join(folder.path, name)
dirPath := filepath.Dir(absolutePath)
err := client.Mkdir(dirPath)
if err != nil {
return NewFolderError(
err, "Fail to create directory '%s'",
dirPath,
)
}
file, err := client.CreateFile(absolutePath)
if err != nil {
return NewFolderError(
err, "Fail to create file '%s'",
absolutePath,
)
}
_, err = io.Copy(file, content)
if err != nil {
return NewFolderError(
err, "Fail write content to file '%s'",
absolutePath,
)
}
return nil
}