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>
99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
// Package manifest is skills.json: the sources and skills you want.
|
|
package manifest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"skill-manager/internal/jsonfile"
|
|
)
|
|
|
|
type Manifest struct {
|
|
Sources map[string]*Source `json:"sources"`
|
|
}
|
|
|
|
type Source struct {
|
|
// URL is a flake ref, e.g. github:owner/repo or git+ssh://git@host/user/repo.
|
|
URL string `json:"url"`
|
|
// Ref is the branch or tag to track; empty means the default branch.
|
|
Ref string `json:"ref,omitempty"`
|
|
Skills Selection `json:"skills"`
|
|
}
|
|
|
|
// Selection is either "*" (every skill in the repo) or a list of skills.
|
|
type Selection struct {
|
|
All bool
|
|
Items []Skill
|
|
}
|
|
|
|
// Skill names a skill in its repo and optionally renames it on install.
|
|
type Skill struct {
|
|
Name string `json:"name"`
|
|
// Path picks one skill when the repo has several with this name.
|
|
Path string `json:"path,omitempty"`
|
|
As string `json:"as,omitempty"`
|
|
}
|
|
|
|
// Installed is the name the skill gets on disk.
|
|
func (k Skill) Installed() string {
|
|
if k.As != "" {
|
|
return k.As
|
|
}
|
|
return k.Name
|
|
}
|
|
|
|
func Load(path string) (*Manifest, error) {
|
|
m := &Manifest{}
|
|
if _, err := jsonfile.Read(path, m); err != nil {
|
|
return nil, err
|
|
}
|
|
if m.Sources == nil {
|
|
m.Sources = map[string]*Source{}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (m *Manifest) Save(path string) error {
|
|
return jsonfile.Write(path, m)
|
|
}
|
|
|
|
func (s Selection) MarshalJSON() ([]byte, error) {
|
|
if s.All {
|
|
return json.Marshal("*")
|
|
}
|
|
if s.Items == nil {
|
|
return []byte("[]"), nil
|
|
}
|
|
return json.Marshal(s.Items)
|
|
}
|
|
|
|
func (s *Selection) UnmarshalJSON(b []byte) error {
|
|
var str string
|
|
if json.Unmarshal(b, &str) == nil {
|
|
if str != "*" {
|
|
return fmt.Errorf(`skills must be "*" or a list, got %q`, str)
|
|
}
|
|
*s = Selection{All: true}
|
|
return nil
|
|
}
|
|
*s = Selection{}
|
|
return json.Unmarshal(b, &s.Items)
|
|
}
|
|
|
|
// A skill is written as "name", or as an object when it has a path or alias.
|
|
func (k Skill) MarshalJSON() ([]byte, error) {
|
|
if k.As == "" && k.Path == "" {
|
|
return json.Marshal(k.Name)
|
|
}
|
|
type plain Skill
|
|
return json.Marshal(plain(k))
|
|
}
|
|
|
|
func (k *Skill) UnmarshalJSON(b []byte) error {
|
|
if json.Unmarshal(b, &k.Name) == nil {
|
|
return nil
|
|
}
|
|
type plain Skill
|
|
return json.Unmarshal(b, (*plain)(k))
|
|
}
|