very rudimentary sync (#438)

This commit is contained in:
Michael Quigley 2023-11-27 17:31:13 -05:00
parent e6aa1420be
commit 93277a191b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 56 additions and 5 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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)
}