From e9032f7492697ef02f2de35ea932095da023a4a2 Mon Sep 17 00:00:00 2001 From: suman chapai Date: Sun, 10 Mar 2024 13:51:35 +0545 Subject: [PATCH] fix: use toml for events file over json. fixes #3 --- cmd/add.go | 6 ++++-- cmd/check.go | 10 +++++++--- cmd/config.go | 2 +- cmd/event.go | 4 ++++ go.mod | 1 + go.sum | 2 ++ 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index ddf0467..7cd911f 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/json" "errors" "fmt" "log" @@ -12,6 +11,7 @@ import ( "strings" "time" + "github.com/pelletier/go-toml" "github.com/spf13/cobra" ) @@ -135,8 +135,10 @@ func saveEvents(events []Event) { // Save the events configFolder := ereConfigFolder() eventsFile := filepath.Join(configFolder, ereEventsFileName) + var eventsFileModel EventsFileModel + eventsFileModel.Events = events // Save the events - bytes, err := json.MarshalIndent(events, " ", " ") + bytes, err := toml.Marshal(eventsFileModel) if err != nil { log.Fatal(err) } diff --git a/cmd/check.go b/cmd/check.go index eb3d2c9..58dd77f 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/json" "errors" "fmt" "io" @@ -10,6 +9,7 @@ import ( "path/filepath" "sort" + "github.com/pelletier/go-toml" "github.com/spf13/cobra" ) @@ -124,7 +124,11 @@ func PrintKnockEvents(events []KnockEvent) { func eventsFromEventsFile() []Event { configFolder := ereConfigFolder() eventsFile := filepath.Join(configFolder, ereEventsFileName) + + var eventsFileModel EventsFileModel events := make([]Event, 0) + eventsFileModel.Events = events + _, err := os.Stat(eventsFile) if errors.Is(err, os.ErrNotExist) { _, err := os.Create(eventsFile) @@ -145,9 +149,9 @@ func eventsFromEventsFile() []Event { if err != nil { log.Fatal(err) } - err = json.Unmarshal(bytes, &events) + err = toml.Unmarshal(bytes, &eventsFileModel) if err != nil { log.Fatal(err) } - return events + return eventsFileModel.Events } diff --git a/cmd/config.go b/cmd/config.go index cd40773..48c8609 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -10,7 +10,7 @@ import ( var ( ereConfigFolderName = ".ere" ereCheckedFileName = "checked.txt" - ereEventsFileName = "events.json" + ereEventsFileName = "events.toml" ) // Returns the ere config folder path diff --git a/cmd/event.go b/cmd/event.go index f968e76..ac2816a 100644 --- a/cmd/event.go +++ b/cmd/event.go @@ -2,6 +2,10 @@ package cmd import "fmt" +type EventsFileModel struct { + Events []Event +} + type Event struct { Id string // Usually, time.UnixNano is used for Id Date string diff --git a/go.mod b/go.mod index f195cf9..7024f02 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/opensource-nepal/go-nepali v0.2.1 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect github.com/rodaine/table v1.1.1 // indirect github.com/spf13/cobra v1.8.0 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go.sum b/go.sum index b7fb774..84745f9 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/opensource-nepal/go-nepali v0.2.0 h1:+ecQpKgHbMvW1ymB3p6zeeymls0reLaJ github.com/opensource-nepal/go-nepali v0.2.0/go.mod h1:EGIwDjCoiF9Iu+Fhg0RT4aPhkqyz3+VGGd9hb69wzVk= github.com/opensource-nepal/go-nepali v0.2.1 h1:+87IafdUTzg0sSo9jbzRgnfsLkrXy7dfbJzawx0ndJY= github.com/opensource-nepal/go-nepali v0.2.1/go.mod h1:EGIwDjCoiF9Iu+Fhg0RT4aPhkqyz3+VGGd9hb69wzVk= +github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= +github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rodaine/table v1.1.1 h1:zBliy3b4Oj6JRmncse2Z85WmoQvDrXOYuy0JXCt8Qz8=