Merge pull request #19 from dnote-io/sync

Migrate Dnote to JSON + Add sync
This commit is contained in:
Sung Won Cho 2017-04-16 12:22:46 +10:00 committed by GitHub
commit 0433d02af8
4 changed files with 74 additions and 115 deletions

27
cmd/login/login.go Normal file
View file

@ -0,0 +1,27 @@
package login
import (
"fmt"
"github.com/dnote-io/cli/utils"
)
func Run() error {
fmt.Print("Please enter your APIKey: ")
var apiKey string
fmt.Scanln(&apiKey)
config, err := utils.ReadConfig()
if err != nil {
return err
}
config.APIKey = apiKey
err = utils.WriteConfig(config)
if err != nil {
return err
}
return nil
}

View file

@ -4,12 +4,13 @@ import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"net/http"
"github.com/dnote-io/cli/utils"
)
func getRequestPayload() (*bytes.Buffer, error) {
func compressDnote() (*bytes.Buffer, error) {
b, err := utils.ReadNoteContent()
if err != nil {
return nil, err
@ -27,27 +28,52 @@ func getRequestPayload() (*bytes.Buffer, error) {
}
return &buf, nil
}
func Sync() error {
config, err := utils.ReadConfig()
if err != nil {
return err
}
if config.APIKey == "" {
fmt.Println("Login required. Please run `dnote login`")
return nil
}
fmt.Println("Compressing dnote...")
payload, err := getRequestPayload()
payload, err := compressDnote()
if err != nil {
return err
}
fmt.Println("Syncing...")
req, err := http.NewRequest("POST", "http://127.0.0.1:3030/sync", payload)
endpoint := "http://api.dnote.io/sync"
//endpoint := "http://127.0.0.1:3030/sync"
req, err := http.NewRequest("POST", endpoint, payload)
if err != nil {
return err
}
req.Header.Set("Authorization", config.APIKey)
client := http.Client{}
_, err = client.Do(req)
resp, err := client.Do(req)
if err != nil {
return err
}
fmt.Println("Successfully synced all notes")
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
bodyStr := string(body)
fmt.Printf("Failed to sync: %s", bodyStr)
} else {
fmt.Println("Successfully synced all notes")
}
return nil
}

123
main.go
View file

@ -2,23 +2,17 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"github.com/dnote-io/cli/cmd/books"
"github.com/dnote-io/cli/cmd/login"
"github.com/dnote-io/cli/cmd/new"
"github.com/dnote-io/cli/cmd/notes"
"github.com/dnote-io/cli/cmd/sync"
"github.com/dnote-io/cli/upgrade"
"github.com/dnote-io/cli/utils"
"gopkg.in/yaml.v2"
)
type Config struct {
Book string
}
func initDnote() error {
configPath, err := utils.GetConfigPath()
if err != nil {
@ -67,128 +61,36 @@ func check(e error) {
}
}
func readConfig() (Config, error) {
var ret Config
configPath, err := utils.GetConfigPath()
if err != nil {
return ret, err
}
b, err := ioutil.ReadFile(configPath)
if err != nil {
return ret, err
}
err = yaml.Unmarshal(b, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func getCurrentBook() (string, error) {
config, err := readConfig()
if err != nil {
return "", err
}
return config.Book, nil
}
func writeConfig(config Config) error {
d, err := yaml.Marshal(config)
if err != nil {
return err
}
configPath, err := utils.GetConfigPath()
if err != nil {
return err
}
err = ioutil.WriteFile(configPath, d, 0644)
if err != nil {
return err
}
return nil
}
// changeBook replaces the book name in the dnote config file
func changeBook(bookName string) error {
config, err := readConfig()
config, err := utils.ReadConfig()
if err != nil {
return err
}
config.Book = bookName
err = writeConfig(config)
err = utils.WriteConfig(config)
if err != nil {
return err
}
fmt.Printf("Using: %s\n", bookName)
fmt.Printf("Now using %s\n", bookName)
return nil
}
func getBooks() ([]string, error) {
note, err := utils.GetNote()
if err != nil {
return nil, err
}
books := make([]string, 0, len(note))
for k := range note {
books = append(books, k)
}
sort.Strings(books)
return books, nil
}
func getNotesInBook(bookName string) ([]string, error) {
note, err := utils.GetNote()
if err != nil {
return nil, err
}
notes := make([]string, 0, len(note))
for k, v := range note {
if k == bookName {
for _, noteContent := range v {
notes = append(notes, noteContent)
}
}
}
sort.Strings(notes)
return notes, nil
}
func getNotesInCurrentBook() ([]string, error) {
currentBook, err := getCurrentBook()
if err != nil {
return nil, err
}
return getNotesInBook(currentBook)
}
func checkFileExists(filepath string) bool {
_, err := os.Stat(filepath)
return !os.IsNotExist(err)
}
func main() {
func init() {
err := initDnote()
check(err)
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Dnote - Spontaneously capture new engineering lessons\n")
fmt.Println("Main commands:")
@ -225,13 +127,16 @@ func main() {
err := notes.Run()
check(err)
case "sync":
//err := sync.Sync()
//check(err)
err := sync.Sync()
check(err)
case "login":
err := login.Run()
check(err)
default:
break
}
err = upgrade.AutoUpgrade()
err := upgrade.AutoUpgrade()
if err != nil {
fmt.Println("Warning - Failed to check for update:", err)
}

View file

@ -16,7 +16,8 @@ import (
)
type Config struct {
Book string
Book string
APIKey string
}
// Deprecated. See upgrade/migrate.go