diff --git a/cmd/zrok/copyFrom.go b/cmd/zrok/copyFrom.go index c211a2c3..8b4e78b3 100644 --- a/cmd/zrok/copyFrom.go +++ b/cmd/zrok/copyFrom.go @@ -2,7 +2,6 @@ package main import ( "github.com/openziti/zrok/util/sync" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -31,18 +30,10 @@ func (cmd *copyFromCommand) run(_ *cobra.Command, args []string) { target = args[1] } - t := sync.NewFilesystemTarget(&sync.FilesystemTargetConfig{ + dst := sync.NewFilesystemTarget(&sync.FilesystemTargetConfig{ Root: target, }) - destTree, err := t.Inventory() - if err != nil { - panic(err) - } - for _, f := range destTree { - logrus.Infof("<- %v [%v, %v, %v]", f.Path, f.Size, f.Modified, f.ETag) - } - - s, err := sync.NewWebDAVTarget(&sync.WebDAVTargetConfig{ + src, err := sync.NewWebDAVTarget(&sync.WebDAVTargetConfig{ URL: args[0], Username: "", Password: "", @@ -50,11 +41,8 @@ func (cmd *copyFromCommand) run(_ *cobra.Command, args []string) { if err != nil { panic(err) } - srcTree, err := s.Inventory() - if err != nil { + + if err := sync.Synchronize(src, dst); err != nil { panic(err) } - for _, f := range srcTree { - logrus.Infof("-> %v [%v, %v, %v]", f.Path, f.Size, f.Modified, f.ETag) - } } diff --git a/util/sync/synchronizer.go b/util/sync/synchronizer.go new file mode 100644 index 00000000..ec168b4d --- /dev/null +++ b/util/sync/synchronizer.go @@ -0,0 +1,41 @@ +package sync + +import ( + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +func Synchronize(src, dst Target) error { + srcTree, err := src.Inventory() + if err != nil { + return errors.Wrap(err, "error creating source inventory") + } + + dstTree, err := dst.Inventory() + if err != nil { + return errors.Wrap(err, "error creating destination inventory") + } + + dstIndex := make(map[string]*Object) + for _, f := range dstTree { + dstIndex[f.Path] = f + } + + var copyList []*Object + for _, srcF := range srcTree { + if dstF, found := dstIndex[srcF.Path]; found { + if dstF.ETag != srcF.ETag { + copyList = append(copyList, srcF) + } + } else { + copyList = append(copyList, srcF) + } + } + + logrus.Infof("files to copy:") + for _, copy := range copyList { + logrus.Infof("-> %v", copy.Path) + } + + return nil +}