mirror of
https://github.com/openziti/zrok.git
synced 2024-11-29 19:43:26 +01:00
very rudimentary sync (#438)
This commit is contained in:
parent
e6aa1420be
commit
93277a191b
@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"golang.org/x/net/webdav"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FilesystemTargetConfig struct {
|
||||
@ -13,16 +15,20 @@ type FilesystemTargetConfig struct {
|
||||
}
|
||||
|
||||
type FilesystemTarget struct {
|
||||
cfg *FilesystemTargetConfig
|
||||
root fs.FS
|
||||
tree []*Object
|
||||
}
|
||||
|
||||
func NewFilesystemTarget(cfg *FilesystemTargetConfig) *FilesystemTarget {
|
||||
root := os.DirFS(cfg.Root)
|
||||
return &FilesystemTarget{root: root}
|
||||
return &FilesystemTarget{cfg: cfg, root: root}
|
||||
}
|
||||
|
||||
func (t *FilesystemTarget) Inventory() ([]*Object, error) {
|
||||
if _, err := os.Stat(t.cfg.Root); os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
t.tree = nil
|
||||
if err := fs.WalkDir(t.root, ".", t.recurse); err != nil {
|
||||
return nil, err
|
||||
@ -52,3 +58,24 @@ func (t *FilesystemTarget) recurse(path string, d fs.DirEntry, err error) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *FilesystemTarget) ReadStream(path string) (io.ReadCloser, error) {
|
||||
return os.Open(path)
|
||||
}
|
||||
|
||||
func (t *FilesystemTarget) WriteStream(path string, stream io.Reader, mode os.FileMode) error {
|
||||
targetPath := filepath.Join(t.cfg.Root, path)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(targetPath), mode); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.Create(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(f, stream)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
package sync
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Object struct {
|
||||
Path string
|
||||
@ -11,4 +15,6 @@ type Object struct {
|
||||
|
||||
type Target interface {
|
||||
Inventory() ([]*Object, error)
|
||||
ReadStream(path string) (io.ReadCloser, error)
|
||||
WriteStream(path string, stream io.Reader, mode os.FileMode) error
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package sync
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Synchronize(src, dst Target) error {
|
||||
@ -32,9 +33,16 @@ func Synchronize(src, dst Target) error {
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Infof("files to copy:")
|
||||
for _, copy := range copyList {
|
||||
logrus.Infof("-> %v", copy.Path)
|
||||
for _, target := range copyList {
|
||||
logrus.Infof("+> %v", target.Path)
|
||||
ss, err := src.ReadStream(target.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := dst.WriteStream(target.Path, ss, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("=> %v", target.Path)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -3,6 +3,8 @@ package sync
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/studio-b12/gowebdav"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
@ -57,3 +59,11 @@ func (t *WebDAVTarget) recurse(path string, tree []*Object) ([]*Object, error) {
|
||||
}
|
||||
return tree, nil
|
||||
}
|
||||
|
||||
func (t *WebDAVTarget) ReadStream(path string) (io.ReadCloser, error) {
|
||||
return t.c.ReadStream(path)
|
||||
}
|
||||
|
||||
func (t *WebDAVTarget) WriteStream(path string, stream io.Reader, mode os.FileMode) error {
|
||||
return t.c.WriteStream(path, stream, mode)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user