gowebdav/main/client.go

182 lines
3.8 KiB
Go
Raw Permalink Normal View History

2014-10-23 10:39:55 +02:00
package main
import (
2014-10-27 14:32:16 +01:00
"errors"
2014-10-23 10:39:55 +02:00
"flag"
"fmt"
2014-10-24 14:04:43 +02:00
d "gowebdav"
2014-10-27 14:32:16 +01:00
"io"
2014-10-24 14:04:43 +02:00
"os"
2014-10-24 13:10:56 +02:00
"strings"
2014-10-23 10:39:55 +02:00
)
func Fail(err interface{}) {
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Usage: client FLAGS ARGS")
fmt.Println("Flags:")
flag.PrintDefaults()
2014-10-24 13:10:56 +02:00
fmt.Println("Method <ARGS>")
fmt.Println(" LS | LIST | PROPFIND <PATH>")
fmt.Println(" RM | DELETE | DEL <PATH>")
fmt.Println(" MKDIR | MKCOL <PATH>")
fmt.Println(" MKDIRALL | MKCOLALL <PATH>")
2014-10-24 14:08:42 +02:00
fmt.Println(" MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>")
fmt.Println(" CP | COPY <OLD_PATH> <NEW_PATH>")
2014-10-27 14:32:16 +01:00
fmt.Println(" GET | PULL | READ <PATH>")
fmt.Println(" PUT | PUSH | WRITE <PATH> <FILE>")
2014-10-27 17:02:28 +01:00
fmt.Println(" STAT <PATH>")
2014-10-23 10:39:55 +02:00
}
os.Exit(-1)
}
2014-10-27 14:32:16 +01:00
func writeFile(path string, bytes []byte, mode os.FileMode) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(bytes)
return err
}
2014-10-23 10:39:55 +02:00
func main() {
root := flag.String("root", "URL", "WebDAV Endpoint")
usr := flag.String("user", "", "user")
pw := flag.String("pw", "", "password")
2014-10-24 14:16:54 +02:00
m := flag.String("X", "GET", "Method ...")
2014-10-23 10:39:55 +02:00
flag.Parse()
if *root == "URL" {
Fail(nil)
}
2014-10-24 14:16:54 +02:00
M := strings.ToUpper(*m)
m = &M
2014-10-23 10:39:55 +02:00
c := d.NewClient(*root, *usr, *pw)
if err := c.Connect(); err != nil {
Fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
2014-10-23 10:39:55 +02:00
}
2014-10-24 13:10:56 +02:00
alen := len(flag.Args())
if alen == 1 {
2014-10-23 10:39:55 +02:00
path := flag.Args()[0]
switch *m {
2014-10-24 14:04:43 +02:00
case "LS", "LIST", "PROPFIND":
if files, err := c.ReadDir(path); err == nil {
fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", path, len(files)))
for _, f := range files {
fmt.Println(f)
2014-10-23 10:39:55 +02:00
}
2014-10-24 14:04:43 +02:00
} else {
fmt.Println(err)
}
2014-10-23 14:10:31 +02:00
2014-10-27 17:02:28 +01:00
case "STAT":
if file, err := c.Stat(path); err == nil {
fmt.Println(file)
} else {
fmt.Println(err)
}
2014-10-27 14:32:16 +01:00
case "GET", "PULL", "READ":
if bytes, err := c.Read(path); err == nil {
if lidx := strings.LastIndex(path, "/"); lidx != -1 {
path = path[lidx+1:]
}
if err := writeFile(path, bytes, 0644); err != nil {
fmt.Println(err)
} else {
fmt.Println(fmt.Sprintf("Written %d bytes to: %s", len(bytes), path))
}
} else {
fmt.Println(err)
}
2014-10-23 14:10:31 +02:00
2014-10-24 14:04:43 +02:00
case "DELETE", "RM", "DEL":
if err := c.Remove(path); err != nil {
fmt.Println(err)
} else {
fmt.Println("Remove: " + path)
}
2014-10-23 15:00:20 +02:00
2014-10-24 14:04:43 +02:00
case "MKCOL", "MKDIR":
if err := c.Mkdir(path, 0); err != nil {
fmt.Println(err)
} else {
fmt.Println("MkDir: " + path)
}
2014-10-23 14:10:31 +02:00
2014-10-24 14:04:43 +02:00
case "MKCOLALL", "MKDIRALL":
if err := c.MkdirAll(path, 0); err != nil {
fmt.Println(err)
} else {
fmt.Println("MkDirAll: " + path)
}
2014-10-24 11:31:16 +02:00
2014-10-24 14:04:43 +02:00
default:
Fail(nil)
2014-10-23 10:39:55 +02:00
}
2014-10-24 13:10:56 +02:00
} else if alen == 2 {
a0 := flag.Args()[0]
a1 := flag.Args()[1]
switch *m {
2014-10-24 14:08:42 +02:00
case "RENAME", "MV", "MOVE":
if err := c.Rename(a0, a1, true); err != nil {
fmt.Println(err)
} else {
fmt.Println("Rename: " + a0 + " -> " + a1)
}
case "COPY", "CP":
if err := c.Copy(a0, a1, true); err != nil {
fmt.Println(err)
} else {
fmt.Println("Copy: " + a0 + " -> " + a1)
}
2014-10-27 14:32:16 +01:00
case "PUT", "PUSH", "WRITE":
2014-10-27 15:17:54 +01:00
stream, err := getStream(a1)
2014-10-27 14:32:16 +01:00
if err != nil {
Fail(err)
}
2014-10-27 15:17:54 +01:00
if err := c.WriteStream(a0, stream, 0644); err != nil {
2014-10-27 14:32:16 +01:00
fmt.Println(err)
} else {
fmt.Println(fmt.Sprintf("Written: '%s' -> %s", a1, a0))
}
2014-10-24 14:04:43 +02:00
default:
Fail(nil)
2014-10-24 13:10:56 +02:00
}
2014-10-23 10:39:55 +02:00
} else {
Fail(nil)
}
}
2014-10-27 14:32:16 +01:00
2014-10-27 15:17:54 +01:00
func getStream(pathOrString string) (io.ReadCloser, error) {
2014-10-27 14:32:16 +01:00
fi, err := os.Stat(pathOrString)
if err == nil {
if fi.IsDir() {
return nil, &os.PathError{"Open", pathOrString, errors.New("Path: '" + pathOrString + "' is a directory")}
}
f, err := os.Open(pathOrString)
if err == nil {
2014-10-27 15:17:54 +01:00
return f, nil
2014-10-27 14:32:16 +01:00
}
return nil, &os.PathError{"Open", pathOrString, err}
} else {
2014-10-27 15:17:54 +01:00
return nopCloser{strings.NewReader(pathOrString)}, nil
2014-10-27 14:32:16 +01:00
}
}
2014-10-27 15:17:54 +01:00
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error {
return nil
}