add working filer.export command

This commit is contained in:
Chris Lu 2018-08-23 00:02:04 -07:00
parent 98b8f8649d
commit 6e3f4d1079

View file

@ -28,6 +28,7 @@ var cmdFilerExport = &Command{
var (
// filerExportOutputFile = cmdFilerExport.Flag.String("output", "", "the output file. If empty, only list out the directory tree")
filerExportSourceStore = cmdFilerExport.Flag.String("sourceStore", "", "the source store name in filer.toml")
filerExportTargetStore = cmdFilerExport.Flag.String("targetStore", "", "the target store name in filer.toml")
)
type statistics struct {
@ -40,7 +41,7 @@ func runFilerExport(cmd *Command, args []string) bool {
weed_server.LoadConfiguration("filer", true)
config := viper.GetViper()
var sourceStore filer2.FilerStore
var sourceStore, targetStore filer2.FilerStore
for _, store := range filer2.Stores {
if store.GetName() == *filerExportSourceStore {
@ -55,6 +56,19 @@ func runFilerExport(cmd *Command, args []string) bool {
}
}
for _, store := range filer2.Stores {
if store.GetName() == *filerExportTargetStore {
viperSub := config.Sub(store.GetName())
if err := store.Initialize(viperSub); err != nil {
glog.Fatalf("Failed to initialize store for %s: %+v",
store.GetName(), err)
} else {
targetStore = store
}
break
}
}
if sourceStore == nil {
glog.Errorf("Failed to find source store %s", *filerExportSourceStore)
println("existing data sources are:")
@ -65,7 +79,18 @@ func runFilerExport(cmd *Command, args []string) bool {
}
stat := statistics{}
doTraverse(&stat, sourceStore, filer2.FullPath("/"), 0, printout)
var fn func(level int, entry *filer2.Entry) error
if targetStore == nil {
fn = printout
} else {
fn = func(level int, entry *filer2.Entry) error {
return targetStore.InsertEntry(entry)
}
}
doTraverse(&stat, sourceStore, filer2.FullPath("/"), 0, fn)
glog.Infof("processed %d directories, %d files", stat.directoryCount, stat.fileCount)