From c89f019084d64e8e67832c9ca484573035080338 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 29 Mar 2016 17:00:58 -0400 Subject: [PATCH] Added restoration routes Former-commit-id: 822e9b62b8c72965c0733f8d2b035291ab4d8613 [formerly cf916cceb9a12def41c854a0b1430ffbf25ef4ca] [formerly f2bf6102389e65d58b6cb6b7c57516d794bb5658 [formerly 796e92eed127c821468a6fe8fbcd65fa50648877]] Former-commit-id: 94e2f000d63311cc44d8300f0c931edc01b5ed43 [formerly 71b130845411a9462beee94bcae12ff890d3be96] Former-commit-id: f99a1f31fa2153a8d0e2c09e8a96562bce107544 --- 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 })