-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (52 loc) · 1.41 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/studio-b12/gowebdav"
)
type config struct {
Username string `json:"username"`
Password string `json:"password"`
Root string `json:"root"`
LocalPath string `json:"localPath"`
}
func loadConfiguration(file string) config {
var config config
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return config
}
func getFiles(directory string, client *gowebdav.Client) []os.FileInfo {
files, err := client.ReadDir(directory)
if err != nil {
fmt.Println(err)
}
return files
}
func walkWebdavDir(directory string, currentLocalPath string, client *gowebdav.Client) {
os.MkdirAll(currentLocalPath, os.ModePerm)
for _, file := range getFiles(directory, client) {
webDavSubDirectory := filepath.Join(directory, file.Name())
localPath := filepath.Join(currentLocalPath, file.Name())
if file.IsDir() {
walkWebdavDir(webDavSubDirectory, localPath, client)
} else {
bytes, _ := client.Read(webDavSubDirectory)
ioutil.WriteFile(localPath, bytes, 0644)
fmt.Println(localPath)
}
}
}
func main() {
config := loadConfiguration("./config.json")
client := gowebdav.NewClient(config.Root, config.Username, config.Password)
walkWebdavDir("product_images/", config.LocalPath, client)
}