mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 17:58:50 +02:00
framework start; webdav (#438)
This commit is contained in:
parent
56ecc330ed
commit
0832f28dc7
@ -1,10 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/openziti/zrok/util/sync"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/studio-b12/gowebdav"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -27,34 +26,19 @@ func newCopyFromCommand() *copyFromCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *copyFromCommand) run(_ *cobra.Command, args []string) {
|
func (cmd *copyFromCommand) run(_ *cobra.Command, args []string) {
|
||||||
c := gowebdav.NewClient(args[0], "", "")
|
t, err := sync.NewWebDAVTarget(&sync.WebDAVTargetConfig{
|
||||||
if err := c.Connect(); err != nil {
|
URL: args[0],
|
||||||
panic(err)
|
Username: "",
|
||||||
}
|
Password: "",
|
||||||
if err := cmd.recurseTree(c, ""); err != nil {
|
})
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cmd *copyFromCommand) recurseTree(c *gowebdav.Client, path string) error {
|
|
||||||
files, err := c.ReadDir(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
panic(err)
|
||||||
}
|
}
|
||||||
for _, f := range files {
|
tree, err := t.Inventory()
|
||||||
sub := filepath.ToSlash(filepath.Join(path, f.Name()))
|
if err != nil {
|
||||||
if f.IsDir() {
|
panic(err)
|
||||||
logrus.Infof("-> %v", sub)
|
}
|
||||||
if err := cmd.recurseTree(c, sub); err != nil {
|
for _, f := range tree {
|
||||||
return err
|
logrus.Infof("-> %v [%v, %v, %v]", f.Path, f.Size, f.Modified, f.ETag)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
etag := "<etag>"
|
|
||||||
if v, ok := f.(gowebdav.File); ok {
|
|
||||||
etag = v.ETag()
|
|
||||||
}
|
|
||||||
logrus.Infof("++ %v (%v)", sub, etag)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
14
util/sync/model.go
Normal file
14
util/sync/model.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package sync
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Object struct {
|
||||||
|
Path string
|
||||||
|
Size int64
|
||||||
|
Modified time.Time
|
||||||
|
ETag string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Target interface {
|
||||||
|
Inventory() ([]*Object, error)
|
||||||
|
}
|
59
util/sync/webdav.go
Normal file
59
util/sync/webdav.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package sync
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/studio-b12/gowebdav"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WebDAVTargetConfig struct {
|
||||||
|
URL string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
type WebDAVTarget struct {
|
||||||
|
c *gowebdav.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWebDAVTarget(cfg *WebDAVTargetConfig) (*WebDAVTarget, error) {
|
||||||
|
c := gowebdav.NewClient(cfg.URL, cfg.Username, cfg.Password)
|
||||||
|
if err := c.Connect(); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error connecting to webdav target")
|
||||||
|
}
|
||||||
|
return &WebDAVTarget{c: c}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *WebDAVTarget) Inventory() ([]*Object, error) {
|
||||||
|
tree, err := t.recurse("", nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tree, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *WebDAVTarget) recurse(path string, tree []*Object) ([]*Object, error) {
|
||||||
|
files, err := t.c.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, f := range files {
|
||||||
|
sub := filepath.ToSlash(filepath.Join(path, f.Name()))
|
||||||
|
if f.IsDir() {
|
||||||
|
tree, err = t.recurse(sub, tree)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if v, ok := f.(gowebdav.File); ok {
|
||||||
|
tree = append(tree, &Object{
|
||||||
|
Path: filepath.ToSlash(filepath.Join(path, f.Name())),
|
||||||
|
Size: v.Size(),
|
||||||
|
Modified: v.ModTime(),
|
||||||
|
ETag: v.ETag(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tree, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user