Artifact 3def02bcf87f1a123c5c2d4e8bb578fd250dbf3bd5e89e65470599d0caab9b91:
- File
leaf/agent/notify/device_registry.go
— part of check-in
[38efed7a12]
at
2026-08-12 20:50:57
on branch main
— chore(deps): migrate to go-libfossil v0.9.0 (#195)
* chore(deps): migrate to go-libfossil v0.9.0 (renamed module path)
- import path danmestas/libfossil → danmestas/go-libfossil everywhere - CloneOpts.ProjectCode removed upstream; assert via CloneResult instead - Timeline now takes TimelineOpts (cursor pagination, newest-first) - testutil.HasFossil removed; check FossilBinary() != ""
* ci: retrigger checks
---------
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> (user: [email protected], size: 2422) [annotate] [blame] [check-ins using]
package notify import ( "encoding/json" "fmt" "time" libfossil "github.com/danmestas/go-libfossil" ) const devicesFilePath = "_notify/devices.json" // Device is a paired device entry in the registry. type Device struct { Name string `json:"name"` EndpointID string `json:"endpoint_id"` PairedAt time.Time `json:"paired_at"` } // DeviceRegistry is the JSON structure stored in the notify repo. type DeviceRegistry struct { Devices []Device `json:"devices"` } // ListDevices reads the device registry from the notify repo. // Returns an empty slice (not error) if the file doesn't exist yet. func ListDevices(r *libfossil.Repo) ([]Device, error) { data, err := readFileContent(r, devicesFilePath) if err != nil { // If file doesn't exist, return empty. return nil, nil } var reg DeviceRegistry if err := json.Unmarshal(data, ®); err != nil { return nil, fmt.Errorf("unmarshal device registry: %w", err) } return reg.Devices, nil } // AddDevice adds a device to the registry. Rejects duplicate names. func AddDevice(r *libfossil.Repo, dev Device) error { devices, err := ListDevices(r) if err != nil { return err } for _, d := range devices { if d.Name == dev.Name { return fmt.Errorf("device %q already exists", dev.Name) } } devices = append(devices, dev) return commitDevices(r, devices) } // RemoveDevice removes a device by name. Returns error if not found. func RemoveDevice(r *libfossil.Repo, name string) error { devices, err := ListDevices(r) if err != nil { return err } found := false filtered := make([]Device, 0, len(devices)) for _, d := range devices { if d.Name == name { found = true continue } filtered = append(filtered, d) } if !found { return fmt.Errorf("device %q not found", name) } return commitDevices(r, filtered) } // commitDevices serializes the device list and commits it to the notify repo. func commitDevices(r *libfossil.Repo, devices []Device) error { reg := DeviceRegistry{Devices: devices} data, err := json.MarshalIndent(reg, "", " ") if err != nil { return fmt.Errorf("marshal device registry: %w", err) } _, _, err = r.Commit(libfossil.CommitOpts{ Files: []libfossil.FileToCommit{ {Name: devicesFilePath, Content: data}, }, Comment: "notify: update device registry", User: "notify", }) if err != nil { return fmt.Errorf("commit device registry: %w", err) } return nil }