All returns every entity in the store sorted by name.
()
| 79 | |
| 80 | // All returns every entity in the store sorted by name. |
| 81 | func (s *Store) All() ([]Entity, error) { |
| 82 | if err := s.migrateIfInstruction(); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | data := map[string]Entity{} |
| 86 | raw, err := os.ReadFile(s.Path()) |
| 87 | if err != nil { |
| 88 | if os.IsNotExist(err) { |
| 89 | return nil, nil |
| 90 | } |
| 91 | return nil, fmt.Errorf("entities: read %s: %w", s.Path(), err) |
| 92 | } |
| 93 | if len(raw) == 0 { |
| 94 | return nil, nil |
| 95 | } |
| 96 | if err := json.Unmarshal(raw, &data); err != nil { |
| 97 | return nil, fmt.Errorf("entities: parse %s: %w", s.Path(), err) |
| 98 | } |
| 99 | out := make([]Entity, 0, len(data)) |
| 100 | for _, e := range data { |
| 101 | out = append(out, e) |
| 102 | } |
| 103 | sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) |
| 104 | return out, nil |
| 105 | } |
| 106 | |
| 107 | // Get returns an entity by name. |
| 108 | func (s *Store) Get(name string) (Entity, error) { |