-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload2dav.go
161 lines (128 loc) · 3.46 KB
/
upload2dav.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/axllent/ghru"
"github.com/spf13/pflag"
"github.com/studio-b12/gowebdav"
)
var (
config Config
client *gowebdav.Client
quiet bool
version = "dev"
)
func main() {
var showHelp, writeConfig, showVersion, update bool
var configFile, uploadPath string
defaultConfig := Home() + "/.config/upload2dav.json"
flag := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
// set the default help
flag.Usage = func() {
fmt.Printf("Usage: %s [options] <file(s)>\n", os.Args[0])
fmt.Println("\nOptions:")
flag.SortFlags = false
flag.PrintDefaults()
fmt.Println("")
}
flag.StringVarP(&uploadPath, "dir", "d", "", "Alternative upload directory")
flag.StringVarP(&configFile, "conf", "c", defaultConfig, "Specify config file")
flag.BoolVarP(&writeConfig, "write-config", "w", false, "Write config")
flag.BoolVarP(&quiet, "quiet", "q", false, "Quiet (do not show upload progress)")
flag.BoolVarP(&showVersion, "version", "v", false, "Show version")
flag.BoolVarP(&update, "update", "u", false, "Update to latest version")
flag.BoolVarP(&showHelp, "help", "h", false, "Show help")
// parse args excluding os.Args[0]
flag.Parse(os.Args[1:])
// parse arguments
files := flag.Args()
if showHelp {
flag.Usage()
os.Exit(1)
}
if showVersion {
fmt.Println(fmt.Sprintf("Version: %s", version))
latest, _, _, err := ghru.Latest("axllent/upload2dav", "upload2dav")
if err == nil && ghru.GreaterThan(latest, version) {
fmt.Println(fmt.Sprintf("Update available: %s\nRun `%s -u` to update.", latest, os.Args[0]))
}
os.Exit(0)
}
if update {
rel, err := ghru.Update("axllent/upload2dav", "upload2dav", version)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(fmt.Sprintf("Updated %s to version %s", os.Args[0], rel))
os.Exit(0)
}
if writeConfig {
if err := WriteConfig(configFile); err != nil {
fmt.Printf("Error: %s\n\n", err)
os.Exit(1)
}
fmt.Printf("Successfully wrote config: %s\n\n", configFile)
os.Exit(0)
}
if err := ReadConfig(configFile); err != nil {
fmt.Printf("Error: %s\n", err)
fmt.Printf("\nUse -c to specify a configuration file, or -w to create a new one\n\n")
os.Exit(1)
}
if len(files) < 1 {
flag.Usage()
os.Exit(1)
}
if uploadPath != "" {
config.UploadDir = uploadPath
}
client = gowebdav.NewClient(config.ServerAddress, config.Username, config.Password)
if err := CheckDirExists(config.UploadDir); err != nil {
fmt.Printf("Error: %s\n", err)
return
}
for _, file := range files {
if err := Upload(file, config.UploadDir); err != nil {
fmt.Printf("Error: %s\n", err)
}
}
}
// Upload sends a local file to the webdav server
func Upload(file, dir string) error {
info, err := os.Stat(file)
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return fmt.Errorf("%s is not a file", file)
}
wfile, err := os.Open(file)
if err != nil {
return err
}
defer wfile.Close()
outFilename := filepath.Base(file)
uploadName := path.Join(config.UploadDir, outFilename)
if !quiet {
fmt.Printf("Uploading %s to %s ... ", file, uploadName)
}
if err := client.WriteStream(uploadName, wfile, 0664); err != nil {
return err
}
if !quiet {
fmt.Println("done")
}
return nil
}
// CheckDirExists checked first is a directory exists
func CheckDirExists(dir string) error {
if _, err := client.ReadDir(dir); err != nil {
if err := client.MkdirAll(dir, 0644); err != nil {
return err
}
}
return nil
}