feat: skm, pin agent skills from git repos via Nix
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>
This commit is contained in:
commit
65a96ada8e
26 files changed
+2843
No files matched your search
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/tabwriter"
|
||||
|
||||
"skill-manager/internal/core"
|
||||
)
|
||||
|
||||
func importCmd(ctx context.Context, store core.Store, args []string) error {
|
||||
stateDir := os.Getenv("XDG_STATE_HOME")
|
||||
if stateDir == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stateDir = filepath.Join(home, ".local", "state")
|
||||
}
|
||||
fs := flag.NewFlagSet("import", flag.ContinueOnError)
|
||||
from := fs.String("from", filepath.Join(stateDir, "skills", ".skill-lock.json"), "vercel skills lock to import")
|
||||
local := fs.String("local", filepath.Join(store.Dir, "skills"), "directory with the committed skill copies to compare against")
|
||||
showDiff := fs.Bool("diff", false, "print diffs for skills that differ from your copy (a/ yours, b/ upstream)")
|
||||
pos, err := parse(fs, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(pos) != 0 {
|
||||
return errors.New("import takes no arguments")
|
||||
}
|
||||
|
||||
rep, err := store.Import(ctx, *from, *local)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
groups := map[core.ImportStatus][]core.Imported{}
|
||||
for _, imp := range rep.Imported {
|
||||
groups[imp.Status] = append(groups[imp.Status], imp)
|
||||
}
|
||||
fmt.Printf("%d skills from %d sources are in %s\n", len(rep.Imported), rep.Sources, core.ManifestFile)
|
||||
section := func(title string, items []core.Imported) {
|
||||
if len(items) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Printf("\n%s (%d):\n", title, len(items))
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
for _, imp := range items {
|
||||
fmt.Fprintf(w, " %s\t%s\n", imp.Name, imp.Source)
|
||||
}
|
||||
w.Flush()
|
||||
}
|
||||
section("identical to your copy", groups[core.Identical])
|
||||
section("differ from your copy; upstream replaces it on rebuild, `skm remove <name>` keeps your copy", groups[core.Differs])
|
||||
section("no committed copy", groups[core.NoLocalCopy])
|
||||
|
||||
if len(rep.Skipped) > 0 {
|
||||
fmt.Printf("\nskipped (%d):\n", len(rep.Skipped))
|
||||
for _, s := range rep.Skipped {
|
||||
fmt.Println(" " + s)
|
||||
}
|
||||
}
|
||||
if len(rep.Untracked) > 0 {
|
||||
fmt.Printf("\nnot managed by skm, still committed in %s (%d):\n", *local, len(rep.Untracked))
|
||||
for _, name := range rep.Untracked {
|
||||
fmt.Println(" " + name)
|
||||
}
|
||||
}
|
||||
|
||||
if *showDiff {
|
||||
for _, imp := range groups[core.Differs] {
|
||||
d, err := core.Diff(ctx, core.Change{Name: imp.Name, Old: imp.Local, New: imp.Upstream})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Print("\n" + d)
|
||||
}
|
||||
} else if len(groups[core.Differs]) > 0 {
|
||||
fmt.Println("\nrun `skm import --diff` to see the differences")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"skill-manager/internal/core"
|
||||
"skill-manager/internal/fetch"
|
||||
"skill-manager/internal/jsonfile"
|
||||
"skill-manager/internal/manifest"
|
||||
)
|
||||
|
||||
const usage = `usage: skm [--dir DIR] <command> [args]
|
||||
|
||||
commands:
|
||||
add <url> [--ref R] [--name N] (--skill a,b=alias,dir/c | --all)
|
||||
remove <name> | remove --source <source>
|
||||
list
|
||||
outdated [source|skill...]
|
||||
update [source|skill...] [--yes] [--switch]
|
||||
import [--from vercel-lock] [--local dir] [--diff]
|
||||
|
||||
--dir defaults to manifestDir from ~/.config/skill-manager/config.json.
|
||||
`
|
||||
|
||||
type config struct {
|
||||
ManifestDir string `json:"manifestDir"`
|
||||
SwitchCommand string `json:"switchCommand"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := run(context.Background(), os.Args[1:])
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
os.Exit(0)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "skm:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context, args []string) error {
|
||||
global := flag.NewFlagSet("skm", flag.ContinueOnError)
|
||||
global.Usage = func() { fmt.Fprint(os.Stderr, usage) }
|
||||
dir := global.String("dir", "", "directory holding skills.json")
|
||||
if err := global.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
args = global.Args()
|
||||
if len(args) == 0 {
|
||||
global.Usage()
|
||||
return errors.New("no command")
|
||||
}
|
||||
|
||||
cfg, err := loadConfig(*dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store := core.Store{Dir: cfg.ManifestDir, Fetcher: fetch.Nix{}}
|
||||
|
||||
switch cmd, rest := args[0], args[1:]; cmd {
|
||||
case "add":
|
||||
return add(ctx, store, rest)
|
||||
case "remove":
|
||||
return remove(store, rest)
|
||||
case "list":
|
||||
return list(store, rest)
|
||||
case "outdated":
|
||||
return outdated(ctx, store, rest)
|
||||
case "update":
|
||||
return update(ctx, store, cfg, rest)
|
||||
case "import":
|
||||
return importCmd(ctx, store, rest)
|
||||
default:
|
||||
global.Usage()
|
||||
return fmt.Errorf("unknown command %q", cmd)
|
||||
}
|
||||
}
|
||||
|
||||
// loadConfig reads the config the home-manager module writes; --dir overrides manifestDir.
|
||||
func loadConfig(flagDir string) (config, error) {
|
||||
// XDG on every OS, macOS included: that's where home-manager's xdg.configFile writes.
|
||||
cfgDir := os.Getenv("XDG_CONFIG_HOME")
|
||||
if cfgDir == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return config{}, err
|
||||
}
|
||||
cfgDir = filepath.Join(home, ".config")
|
||||
}
|
||||
path := filepath.Join(cfgDir, "skill-manager", "config.json")
|
||||
var cfg config
|
||||
if _, err := jsonfile.Read(path, &cfg); err != nil {
|
||||
return config{}, err
|
||||
}
|
||||
if flagDir != "" {
|
||||
cfg.ManifestDir = flagDir
|
||||
}
|
||||
if cfg.ManifestDir == "" {
|
||||
return config{}, fmt.Errorf("no manifest directory: pass --dir or set manifestDir in %s", path)
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// parse allows flags after positional arguments, which flag.Parse alone does not.
|
||||
func parse(fs *flag.FlagSet, args []string) ([]string, error) {
|
||||
var pos []string
|
||||
for {
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fs.NArg() == 0 {
|
||||
return pos, nil
|
||||
}
|
||||
pos = append(pos, fs.Arg(0))
|
||||
args = fs.Args()[1:]
|
||||
}
|
||||
}
|
||||
|
||||
func add(ctx context.Context, store core.Store, args []string) error {
|
||||
fs := flag.NewFlagSet("add", flag.ContinueOnError)
|
||||
ref := fs.String("ref", "", "branch or tag to track (default: the default branch)")
|
||||
name := fs.String("name", "", "source name (default: owner/repo from the url)")
|
||||
skillList := fs.String("skill", "", "comma-separated skill names or paths; rename with name=alias")
|
||||
all := fs.Bool("all", false, "install every skill, including ones added upstream later")
|
||||
pos, err := parse(fs, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(pos) != 1 {
|
||||
return errors.New("add takes exactly one url")
|
||||
}
|
||||
|
||||
req := core.AddRequest{Name: *name, URL: pos[0], Ref: *ref, All: *all}
|
||||
for item := range strings.SplitSeq(*skillList, ",") {
|
||||
if item = strings.TrimSpace(item); item == "" {
|
||||
continue
|
||||
}
|
||||
ref, as, _ := strings.Cut(item, "=")
|
||||
k := manifest.Skill{Name: ref, As: as}
|
||||
if strings.Contains(ref, "/") {
|
||||
k = manifest.Skill{Path: ref, As: as}
|
||||
}
|
||||
req.Skills = append(req.Skills, k)
|
||||
}
|
||||
|
||||
key, err := store.Add(ctx, req)
|
||||
if sel, ok := errors.AsType[*core.NoSelectionError](err); ok {
|
||||
fmt.Fprintf(os.Stderr, "skills in %s:\n", sel.Source)
|
||||
w := tabwriter.NewWriter(os.Stderr, 0, 0, 2, ' ', 0)
|
||||
for _, s := range sel.Available {
|
||||
fmt.Fprintf(w, " %s\t%s\n", s.Name, s.Path)
|
||||
}
|
||||
w.Flush()
|
||||
return errors.New("choose with --skill a,b (or a path for same-named skills) or --all")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("added to %s; rebuild to install\n", key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func remove(store core.Store, args []string) error {
|
||||
fs := flag.NewFlagSet("remove", flag.ContinueOnError)
|
||||
source := fs.String("source", "", "remove a whole source")
|
||||
pos, err := parse(fs, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case *source != "" && len(pos) == 0:
|
||||
if err := store.RemoveSource(*source); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("removed source %s\n", *source)
|
||||
case *source == "" && len(pos) == 1:
|
||||
key, err := store.Remove(pos[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("removed %s (from %s)\n", pos[0], key)
|
||||
default:
|
||||
return errors.New("remove takes one skill name, or --source")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func list(store core.Store, args []string) error {
|
||||
if len(args) != 0 {
|
||||
return errors.New("list takes no arguments")
|
||||
}
|
||||
installed, err := store.List()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(w, "NAME\tSOURCE\tREV\tPATH")
|
||||
for _, s := range installed {
|
||||
fmt.Fprintf(w, "%s\t%s\t%.7s\t%s\n", s.Name, s.Source, s.Rev, s.Path)
|
||||
}
|
||||
return w.Flush()
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"skill-manager/internal/core"
|
||||
)
|
||||
|
||||
func outdated(ctx context.Context, store core.Store, args []string) error {
|
||||
statuses, err := store.Check(ctx, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
failed, n := 0, 0
|
||||
for _, st := range statuses {
|
||||
if st.Err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", st.Source, st.Err)
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
if st.Outdated() {
|
||||
printStatus(os.Stdout, st)
|
||||
n++
|
||||
}
|
||||
}
|
||||
if n == 0 && failed == 0 {
|
||||
fmt.Println("all skills up to date")
|
||||
}
|
||||
if failed > 0 {
|
||||
return fmt.Errorf("%d source(s) failed", failed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func update(ctx context.Context, store core.Store, cfg config, args []string) error {
|
||||
fs := flag.NewFlagSet("update", flag.ContinueOnError)
|
||||
yes := fs.Bool("yes", false, "apply without showing diffs or asking")
|
||||
doSwitch := fs.Bool("switch", false, "run switchCommand afterwards")
|
||||
names, err := parse(fs, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
statuses, err := store.Check(ctx, names...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
in := bufio.NewReader(os.Stdin)
|
||||
var accepted []core.SourceStatus
|
||||
failed := 0
|
||||
for _, st := range statuses {
|
||||
if st.Err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", st.Source, st.Err)
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
if !st.Outdated() {
|
||||
continue
|
||||
}
|
||||
printStatus(os.Stdout, st)
|
||||
if !*yes {
|
||||
for _, ch := range st.Changes {
|
||||
d, err := core.Diff(ctx, ch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Print(d)
|
||||
}
|
||||
ok, err := confirm(in, fmt.Sprintf("apply %s? [y/N] ", st.Source))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
accepted = append(accepted, st)
|
||||
}
|
||||
|
||||
if len(accepted) > 0 {
|
||||
if err := store.Apply(accepted); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("\nupdated %s. Suggested commit message:\n\n%s\n\n", core.LockFile, core.CommitMessage(accepted))
|
||||
if err := rebuild(cfg, *doSwitch); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if failed == 0 {
|
||||
fmt.Println("nothing to update")
|
||||
}
|
||||
if failed > 0 {
|
||||
return fmt.Errorf("%d source(s) failed", failed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printStatus(w io.Writer, st core.SourceStatus) {
|
||||
switch {
|
||||
case st.OldRev == "":
|
||||
fmt.Fprintf(w, "%s (new) → %.7s\n", st.Source, st.NewRev)
|
||||
case st.NewRev == "":
|
||||
fmt.Fprintf(w, "%s %.7s (removed from skills.json)\n", st.Source, st.OldRev)
|
||||
default:
|
||||
fmt.Fprintf(w, "%s %.7s → %.7s\n", st.Source, st.OldRev, st.NewRev)
|
||||
}
|
||||
for _, ch := range st.Changes {
|
||||
fmt.Fprintf(w, " %s %s\n", ch.Kind.Symbol(), ch.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func confirm(in *bufio.Reader, prompt string) (bool, error) {
|
||||
fmt.Print(prompt)
|
||||
line, err := in.ReadString('\n')
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return false, err
|
||||
}
|
||||
answer := strings.ToLower(strings.TrimSpace(line))
|
||||
return answer == "y" || answer == "yes", nil
|
||||
}
|
||||
|
||||
func rebuild(cfg config, run bool) error {
|
||||
if cfg.SwitchCommand == "" {
|
||||
fmt.Println("rebuild to install")
|
||||
return nil
|
||||
}
|
||||
if !run {
|
||||
fmt.Printf("rebuild to install: %s\n", cfg.SwitchCommand)
|
||||
return nil
|
||||
}
|
||||
cmd := exec.Command("sh", "-c", cfg.SwitchCommand)
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("switch command: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user