package main import ( "sort" scribble "github.com/nanobox-io/golang-scribble" ) type Meta struct { Years []int Tags []Tag } func LoadMetadata() (*Meta, error) { db, err := scribble.New(cfg.Get("dbdir"), nil) var m *Meta err = db.Read("meta", "data", &m) return m, err } func SaveMetadata(m *Meta) error { db, err := scribble.New(cfg.Get("dbdir"), nil) if err != nil { return err } return db.Write("meta", "data", m) } func (m *Meta) AddYear(yr int) { for _, v := range m.Years { if v == yr { return } } m.Years = append(m.Years, yr) // Sort the years sort.Slice(m.Years, func(i, j int) bool { return m.Years[i] > m.Years[j] }) } func (m *Meta) HasYear(yr int) bool { for _, v := range m.Years { if v == yr { return true } } return false } func (m *Meta) AddTags(tgs []string) { for _, tg := range tgs { if tag := m.GetTag(tg); tag == nil { m.SaveTag(NewTag(tg, false)) } } // sort the tags sort.Slice(m.Tags, func(i, j int) bool { return m.Tags[i].Name > m.Tags[j].Name }) } func (m *Meta) SaveTag(t *Tag) { for i, v := range m.Tags { if v.Name == t.Name { m.Tags[i].Billable = t.Billable return } } m.Tags = append(m.Tags, *t) } func (m *Meta) GetTag(uuid string) *Tag { for _, v := range m.Tags { if v.UUID == uuid { return &v } } return nil } func (m *Meta) GetTags(uuids []string) []Tag { var ret []Tag for _, v := range uuids { if t := m.GetTag(v); t != nil { ret = append(ret, *t) } } return ret } func (m *Meta) GetTagByName(nm string) *Tag { for _, v := range m.Tags { if v.Name == nm { return &v } } return nil } func (m *Meta) GetTagsByName(nms []string) []Tag { var ret []Tag for _, v := range nms { if t := m.GetTagByName(v); t != nil { ret = append(ret, *t) } } return ret }