synchronizer framework tweaks (#438)

This commit is contained in:
Michael Quigley 2024-01-10 13:38:35 -05:00
parent 8313f3f686
commit 16ddb0a1f5
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 36 additions and 26 deletions

View File

@ -7,7 +7,6 @@ import (
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/openziti/zrok/tui"
"github.com/openziti/zrok/util/sync"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
)
@ -22,8 +21,8 @@ type copyCommand struct {
func newCopyCommand() *copyCommand {
cmd := &cobra.Command{
Use: "copy <source> [<target>]",
Short: "Copy zrok drive contents from <source> to <target> ('file://' and 'zrok://' supported)",
Use: "copy <source> [<target>] (<target> defaults to 'file://.`)",
Short: "Copy (unidirectional sync) zrok drive contents from <source> to <target> ('http://', 'file://', and 'zrok://' supported)",
Args: cobra.RangeArgs(1, 2),
}
command := &copyCommand{cmd: cmd}
@ -57,36 +56,29 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
tui.Error("error loading root", err)
}
var srcAccess *sdk.Access
var allocatedAccesses []*sdk.Access
if sourceUrl.Scheme == "zrok" {
srcAccess, err = sdk.CreateAccess(root, &sdk.AccessRequest{ShareToken: sourceUrl.Host})
access, err := sdk.CreateAccess(root, &sdk.AccessRequest{ShareToken: sourceUrl.Host})
if err != nil {
tui.Error("error creating access", err)
}
allocatedAccesses = append(allocatedAccesses, access)
}
if srcAccess != nil {
defer func() {
err := sdk.DeleteAccess(root, srcAccess)
if err != nil {
tui.Error("error deleting source access", err)
}
}()
}
var dstAccess *sdk.Access
if targetUrl.Scheme == "zrok" {
dstAccess, err = sdk.CreateAccess(root, &sdk.AccessRequest{ShareToken: targetUrl.Host})
access, err := sdk.CreateAccess(root, &sdk.AccessRequest{ShareToken: targetUrl.Host})
if err != nil {
tui.Error("error creating access", err)
}
allocatedAccesses = append(allocatedAccesses, access)
}
if dstAccess != nil {
defer func() {
err := sdk.DeleteAccess(root, dstAccess)
defer func() {
for _, access := range allocatedAccesses {
err := sdk.DeleteAccess(root, access)
if err != nil {
tui.Error("error deleting target access", err)
tui.Warning("error deleting target access", err)
}
}()
}
}
}()
source, err := cmd.createTarget(sourceUrl, root)
if err != nil {
@ -107,7 +99,6 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
func (cmd *copyCommand) createTarget(url *url.URL, root env_core.Root) (sync.Target, error) {
switch url.Scheme {
case "file":
logrus.Infof("%v", url)
return sync.NewFilesystemTarget(&sync.FilesystemTargetConfig{Root: url.Path}), nil
case "zrok":

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/openziti/zrok/drives/davServer"
"github.com/sirupsen/logrus"
"io"
"io/fs"
"os"
@ -23,7 +22,6 @@ type FilesystemTarget struct {
}
func NewFilesystemTarget(cfg *FilesystemTargetConfig) *FilesystemTarget {
logrus.Infof("root = %v", cfg.Root)
root := os.DirFS(cfg.Root)
return &FilesystemTarget{cfg: cfg, root: root}
}
@ -38,7 +36,13 @@ func (t *FilesystemTarget) Inventory() ([]*Object, error) {
}
if !fi.IsDir() {
return []*Object{{Path: "/" + t.cfg.Root, Size: fi.Size(), Modified: fi.ModTime()}}, nil
t.cfg.Root = filepath.Dir(t.cfg.Root)
return []*Object{{
Path: "/" + fi.Name(),
IsDir: false,
Size: fi.Size(),
Modified: fi.ModTime(),
}}, nil
}
t.tree = nil

View File

@ -36,6 +36,7 @@ func Synchronize(src, dst Target) error {
}
for _, copyPath := range copyList {
logrus.Infof("copyPath: '%v' (%t)", copyPath.Path, copyPath.IsDir)
if copyPath.IsDir {
if err := dst.Mkdir(copyPath.Path); err != nil {
return err

View File

@ -49,7 +49,21 @@ func NewZrokTarget(cfg *ZrokTargetConfig) (*ZrokTarget, error) {
}
func (t *ZrokTarget) Inventory() ([]*Object, error) {
fis, err := t.dc.Readdir(context.Background(), "/", true)
rootFi, err := t.dc.Stat(context.Background(), t.cfg.URL.Path)
if err != nil {
return nil, err
}
if !rootFi.IsDir {
return []*Object{{
Path: t.cfg.URL.Path,
IsDir: false,
Size: rootFi.Size,
Modified: rootFi.ModTime,
}}, nil
}
fis, err := t.dc.Readdir(context.Background(), t.cfg.URL.Path, true)
if err != nil {
return nil, err
}