-
Notifications
You must be signed in to change notification settings - Fork 18
/
projectdirsyncer.go
53 lines (45 loc) · 1.14 KB
/
projectdirsyncer.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
package projectdirsyncer
import (
"github.com/cirruslabs/cirrus-cli/internal/executor/platform"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"io"
"os"
"path/filepath"
)
func SyncProjectDir(dir string, sshClient *ssh.Client) error {
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
return err
}
defer sftpClient.Close()
return filepath.Walk(dir, func(path string, fileInfo os.FileInfo, err error) error {
// Handle possible error that occurred when reading this directory entry information
if err != nil {
return err
}
relativePath, err := filepath.Rel(dir, path)
if err != nil {
return err
}
remotePath := sftp.Join(platform.NewUnix().GenericWorkingDir(), relativePath)
if fileInfo.Mode().IsDir() {
return sftpClient.MkdirAll(remotePath)
} else if fileInfo.Mode().IsRegular() {
localFile, err := os.Open(path)
if err != nil {
return err
}
defer localFile.Close()
remoteFile, err := sftpClient.Create(remotePath)
if err != nil {
return err
}
defer remoteFile.Close()
if _, err := io.Copy(remoteFile, localFile); err != nil {
return err
}
}
return nil
})
}