implement MkDir aka MKCOL
This commit is contained in:
parent
7caa8c8b7d
commit
c1fc968bc9
14
client.go
14
client.go
@ -131,6 +131,20 @@ func (c *Client) Remove(path string) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Mkdir(path string) error {
|
||||
path = FixSlashes(path)
|
||||
status := c.MkCol(path)
|
||||
if status == 201 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if status == 409 {
|
||||
// TODO
|
||||
}
|
||||
|
||||
return errors.New(fmt.Sprintf("%d", status))
|
||||
}
|
||||
|
||||
func (c *Client) Read(path string) {
|
||||
fmt.Println("Read " + path)
|
||||
}
|
||||
|
@ -14,6 +14,10 @@ func Fail(err interface{}) {
|
||||
fmt.Println("Usage: client FLAGS ARGS")
|
||||
fmt.Println("Flags:")
|
||||
flag.PrintDefaults()
|
||||
fmt.Println("Method:")
|
||||
fmt.Println(" LIST, PROPFIND:")
|
||||
fmt.Println(" RM, DELETE, DEL:")
|
||||
fmt.Println(" MKDIR, MKCOL:")
|
||||
}
|
||||
os.Exit(-1)
|
||||
}
|
||||
@ -22,7 +26,7 @@ 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: LIST aka PROPFIND, GET, DELETE")
|
||||
m := flag.String("X", "GET", "Method ...")
|
||||
flag.Parse()
|
||||
|
||||
if *root == "URL" {
|
||||
@ -39,7 +43,7 @@ func main() {
|
||||
switch *m {
|
||||
case "LIST", "PROPFIND":
|
||||
if files, err := c.ReadDir(path); err == nil {
|
||||
fmt.Println(fmt.Sprintf("Resources: %d - %s", len(files), path))
|
||||
fmt.Println(fmt.Sprintf("ReadDir: %s, entries: ", path, len(files)))
|
||||
for _, f := range files {
|
||||
fmt.Println(f)
|
||||
}
|
||||
@ -49,9 +53,18 @@ func main() {
|
||||
|
||||
case "GET": c.Read(path)
|
||||
|
||||
case "DELETE", "RM":
|
||||
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); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("MkDir: " + path)
|
||||
}
|
||||
|
||||
default: Fail(nil)
|
||||
|
14
requests.go
14
requests.go
@ -30,6 +30,20 @@ func (c *Client) reqDo(method string, path string, body io.Reader) (*http.Respon
|
||||
return c.c.Do(rq)
|
||||
}
|
||||
|
||||
func (c *Client) MkCol(path string) int {
|
||||
rs, err := c.reqDo("MKCOL", path, nil)
|
||||
if err != nil {
|
||||
return 400
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode == 201 || rs.StatusCode == 405 {
|
||||
return 201
|
||||
}
|
||||
|
||||
return rs.StatusCode
|
||||
}
|
||||
|
||||
func (c *Client) Options(path string) (*http.Response, error) {
|
||||
rq, err := c.req("OPTIONS", path, nil)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user