-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncDataToFileserver_windows.go
68 lines (59 loc) · 2.15 KB
/
syncDataToFileserver_windows.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
// functionality needed for "de-central" data
package datasetIngestor
import (
"fmt"
"io"
"path"
"regexp"
"strings"
)
// copies data from a local machine to a fileserver, uses scp underneath
func SyncLocalDataToFileserver(datasetId string, user map[string]string, RSYNCServer string, sourceFolder string, absFileListing string, commandOutput io.Writer) (err error) {
username := user["username"]
password := user["password"]
shortDatasetId := strings.Split(datasetId, "/")[1]
// remove leading "C:"" if existing etc
ss := strings.Split(sourceFolder, ":")
// construct destination folder from sourceFolder, sourceFolder allowed to have Windows backslash in folder name
destFull := ss[len(ss)-1]
separator := "/"
if strings.Index(destFull, "/") < 0 {
separator = "\\"
}
destparts := strings.Split(destFull, separator)
destFolder := "archive/" + shortDatasetId + strings.Join(destparts[0:len(destparts)-1], "/")
destFolder2 := "archive/" + shortDatasetId + strings.Join(destparts[0:len(destparts)], "/")
// add port number if missing
FullRSYNCServer := RSYNCServer
if !strings.Contains(RSYNCServer, ":") {
FullRSYNCServer = RSYNCServer + ":22"
}
c, err := NewDumbClient(username, password, FullRSYNCServer)
if err != nil {
return err
}
c.Quiet = false
c.PreseveTimes = true
re := regexp.MustCompile(`^\/([A-Z])\/`)
// now copy recursively: either just one sourceFolder or all entries inside absFileListing
// Note: destfolder must exist before, needs dedicated scp server support
if absFileListing != "" {
lines, err := readLines(absFileListing)
if err != nil {
return fmt.Errorf("could not read filelist, readlines: %v", err)
}
for _, line := range lines {
windowsSource := re.ReplaceAllString(path.Join(sourceFolder, line), "$1:/")
fmt.Fprintf(commandOutput, "Copying data via scp from %s to %s\n", windowsSource, destFolder)
err = c.Send(destFolder2, windowsSource)
if err != nil {
return err
}
}
} else {
windowsSource := re.ReplaceAllString(sourceFolder, "$1:/")
fmt.Fprintf(commandOutput, "Copying data via scp from %s to %s\n", windowsSource, destFolder)
err = c.Send(destFolder, windowsSource)
}
return err
}