LoadRegistry reads the installation registry from disk. Returns an empty registry if the file doesn't exist.
()
| 42 | // LoadRegistry reads the installation registry from disk. |
| 43 | // Returns an empty registry if the file doesn't exist. |
| 44 | func LoadRegistry() (*Registry, error) { |
| 45 | path, err := registryPath() |
| 46 | if err != nil { |
| 47 | return &Registry{}, nil |
| 48 | } |
| 49 | |
| 50 | data, err := os.ReadFile(path) |
| 51 | if err != nil { |
| 52 | if os.IsNotExist(err) { |
| 53 | return &Registry{}, nil |
| 54 | } |
| 55 | return nil, fmt.Errorf("read registry: %w", err) |
| 56 | } |
| 57 | |
| 58 | var reg Registry |
| 59 | if err := json.Unmarshal(data, ®); err != nil { |
| 60 | // Corrupted file — start fresh |
| 61 | return &Registry{}, nil |
| 62 | } |
| 63 | return ®, nil |
| 64 | } |
| 65 | |
| 66 | // Save writes the registry to disk. |
| 67 | func (r *Registry) Save() error { |
no test coverage detected