Skip to content

Commit

Permalink
fix: use toml for events file over json. fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanchapai committed Mar 10, 2024
1 parent 0596e1b commit e9032f7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
6 changes: 4 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"log"
Expand All @@ -12,6 +11,7 @@ import (
"strings"
"time"

"github.com/pelletier/go-toml"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/check.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -10,6 +9,7 @@ import (
"path/filepath"
"sort"

"github.com/pelletier/go-toml"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
ereConfigFolderName = ".ere"
ereCheckedFileName = "checked.txt"
ereEventsFileName = "events.json"
ereEventsFileName = "events.toml"
)

// Returns the ere config folder path
Expand Down
4 changes: 4 additions & 0 deletions cmd/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit e9032f7

Please sign in to comment.