'zrok copy' now supports copy and oneway sync (#438)

This commit is contained in:
Michael Quigley 2024-01-11 11:36:05 -05:00
parent 3b35250024
commit e40df8c101
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 27 additions and 6 deletions

View File

@ -15,7 +15,8 @@ func init() {
} }
type copyCommand struct { type copyCommand struct {
cmd *cobra.Command cmd *cobra.Command
sync bool
} }
func newCopyCommand() *copyCommand { func newCopyCommand() *copyCommand {
@ -27,6 +28,7 @@ func newCopyCommand() *copyCommand {
} }
command := &copyCommand{cmd: cmd} command := &copyCommand{cmd: cmd}
cmd.Run = command.run cmd.Run = command.run
cmd.Flags().BoolVarP(&command.sync, "sync", "s", false, "Only copy modified files (one-way synchronize)")
return command return command
} }
@ -89,7 +91,7 @@ func (cmd *copyCommand) run(_ *cobra.Command, args []string) {
tui.Error("error creating target", err) tui.Error("error creating target", err)
} }
if err := sync.Synchronize(source, target); err != nil { if err := sync.OneWay(source, target, cmd.sync); err != nil {
tui.Error("error copying", err) tui.Error("error copying", err)
} }

View File

@ -6,15 +6,18 @@ import (
"os" "os"
) )
func Synchronize(src, dst Target) error { func OneWay(src, dst Target, sync bool) error {
srcTree, err := src.Inventory() srcTree, err := src.Inventory()
if err != nil { if err != nil {
return errors.Wrap(err, "error creating source inventory") return errors.Wrap(err, "error creating source inventory")
} }
dstTree, err := dst.Inventory() var dstTree []*Object
if err != nil { if sync {
return errors.Wrap(err, "error creating destination inventory") dstTree, err = dst.Inventory()
if err != nil {
return errors.Wrap(err, "error creating destination inventory")
}
} }
dstIndex := make(map[string]*Object) dstIndex := make(map[string]*Object)

View File

@ -3,6 +3,7 @@ package sync
import ( import (
"context" "context"
"github.com/openziti/zrok/drives/davClient" "github.com/openziti/zrok/drives/davClient"
"github.com/pkg/errors"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -85,6 +86,13 @@ func (t *WebDAVTarget) Dir(path string) ([]*Object, error) {
} }
func (t *WebDAVTarget) Mkdir(path string) error { func (t *WebDAVTarget) Mkdir(path string) error {
fi, err := t.dc.Stat(context.Background(), filepath.Join(t.cfg.URL.Path, path))
if err == nil {
if fi.IsDir {
return nil
}
return errors.Errorf("'%v' already exists; not directory", path)
}
return t.dc.Mkdir(context.Background(), filepath.Join(t.cfg.URL.Path, path)) return t.dc.Mkdir(context.Background(), filepath.Join(t.cfg.URL.Path, path))
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/openziti/zrok/drives/davClient" "github.com/openziti/zrok/drives/davClient"
"github.com/openziti/zrok/environment/env_core" "github.com/openziti/zrok/environment/env_core"
"github.com/openziti/zrok/sdk/golang/sdk" "github.com/openziti/zrok/sdk/golang/sdk"
"github.com/pkg/errors"
"io" "io"
"net" "net"
"net/http" "net/http"
@ -104,6 +105,13 @@ func (t *ZrokTarget) Dir(path string) ([]*Object, error) {
} }
func (t *ZrokTarget) Mkdir(path string) error { func (t *ZrokTarget) Mkdir(path string) error {
fi, err := t.dc.Stat(context.Background(), filepath.Join(t.cfg.URL.Path, path))
if err == nil {
if fi.IsDir {
return nil
}
return errors.Errorf("'%v' already exists; not directory", path)
}
return t.dc.Mkdir(context.Background(), filepath.Join(t.cfg.URL.Path, path)) return t.dc.Mkdir(context.Background(), filepath.Join(t.cfg.URL.Path, path))
} }