create data file if not exist

This commit is contained in:
agatha 2024-05-12 16:26:05 -04:00
parent 29db699202
commit 2fa15112cb

View File

@ -24,9 +24,16 @@ func NewContext(dataPath string) (AppContext, error) {
// LoadRecords will load records from file // LoadRecords will load records from file
func (ctx *AppContext) LoadRecords() error { func (ctx *AppContext) LoadRecords() error {
data, err := os.ReadFile(ctx.dataPath) data, err := os.ReadFile(ctx.dataPath)
if err != nil {
// Create the records file if it does not exist
if os.IsNotExist(err) {
ctx.Records = []model.Record{}
return ctx.SaveRecords()
} else if err != nil {
// Some other type of error has occurred
return err return err
} }
return json.Unmarshal(data, &ctx.Records) return json.Unmarshal(data, &ctx.Records)
} }