create data file if not exist
This commit is contained in:
parent
29db699202
commit
2fa15112cb
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user