From 796e92eed127c821468a6fe8fbcd65fa50648877 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 29 Mar 2016 17:00:58 -0400 Subject: [PATCH] Added restoration routes --- main.go | 32 ++++++++++++++++++++++++++++---- routes.go | 6 +++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 31fcd09..11dfb08 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,14 @@ package main import ( + "bytes" "flag" "fmt" + "io" "io/ioutil" "os" "path" + "path/filepath" "github.com/boltdb/bolt" "github.com/gin-gonic/gin" @@ -32,6 +35,8 @@ var RuntimeArgs struct { AdminKey string Socket string ForceWss bool + DumpDataset string + RestoreDataset string } var VersionNum string @@ -51,7 +56,8 @@ func main() { flag.StringVar(&RuntimeArgs.ServerKey, "key", "", "location of SSL key") flag.StringVar(&RuntimeArgs.WikiName, "w", "cowyo", "custom name for wiki") flag.BoolVar(&RuntimeArgs.ForceWss, "e", false, "force encrypted sockets (use if using Caddy auto HTTPS)") - dumpDataset := flag.Bool("dump", false, "flag to dump all data to 'dump' directory") + flag.StringVar(&RuntimeArgs.DumpDataset, "dump", "", "directory to dump all data to") + flag.StringVar(&RuntimeArgs.RestoreDataset, "restore", "", "directory to restore all data from") flag.CommandLine.Usage = func() { fmt.Println(`cowyo (version ` + VersionNum + `) @@ -70,9 +76,9 @@ Options:`) } flag.Parse() - if *dumpDataset { - fmt.Println("Dumping data to 'dump' folder...") - dumpEverything() + if len(RuntimeArgs.DumpDataset) > 0 { + fmt.Println("Dumping data to '" + RuntimeArgs.DumpDataset + "' folder...") + dumpEverything(RuntimeArgs.DumpDataset) os.Exit(1) } @@ -105,6 +111,11 @@ Options:`) p := WikiData{"help", "", []string{}, []string{}, false, "zzz"} p.save(string(aboutFile)) + if len(RuntimeArgs.RestoreDataset) > 0 { + fmt.Println("Restoring data from '" + RuntimeArgs.RestoreDataset + "' folder...") + filepath.Walk(RuntimeArgs.RestoreDataset, restoreFile) + os.Exit(1) + } // var q WikiData // q.load("about") // fmt.Println(getImportantVersions(q)) @@ -139,3 +150,16 @@ Options:`) r.Run(RuntimeArgs.Port) } } + +func restoreFile(path string, f os.FileInfo, err error) error { + fName := filepath.Base(path) + buf := bytes.NewBuffer(nil) + fOpen, _ := os.Open(path) // Error handling elided for brevity. + io.Copy(buf, fOpen) // Error handling elided for brevity. + fOpen.Close() + s := string(buf.Bytes()) + fmt.Println(fName) + p := WikiData{fName, "", []string{}, []string{}, false, ""} + p.save(s) + return nil +} diff --git a/routes.go b/routes.go index 4ae02bc..3ee74e0 100644 --- a/routes.go +++ b/routes.go @@ -578,10 +578,10 @@ func listEverything() string { return everything } -func dumpEverything() { +func dumpEverything(folderpath string) { Open(RuntimeArgs.DatabaseLocation) defer Close() - err := os.MkdirAll("dump", 0777) + err := os.MkdirAll(folderpath, 0777) if err != nil { fmt.Println("Already exists") } @@ -593,7 +593,7 @@ func dumpEverything() { var p WikiData p.load(string(k)) fmt.Println(string(k), len(p.CurrentText)) - ioutil.WriteFile(path.Join("dump", string(k)+".md"), []byte(p.CurrentText), 0644) + ioutil.WriteFile(path.Join(folderpath, string(k)), []byte(p.CurrentText), 0644) } return nil })