refactor cmd
This commit is contained in:
parent
34368960d0
commit
3993494db0
22
README.md
22
README.md
@ -14,27 +14,35 @@ go get -u github.com/studio-b12/gowebdav/cmd/gowebdav
|
||||
## Usage
|
||||
|
||||
```sh
|
||||
$ gowebdav
|
||||
Usage: gowebdav FLAGS ARGS
|
||||
Flags:
|
||||
-X string
|
||||
Method ... (default "GET")
|
||||
Method (default "GET")
|
||||
-pw string
|
||||
password
|
||||
Password [ENV.PASSWORD]
|
||||
-root string
|
||||
WebDAV Endpoint (default "URL")
|
||||
WebDAV Endpoint [ENV.ROOT]
|
||||
-user string
|
||||
user
|
||||
User [ENV.USER]
|
||||
Method <ARGS>
|
||||
LS | LIST | PROPFIND <PATH>
|
||||
STAT <PATH>
|
||||
RM | DELETE | DEL <PATH>
|
||||
MKDIR | MKCOL <PATH>
|
||||
MKDIRALL | MKCOLALL <PATH>
|
||||
MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>
|
||||
CP | COPY <OLD_PATH> <NEW_PATH>
|
||||
GET | PULL | READ <PATH>
|
||||
GET | PULL | READ <PATH> <FILE>
|
||||
PUT | PUSH | WRITE <PATH> <FILE>
|
||||
STAT <PATH>
|
||||
```
|
||||
|
||||
*Example*
|
||||
|
||||
```sh
|
||||
ROOT="https://webdav.server/" \
|
||||
USER="foo" \
|
||||
PASSWORD="bar" \
|
||||
./gowebdav -X LS /
|
||||
```
|
||||
|
||||
## LINKS
|
||||
|
@ -13,20 +13,6 @@ import (
|
||||
func fail(err interface{}) {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("Usage: gowebdav FLAGS ARGS")
|
||||
fmt.Println("Flags:")
|
||||
flag.PrintDefaults()
|
||||
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>")
|
||||
fmt.Println(" MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>")
|
||||
fmt.Println(" CP | COPY <OLD_PATH> <NEW_PATH>")
|
||||
fmt.Println(" GET | PULL | READ <PATH>")
|
||||
fmt.Println(" PUT | PUSH | WRITE <PATH> <FILE>")
|
||||
fmt.Println(" STAT <PATH>")
|
||||
}
|
||||
os.Exit(-1)
|
||||
}
|
||||
@ -42,118 +28,128 @@ func writeFile(path string, bytes []byte, mode os.FileMode) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func main() {
|
||||
root := flag.String("root", "URL", "WebDAV Endpoint")
|
||||
usr := flag.String("user", "", "user")
|
||||
pw := flag.String("pw", "", "password")
|
||||
m := flag.String("X", "GET", "Method ...")
|
||||
flag.Parse()
|
||||
|
||||
if *root == "URL" {
|
||||
fail(nil)
|
||||
func getCmd(method string) func(c *d.Client, p0, p1 string) error {
|
||||
switch method {
|
||||
case "LS", "LIST", "PROPFIND":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
files, err := c.ReadDir(p0)
|
||||
if err == nil {
|
||||
fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", p0, len(files)))
|
||||
for _, f := range files {
|
||||
fmt.Println(f)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
M := strings.ToUpper(*m)
|
||||
m = &M
|
||||
case "STAT":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
file, err := c.Stat(p0)
|
||||
if err == nil {
|
||||
fmt.Println(file)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
case "GET", "PULL", "READ":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
bytes, err := c.Read(p0)
|
||||
if err == nil {
|
||||
if err = writeFile(p1, bytes, 0644); err == nil {
|
||||
fmt.Println(fmt.Sprintf("Written %d bytes to: %s", len(bytes), p1))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
case "DELETE", "RM", "DEL":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
if err = c.Remove(p0); err == nil {
|
||||
fmt.Println("RM: " + p0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
case "MKCOL", "MKDIR":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
if err = c.Mkdir(p0, 0755); err == nil {
|
||||
fmt.Println("MkDir: " + p0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
case "MKCOLALL", "MKDIRALL":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
if err = c.MkdirAll(p0, 0755); err == nil {
|
||||
fmt.Println("MkDirAll: " + p0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
case "RENAME", "MV", "MOVE":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
if err = c.Rename(p0, p1, true); err == nil {
|
||||
fmt.Println("Rename: " + p0 + " -> " + p1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
case "COPY", "CP":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
if err = c.Copy(p0, p1, true); err == nil {
|
||||
fmt.Println("Copy: " + p0 + " -> " + p1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
case "PUT", "PUSH", "WRITE":
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
stream, err := getStream(p1)
|
||||
if err == nil {
|
||||
if err = c.WriteStream(p0, stream, 0644); err == nil {
|
||||
fmt.Println(fmt.Sprintf("Put: '%s' -> %s", p1, p0))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
return func(c *d.Client, p0, p1 string) (err error) {
|
||||
return errors.New("Unsupported method: " + method)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
root := flag.String("root", os.Getenv("ROOT"), "WebDAV Endpoint [ENV.ROOT]")
|
||||
usr := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
|
||||
pw := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
|
||||
m := flag.String("X", "GET", "Method")
|
||||
flag.Parse()
|
||||
|
||||
if *root == "" {
|
||||
fail("Set WebDAV ROOT")
|
||||
}
|
||||
|
||||
var path0, path1 string
|
||||
switch len(flag.Args()) {
|
||||
case 1:
|
||||
path0 = flag.Args()[0]
|
||||
case 2:
|
||||
path1 = flag.Args()[1]
|
||||
default:
|
||||
fail("Unsupported arguments")
|
||||
}
|
||||
|
||||
c := d.NewClient(*root, *usr, *pw)
|
||||
if err := c.Connect(); err != nil {
|
||||
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
|
||||
}
|
||||
alen := len(flag.Args())
|
||||
if alen == 1 {
|
||||
path := flag.Args()[0]
|
||||
switch *m {
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
case "STAT":
|
||||
if file, err := c.Stat(path); err == nil {
|
||||
fmt.Println(file)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
cmd := getCmd(strings.ToUpper(*m))
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
case "DELETE", "RM", "DEL":
|
||||
if err := c.Remove(path); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("Remove: " + path)
|
||||
}
|
||||
|
||||
case "MKCOL", "MKDIR":
|
||||
if err := c.Mkdir(path, 0); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("MkDir: " + path)
|
||||
}
|
||||
|
||||
case "MKCOLALL", "MKDIRALL":
|
||||
if err := c.MkdirAll(path, 0); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("MkDirAll: " + path)
|
||||
}
|
||||
|
||||
default:
|
||||
fail(nil)
|
||||
}
|
||||
|
||||
} else if alen == 2 {
|
||||
a0 := flag.Args()[0]
|
||||
a1 := flag.Args()[1]
|
||||
switch *m {
|
||||
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)
|
||||
}
|
||||
|
||||
case "PUT", "PUSH", "WRITE":
|
||||
stream, err := getStream(a1)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
if err := c.WriteStream(a0, stream, 0644); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(fmt.Sprintf("Written: '%s' -> %s", a1, a0))
|
||||
}
|
||||
|
||||
default:
|
||||
fail(nil)
|
||||
}
|
||||
} else {
|
||||
fail(nil)
|
||||
if e := cmd(c, path0, path1); e != nil {
|
||||
fail(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user