Compare commits

..

No commits in common. "796075d9f29bb3b4b9e380e37632d7dc5e03c8ac" and "0ca262c2ea155ef26ec0472da6b05d3255540ec4" have entirely different histories.

2 changed files with 8 additions and 24 deletions

View File

@ -14,22 +14,12 @@ type Record struct {
// AppContext is the application data store that holds Records
type AppContext struct {
Records []Record
dataPath string
}
func NewContext(dataPath string) (AppContext, error) {
ctx := AppContext{dataPath: dataPath}
err := ctx.LoadRecords()
if err != nil {
return AppContext{}, err
}
return ctx, nil
Records []Record
}
// LoadRecords will load records from file
func (ctx *AppContext) LoadRecords() error {
data, err := os.ReadFile(ctx.dataPath)
func (ctx *AppContext) LoadRecords(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
@ -37,12 +27,12 @@ func (ctx *AppContext) LoadRecords() error {
}
// SaveRecords will save records to a file
func (ctx *AppContext) SaveRecords() error {
func (ctx *AppContext) SaveRecords(filename string) error {
jsonData, err := json.Marshal(ctx.Records)
if err != nil {
return err
}
return os.WriteFile(ctx.dataPath, jsonData, 0660)
return os.WriteFile(filename, jsonData, 0660)
}
// AddReading will add a Reading to the AppContext

12
main.go
View File

@ -9,7 +9,6 @@ import (
"strconv"
"strings"
"ketotrack/config"
"ketotrack/data"
"ketotrack/model"
)
@ -39,14 +38,9 @@ func init() {
}
func main() {
appConfig := config.Load()
appCtx, err := data.NewContext(appConfig.DataPath)
if err != nil {
fmt.Printf("Error initializing application context: %s\n", err)
return
}
var appCtx data.AppContext
if err := appCtx.LoadRecords(); err != nil {
if err := appCtx.LoadRecords(recordsFilename); err != nil {
fmt.Printf("Error loading records from file: %s\n", err)
}
@ -85,7 +79,7 @@ func main() {
}
// Save Records before exiting
if err := appCtx.SaveRecords(); err != nil {
if err := appCtx.SaveRecords(recordsFilename); err != nil {
fmt.Printf("Error saving records to file: %s\n", err)
}
}