mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Added restoration routes
Former-commit-id: 822e9b62b8c72965c0733f8d2b035291ab4d8613 [formerly cf916cceb9a12def41c854a0b1430ffbf25ef4ca] [formerly f2bf6102389e65d58b6cb6b7c57516d794bb5658 [formerly 9cd15b3ddc279a286d8a0b4e735690f0e304a894 [formerly796e92eed1
]]] Former-commit-id: 94e2f000d63311cc44d8300f0c931edc01b5ed43 [formerly 71b130845411a9462beee94bcae12ff890d3be96] Former-commit-id: f99a1f31fa2153a8d0e2c09e8a96562bce107544 Former-commit-id:c89f019084
This commit is contained in:
parent
0dacd669c8
commit
1a8d796e98
32
main.go
32
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
|
||||
}
|
||||
|
@ -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
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user