use now creates a new book entry in the Dnote file.

This commit is contained in:
Michiel Alexander Boekhoff 2017-04-20 15:30:48 +01:00
commit 941aafc962
3 changed files with 38 additions and 13 deletions

View file

@ -1,9 +1,7 @@
package new
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
"github.com/dnote-io/cli/utils"
@ -50,17 +48,7 @@ func writeNote(note utils.Note) error {
dnote[book] = []utils.Note{note}
}
d, err := json.MarshalIndent(dnote, "", " ")
if err != nil {
return err
}
notePath, err := utils.GetDnotePath()
if err != nil {
return err
}
err = ioutil.WriteFile(notePath, d, 0644)
err := utils.WriteDnote(dnote)
if err != nil {
return err
}

15
main.go
View file

@ -75,6 +75,21 @@ func changeBook(bookName string) error {
return err
}
// Now add this book to the .dnote file, for issue #2
dnote, err := utils.GetDnote()
if err != nil {
return err
}
_, exists := dnote[bookName]
if exists == false {
dnote[bookName] = make([]utils.Note, 0)
err := utils.WriteDnote(dnote)
if err != nil {
return err
}
}
fmt.Printf("Now using %s\n", bookName)
return nil

View file

@ -12,6 +12,8 @@ import (
"strconv"
"time"
"github.com/dnote-io/cli/utils"
"gopkg.in/yaml.v2"
)
@ -167,6 +169,26 @@ func GetDnote() (Dnote, error) {
return ret, nil
}
// WriteDnote persists the state of Dnote into the dnote file
func WriteDnote(dnote Dnote) error {
d, err := json.MarshalIndent(dnote, "", " ")
if err != nil {
return err
}
notePath, err := utils.GetDnotePath()
if err != nil {
return err
}
err = ioutil.WriteFile(notePath, d, 0644)
if err != nil {
return err
}
return nil
}
// Deprecated. See upgrade/upgrade.go
func GetNote() (YAMLDnote, error) {
ret := YAMLDnote{}