-
Notifications
You must be signed in to change notification settings - Fork 1
/
directories.go
43 lines (38 loc) · 1.16 KB
/
directories.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
package sftpClient
import (
"fmt"
"os"
"strings"
)
// CreateDir creates a folder on remote location
func (c *SFTPClient) CreateDir(remoteFolderPath string) error {
if _, err := c.client.Lstat(remoteFolderPath); err != nil {
if os.IsNotExist(err) {
if err := c.client.Mkdir(remoteFolderPath); err != nil {
return fmt.Errorf("Could not create folder 'remote:%s': %v", remoteFolderPath, err)
}
if err := c.client.Chmod(remoteFolderPath, 0755); err != nil {
return fmt.Errorf("Could not set folder permissions on 'remote:%s': %v", remoteFolderPath, err)
}
} else {
return fmt.Errorf("Error finding 'remote:%s': %v", remoteFolderPath, err)
}
}
return nil
}
// CreateDirHierarchy creates a folder hierarchy on remote location
func (c *SFTPClient) CreateDirHierarchy(remoteFolderPath string) error {
parent := "."
if strings.HasPrefix(remoteFolderPath, "/") {
parent = "/"
remoteFolderPath = strings.TrimPrefix(remoteFolderPath, "/")
}
tree := strings.Split(remoteFolderPath, "/")
for _, dir := range tree {
parent = strings.Join([]string{parent, dir}, "/")
if err := c.CreateDir(parent); err != nil {
return err
}
}
return nil
}