diff --git a/README.md b/README.md index 7ee9d4d..2af9146 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,27 @@ -# wf-import +# wf-migrate -[![GoDoc](https://godoc.org/github.com/writeas/wf-import?status.svg)](https://godoc.org/github.com/writeas/wf-import) +[![GoDoc](https://godoc.org/github.com/writeas/wf-migrate?status.svg)](https://godoc.org/github.com/writeas/wf-migrate) -wf-import provides helper functions and a command-line utility for migrating posts between [WriteFreely](https://writefreely.org) instances. +wf-migrate provides helper functions and a command-line utility for migrating posts between [WriteFreely](https://writefreely.org) instances. ## Command-line Install the command-line utility with: ``` -go get github.com/writeas/wf-import/cmd/wfimport +go get github.com/writeas/wf-migrate/cmd/wfimport ``` `wfimport` takes a username `-u`, optional WriteFreely instance hostname `-h`, and the filename of the JSON data you want to import. By default, `wfimport` publishes posts to Write.as: ``` wfimport -u username exported-data.json ``` But you can also supply another WriteFreely instance to import to: ``` wfimport -u username -h pencil.writefree.ly exported-data.json ``` diff --git a/cmd/wfimport/main.go b/cmd/wfimport/main.go index 0753423..c0eaa1a 100644 --- a/cmd/wfimport/main.go +++ b/cmd/wfimport/main.go @@ -1,135 +1,135 @@ package main import ( "encoding/json" "flag" "fmt" "github.com/howeyc/gopass" "github.com/writeas/go-writeas" - "github.com/writeas/wf-import" + "github.com/writeas/wf-migrate" "io/ioutil" "os" ) func main() { // Get parameters u := flag.String("u", "", "WriteFreely username") host := flag.String("h", "write.as", "WriteFreely host") flag.Parse() // Validate parameters args := flag.Args() if *u == "" || len(args) == 0 { fmt.Fprintf(os.Stderr, "usage: wfimport -u username [-h example.com] file1\n") os.Exit(1) } fn := args[0] // Get password fmt.Print("Password: ") pass, err := gopass.GetPasswdMasked() if err != nil { fmt.Fprintf(os.Stderr, "error reading pass: %v\n", err) os.Exit(1) } // Validate password if len(pass) == 0 { fmt.Fprintf(os.Stderr, "Please enter your password.\n") os.Exit(1) } // Create Write.as client cl := writeas.NewClientWith(writeas.Config{ URL: "https://" + *host + "/api", }) // Log user in fmt.Printf("Logging in to %s...", *host) _, err = cl.LogIn(*u, string(pass)) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } fmt.Print("OK\n") defer func() { fmt.Print("Logging out...") cl.LogOut() fmt.Print("OK\n") }() // Read file contents // TODO: validate fmt.Print("Reading file...") content, err := ioutil.ReadFile(fn) if err != nil { fmt.Fprintf(os.Stderr, "error %s: %v\n", fn, err) os.Exit(1) } fmt.Print("OK\n") - imp := wfimport.Import{} + imp := wfmigrate.Import{} fmt.Print("Parsing file...") err = json.Unmarshal(content, &imp) if err != nil { fmt.Fprintf(os.Stderr, "error %s: %v\n", fn, err) os.Exit(1) } fmt.Print("OK\n") fmtln("Read user %s export.", imp.User.Username) fmtln("Found %d collection(s).", len(imp.Collections)) fmtln("Found %d draft post(s).", len(imp.Posts)) // Create collections and their posts for _, coll := range imp.Collections { fmt.Printf("%s has %d post(s). ", coll.Alias, len(*coll.Posts)) if len(*coll.Posts) == 0 { fmt.Print("Skipping.\n") continue } fmt.Print("\n") fmt.Printf("Creating collection %s...", coll.Alias) _, err = cl.CreateCollection(&writeas.CollectionParams{ Alias: coll.Alias, Title: coll.Title, Description: coll.Description, // TODO: //Stylesheet: coll.Stylesheet, //Public: coll.Public, }) if err != nil { // TODO: handle alias collisions // TODO: handle hitting collection allowance limit fmt.Fprintf(os.Stderr, "error: %v\n", err) continue } fmt.Print("OK\n") // Create posts for _, p := range *coll.Posts { fmt.Printf("Creating post %s...", p.Slug) - _, err = wfimport.CreatePost(cl, p, coll.Alias) + _, err = wfmigrate.CreatePost(cl, p, coll.Alias) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) continue } fmt.Print("OK\n") } } // Create anonymous / draft posts for _, p := range imp.Posts { fmt.Printf("Creating draft post from %s...", p.ID) - _, err = wfimport.CreatePost(cl, p, "") + _, err = wfmigrate.CreatePost(cl, p, "") if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) continue } fmt.Print("OK\n") } } func fmtln(s string, v ...interface{}) { fmt.Printf(s+"\n", v...) } diff --git a/import.go b/import.go index c01b4b4..eb7bc33 100644 --- a/import.go +++ b/import.go @@ -1,24 +1,24 @@ -package wfimport +package wfmigrate import "github.com/writeas/go-writeas" type Import struct { writeas.User Collections []writeas.Collection `json:"collections"` Posts []writeas.Post `json:"posts"` } // CreatePost publishes a post from the given writeas.Post. func CreatePost(cl *writeas.Client, p writeas.Post, collAlias string) (*writeas.Post, error) { return cl.CreatePost(&writeas.PostParams{ Slug: p.Slug, Title: p.Title, Content: p.Content, Font: p.Font, Language: p.Language, IsRTL: p.RTL, Created: &p.Created, Updated: &p.Updated, Collection: collAlias, }) }