framework start; webdav (#438)

This commit is contained in:
Michael Quigley 2023-11-27 15:44:38 -05:00
parent 56ecc330ed
commit 0832f28dc7
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 86 additions and 29 deletions

View File

@ -1,10 +1,9 @@
package main
import (
"github.com/openziti/zrok/util/sync"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/studio-b12/gowebdav"
"path/filepath"
)
func init() {
@ -27,34 +26,19 @@ func newCopyFromCommand() *copyFromCommand {
}
func (cmd *copyFromCommand) run(_ *cobra.Command, args []string) {
c := gowebdav.NewClient(args[0], "", "")
if err := c.Connect(); err != nil {
panic(err)
}
if err := cmd.recurseTree(c, ""); err != nil {
panic(err)
}
}
func (cmd *copyFromCommand) recurseTree(c *gowebdav.Client, path string) error {
files, err := c.ReadDir(path)
t, err := sync.NewWebDAVTarget(&sync.WebDAVTargetConfig{
URL: args[0],
Username: "",
Password: "",
})
if err != nil {
return err
panic(err)
}
for _, f := range files {
sub := filepath.ToSlash(filepath.Join(path, f.Name()))
if f.IsDir() {
logrus.Infof("-> %v", sub)
if err := cmd.recurseTree(c, sub); err != nil {
return err
}
} else {
etag := "<etag>"
if v, ok := f.(gowebdav.File); ok {
etag = v.ETag()
}
logrus.Infof("++ %v (%v)", sub, etag)
}
tree, err := t.Inventory()
if err != nil {
panic(err)
}
for _, f := range tree {
logrus.Infof("-> %v [%v, %v, %v]", f.Path, f.Size, f.Modified, f.ETag)
}
return nil
}

14
util/sync/model.go Normal file
View 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
View 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
}