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