CLI (add, remove, list, outdated, update, import) over a skills.json manifest and a skills.lock.json pinned with nix flake prefetch, plus a home-manager module that links each locked skill via builtins.fetchTree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
// Package jsonfile reads and atomically writes the hand-editable JSON files.
|
|
package jsonfile
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// Read decodes path into v. A missing file leaves v untouched and reports false.
|
|
func Read(path string, v any) (bool, error) {
|
|
b, err := os.ReadFile(path)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if err := json.Unmarshal(b, v); err != nil {
|
|
return false, fmt.Errorf("%s: %w", path, err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// Write encodes v with two-space indent and replaces path atomically.
|
|
func Write(path string, v any) error {
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
// Keep "&" in flake ref queries readable.
|
|
enc.SetEscapeHTML(false)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(v); err != nil {
|
|
return err
|
|
}
|
|
|
|
tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(tmp.Name())
|
|
if _, err := tmp.Write(buf.Bytes()); err != nil {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if err := tmp.Close(); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Chmod(tmp.Name(), 0o644); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp.Name(), path)
|
|
}
|