mirror of
https://github.com/openziti/zrok.git
synced 2025-01-24 23:09:32 +01:00
synchronizer framework tweaks (#438)
This commit is contained in:
parent
8313f3f686
commit
16ddb0a1f5
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/openziti/zrok/sdk/golang/sdk"
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
||||||
"github.com/openziti/zrok/tui"
|
"github.com/openziti/zrok/tui"
|
||||||
"github.com/openziti/zrok/util/sync"
|
"github.com/openziti/zrok/util/sync"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
@ -22,8 +21,8 @@ type copyCommand struct {
|
|||||||
|
|
||||||
func newCopyCommand() *copyCommand {
|
func newCopyCommand() *copyCommand {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "copy <source> [<target>]",
|
Use: "copy <source> [<target>] (<target> defaults to 'file://.`)",
|
||||||
Short: "Copy zrok drive contents from <source> to <target> ('file://' and 'zrok://' supported)",
|
Short: "Copy (unidirectional sync) zrok drive contents from <source> to <target> ('http://', 'file://', and 'zrok://' supported)",
|
||||||
Args: cobra.RangeArgs(1, 2),
|
Args: cobra.RangeArgs(1, 2),
|
||||||
}
|
}
|
||||||
command := ©Command{cmd: cmd}
|
command := ©Command{cmd: cmd}
|
||||||
@ -57,36 +56,29 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
|
|||||||
tui.Error("error loading root", err)
|
tui.Error("error loading root", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var srcAccess *sdk.Access
|
var allocatedAccesses []*sdk.Access
|
||||||
if sourceUrl.Scheme == "zrok" {
|
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 {
|
if err != nil {
|
||||||
tui.Error("error creating access", err)
|
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" {
|
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 {
|
if err != nil {
|
||||||
tui.Error("error creating access", err)
|
tui.Error("error creating access", err)
|
||||||
}
|
}
|
||||||
|
allocatedAccesses = append(allocatedAccesses, access)
|
||||||
}
|
}
|
||||||
if dstAccess != nil {
|
|
||||||
defer func() {
|
defer func() {
|
||||||
err := sdk.DeleteAccess(root, dstAccess)
|
for _, access := range allocatedAccesses {
|
||||||
|
err := sdk.DeleteAccess(root, access)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tui.Error("error deleting target access", err)
|
tui.Warning("error deleting target access", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
|
||||||
|
|
||||||
source, err := cmd.createTarget(sourceUrl, root)
|
source, err := cmd.createTarget(sourceUrl, root)
|
||||||
if err != nil {
|
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) {
|
func (cmd *copyCommand) createTarget(url *url.URL, root env_core.Root) (sync.Target, error) {
|
||||||
switch url.Scheme {
|
switch url.Scheme {
|
||||||
case "file":
|
case "file":
|
||||||
logrus.Infof("%v", url)
|
|
||||||
return sync.NewFilesystemTarget(&sync.FilesystemTargetConfig{Root: url.Path}), nil
|
return sync.NewFilesystemTarget(&sync.FilesystemTargetConfig{Root: url.Path}), nil
|
||||||
|
|
||||||
case "zrok":
|
case "zrok":
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/openziti/zrok/drives/davServer"
|
"github.com/openziti/zrok/drives/davServer"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
@ -23,7 +22,6 @@ type FilesystemTarget struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewFilesystemTarget(cfg *FilesystemTargetConfig) *FilesystemTarget {
|
func NewFilesystemTarget(cfg *FilesystemTargetConfig) *FilesystemTarget {
|
||||||
logrus.Infof("root = %v", cfg.Root)
|
|
||||||
root := os.DirFS(cfg.Root)
|
root := os.DirFS(cfg.Root)
|
||||||
return &FilesystemTarget{cfg: cfg, root: root}
|
return &FilesystemTarget{cfg: cfg, root: root}
|
||||||
}
|
}
|
||||||
@ -38,7 +36,13 @@ func (t *FilesystemTarget) Inventory() ([]*Object, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !fi.IsDir() {
|
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
|
t.tree = nil
|
||||||
|
@ -36,6 +36,7 @@ func Synchronize(src, dst Target) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, copyPath := range copyList {
|
for _, copyPath := range copyList {
|
||||||
|
logrus.Infof("copyPath: '%v' (%t)", copyPath.Path, copyPath.IsDir)
|
||||||
if copyPath.IsDir {
|
if copyPath.IsDir {
|
||||||
if err := dst.Mkdir(copyPath.Path); err != nil {
|
if err := dst.Mkdir(copyPath.Path); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -49,7 +49,21 @@ func NewZrokTarget(cfg *ZrokTargetConfig) (*ZrokTarget, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *ZrokTarget) Inventory() ([]*Object, 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user