Skip to content

Commit

Permalink
framework start; webdav (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Nov 27, 2023
1 parent 56ecc33 commit 0832f28
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
38 changes: 11 additions & 27 deletions cmd/zrok/copyFrom.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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 {
t, err := sync.NewWebDAVTarget(&sync.WebDAVTargetConfig{
URL: args[0],
Username: "",
Password: "",
})
if err != nil {
panic(err)
}
}

func (cmd *copyFromCommand) recurseTree(c *gowebdav.Client, path string) error {
files, err := c.ReadDir(path)
tree, err := t.Inventory()
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)
}
for _, f := range tree {
logrus.Infof("-> %v [%v, %v, %v]", f.Path, f.Size, f.Modified, f.ETag)
}
return nil
}
14 changes: 14 additions & 0 deletions util/sync/model.go
Original file line number Diff line number Diff line change
@@ -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 changes: 59 additions & 0 deletions util/sync/webdav.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0832f28

Please sign in to comment.