-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer_sftp_xp.go
executable file
·103 lines (85 loc) · 3.22 KB
/
transfer_sftp_xp.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
//go:build !go1.11
// +build !go1.11
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
var tempCounter = 0
func winscp(cmd ...string) error {
ex, err := os.Executable()
if err != nil {
panic(err)
}
tempCounter = (tempCounter + 1) % 10000
tempFilename := fmt.Sprintf("temp_%d.bat", tempCounter)
// Next we'll look at a slightly more involved case
// where we pipe data to the external process on its
// `stdin` and collect the results from its `stdout`.
f, err := os.Create(tempFilename)
_, err = f.WriteString(filepath.Dir(ex) + "\\winscp.com " + strings.Join(cmd, " "))
_ = f.Close()
defer os.Remove(tempFilename)
grepCmd := exec.Command(tempFilename)
// Here we explicitly grab input/output pipes, start
// the process, write some input to it, read the
// resulting output, and finally wait for the process
// to exit.
_, err = grepCmd.CombinedOutput()
grepCmd.Wait()
return err
}
// TransferManagerSftp reacts on the channel done_files.
// If folder of file is ready to send it sends it via WebDAV (HTTP) to <CMD arg -dst>.
// It also initializes the zipping if <CMD arg -zip> is set.
type TransferManagerSftp struct {
args *Args
}
// doWork runs in a endless loop. It reacts on the channel done_files.
// If folder of file is ready to send it sends it via HWebDAV (HTTP) to <CMD arg -dst>.
// It also initializes the zipping if <CMD arg -zip> is set
// It terminates as soon as a value is pushed into quit. Run in extra goroutine.
func (m *TransferManagerSftp) doWork(quit chan int) {
doWorkImplementation(quit, m, m.args)
}
func (m *TransferManagerSftp) connect_to_server() error {
//user := m.args.user
//password := m.args.pass
sftpConnStr := fmt.Sprintf("sftp://%s:%s@%s/", m.args.user, m.args.pass, m.args.dst.Host)
//return winscp("sftp://martin:[email protected]:22/home/martin/data_sft_test")
return winscp(sftpConnStr)
}
// send_file sends a file via WebDAV
func (m *TransferManagerSftp) send_file(path_to_file string, file os.FileInfo) (bool, error) {
var webdavFilePath, urlPathDir string
if m.args.sendType == "file" {
webdavFilePath = file.Name()
} else if relpath, err := filepath.Rel(TempPath, path_to_file); err == nil {
webdavFilePath = strings.Replace(relpath, string(os.PathSeparator), "/", -1)
webdavFilePath = strings.TrimPrefix(webdavFilePath, "./")
} else {
return false, err
}
webdavFilePath = filepath.Join(m.args.dst.Path, webdavFilePath)
urlPathDir = filepath.Dir(webdavFilePath)
InfoLogger.Println("Sending...", webdavFilePath)
sftpConnStr := fmt.Sprintf("sftp://%s:%s@%s/", m.args.user, m.args.pass, m.args.dst.Host)
if relpath, err := filepath.Rel(m.args.dst.Path, urlPathDir); err == nil {
urlPathDir = strings.Replace(relpath, string(os.PathSeparator), "/", -1)
} else {
return false, err
}
cwp := m.args.dst.Path
webdavFilePath = strings.Replace(webdavFilePath, string(os.PathSeparator), "/", -1)
for _, s := range strings.Split(urlPathDir, "/") {
cwp := cwp + "/" + s
_ = winscp("/command \"open "+sftpConnStr+"\"", fmt.Sprintf("\"mkdir \"\"%s\"\"\"", cwp), "\"exit\"")
}
err := winscp("/command", "\"open "+sftpConnStr+"\"",
fmt.Sprintf("\"put \"\"%s\"\" \"\"%s\"\"\"", path_to_file, webdavFilePath),
"\"exit\"")
return err == nil, err
}