From 941aafc9622b0a3e46b7bcc2597809dd61ccc2de Mon Sep 17 00:00:00 2001 From: Michiel Alexander Boekhoff Date: Thu, 20 Apr 2017 15:30:48 +0100 Subject: [PATCH] `use` now creates a new book entry in the Dnote file. --- cmd/new/new.go | 14 +------------- main.go | 15 +++++++++++++++ utils/utils.go | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/cmd/new/new.go b/cmd/new/new.go index 1ad351e1..8c104085 100644 --- a/cmd/new/new.go +++ b/cmd/new/new.go @@ -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 } diff --git a/main.go b/main.go index 9c4b42d9..f5a254dc 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/utils/utils.go b/utils/utils.go index f24d6724..a312dfea 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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{}