refactor main/client args

This commit is contained in:
Christoph Polcin 2014-10-24 13:10:56 +02:00
parent b8d7beb48c
commit b781d56161

View File

@ -5,6 +5,7 @@ import (
"flag" "flag"
"os" "os"
"fmt" "fmt"
"strings"
) )
func Fail(err interface{}) { func Fail(err interface{}) {
@ -14,11 +15,11 @@ func Fail(err interface{}) {
fmt.Println("Usage: client FLAGS ARGS") fmt.Println("Usage: client FLAGS ARGS")
fmt.Println("Flags:") fmt.Println("Flags:")
flag.PrintDefaults() flag.PrintDefaults()
fmt.Println("Method:") fmt.Println("Method <ARGS>")
fmt.Println(" LIST, PROPFIND:") fmt.Println(" LS | LIST | PROPFIND <PATH>")
fmt.Println(" RM, DELETE, DEL:") fmt.Println(" RM | DELETE | DEL <PATH>")
fmt.Println(" MKDIR, MKCOL:") fmt.Println(" MKDIR | MKCOL <PATH>")
fmt.Println(" MKDIRALL, MKCOLALL:") fmt.Println(" MKDIRALL | MKCOLALL <PATH>")
} }
os.Exit(-1) os.Exit(-1)
} }
@ -27,7 +28,8 @@ func main() {
root := flag.String("root", "URL", "WebDAV Endpoint") root := flag.String("root", "URL", "WebDAV Endpoint")
usr := flag.String("user", "", "user") usr := flag.String("user", "", "user")
pw := flag.String("pw", "", "password") pw := flag.String("pw", "", "password")
m := flag.String("X", "GET", "Method ...") mm := strings.ToUpper(*(flag.String("X", "GET", "Method ...")))
m := &mm
flag.Parse() flag.Parse()
if *root == "URL" { if *root == "URL" {
@ -38,11 +40,11 @@ func main() {
if err := c.Connect(); err != nil { if err := c.Connect(); err != nil {
Fail(fmt.Sprintf("Failed to connect due to: %s", err.Error())) Fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
} }
alen := len(flag.Args())
if len(flag.Args()) > 0 { if alen == 1 {
path := flag.Args()[0] path := flag.Args()[0]
switch *m { switch *m {
case "LIST", "PROPFIND": case "LS", "LIST", "PROPFIND":
if files, err := c.ReadDir(path); err == nil { if files, err := c.ReadDir(path); err == nil {
fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", path, len(files))) fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", path, len(files)))
for _, f := range files { for _, f := range files {
@ -77,8 +79,14 @@ func main() {
default: Fail(nil) default: Fail(nil)
} }
} else if alen == 2 {
a0 := flag.Args()[0]
a1 := flag.Args()[1]
switch *m {
default: Fail(nil)
}
} else { } else {
Fail(nil) Fail(nil)
} }
} }