implement Remove

This commit is contained in:
Christoph Polcin 2014-10-23 14:10:31 +02:00
parent a5b6eb39ff
commit d4b8ebb4b2
4 changed files with 38 additions and 1 deletions

View File

@ -117,6 +117,20 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
return files, err return files, err
} }
func (c *Client) Remove(path string) error {
rs, err := c.reqDo("DELETE", path, nil)
if err != nil {
return err
}
defer rs.Body.Close()
if rs.StatusCode == 200 {
return nil
} else {
return Error(rs)
}
}
func (c *Client) Read(path string) { func (c *Client) Read(path string) {
fmt.Println("Read " + path) fmt.Println("Read " + path)
} }

View File

@ -22,7 +22,7 @@ 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: LIST aka PROPFIND, GET") m := flag.String("X", "GET", "Method: LIST aka PROPFIND, GET, DELETE")
flag.Parse() flag.Parse()
if *root == "URL" { if *root == "URL" {
@ -46,7 +46,14 @@ func main() {
} else { } else {
fmt.Println(err) fmt.Println(err)
} }
case "GET": c.Read(path) case "GET": c.Read(path)
case "DELETE", "RM":
if err := c.Remove(path); err != nil {
fmt.Println(err)
}
default: Fail(nil) default: Fail(nil)
} }
} else { } else {

View File

@ -21,6 +21,15 @@ func (c *Client) req(method string, path string, body io.Reader) (req *http.Requ
return req, nil return req, nil
} }
func (c *Client) reqDo(method string, path string, body io.Reader) (*http.Response, error) {
rq, err := c.req(method, path, body)
if err != nil {
return nil, err
}
return c.c.Do(rq)
}
func (c *Client) Options(path string) (*http.Response, error) { func (c *Client) Options(path string) (*http.Response, error) {
rq, err := c.req("OPTIONS", path, nil) rq, err := c.req("OPTIONS", path, nil)
if err != nil { if err != nil {

View File

@ -3,12 +3,19 @@ package gowebdav
import ( import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"errors"
"fmt"
"io" "io"
"net/http"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
func Error(r *http.Response) error {
return errors.New(fmt.Sprintf("%s - %s %s", r.Status, r.Request.Method, r.Request.URL.String()))
}
func FixSlash(s string) string { func FixSlash(s string) string {
if !strings.HasSuffix(s, "/") { if !strings.HasSuffix(s, "/") {
s += "/" s += "/"