files to copy (#438)

This commit is contained in:
Michael Quigley 2023-11-27 16:53:02 -05:00
parent 57c5a9977e
commit e6aa1420be
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 45 additions and 16 deletions

View File

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

41
util/sync/synchronizer.go Normal file
View File

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