bd4f1437bc
Adds a SKILL.md bundle validator, immutable per-skill versions with content digests and deduplication, protocol requests to import, list and show skills, and the pipelines skills import/list/show commands. Imports are serialised so concurrent imports never share a version number.
127 lines
3.6 KiB
Go
127 lines
3.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/client"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/skills"
|
|
)
|
|
|
|
func (a App) skillsCommand(ctx context.Context, args []string) error {
|
|
if len(args) < 1 {
|
|
return fmt.Errorf("usage: pipelines skills import|list|show")
|
|
}
|
|
if args[0] == "help" || args[0] == "--help" || args[0] == "-h" {
|
|
_, err := fmt.Fprintln(a.Out, "Usage: pipelines skills import <folder> [--yes] | skills list | skills show <name> [--version N]")
|
|
return err
|
|
}
|
|
// Accept flags after the folder, matching the rest of the Pipelines CLI.
|
|
var flags, positionals []string
|
|
for i := 1; i < len(args); i++ {
|
|
if args[i] == "--yes" || args[i] == "--json" || args[i] == "-json" {
|
|
flags = append(flags, args[i])
|
|
continue
|
|
}
|
|
if args[i] == "--url" || args[i] == "--version" {
|
|
if i+1 >= len(args) {
|
|
return fmt.Errorf("%s needs a value", args[i])
|
|
}
|
|
flags = append(flags, args[i], args[i+1])
|
|
i++
|
|
continue
|
|
}
|
|
positionals = append(positionals, args[i])
|
|
}
|
|
args = append([]string{args[0]}, append(flags, positionals...)...)
|
|
sub := args[0]
|
|
fs := flag.NewFlagSet("skills "+sub, flag.ContinueOnError)
|
|
fs.SetOutput(a.Err)
|
|
url := envOr("PIPELINES_URL", "ws://127.0.0.1:8080/connect")
|
|
yes := false
|
|
version := 0
|
|
jsonMode := false
|
|
fs.StringVar(&url, "url", url, "Pipeline server WebSocket URL")
|
|
fs.BoolVar(&yes, "yes", false, "Confirm import without prompting")
|
|
fs.BoolVar(&jsonMode, "json", false, "Machine-readable JSON")
|
|
fs.IntVar(&version, "version", 0, "Immutable version (0 = latest)")
|
|
if err := fs.Parse(args[1:]); err != nil {
|
|
return err
|
|
}
|
|
pos := fs.Args()
|
|
if sub == "import" {
|
|
if len(pos) != 1 {
|
|
return fmt.Errorf("skills import requires a folder")
|
|
}
|
|
b, e := skills.Validate(pos[0])
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if !yes {
|
|
if e = a.confirmAction(fmt.Sprintf("Import skill %s (%d files, %d bytes, digest %s)?", b.Name, len(b.Files), b.TotalBytes, b.Digest), false); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
c, e := client.Open(ctx, url)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
defer c.Close()
|
|
files := make([]protocol.SkillFile, len(b.Files))
|
|
for i, f := range b.Files {
|
|
files[i] = protocol.SkillFile{Path: f.Path, Bytes: f.Bytes}
|
|
}
|
|
r, e := c.Call(ctx, protocol.Message{Type: protocol.ImportSkill, Name: b.Name, Description: b.Description, Digest: b.Digest, SourcePath: pos[0], SkillFiles: files})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if r.SkillVersion == nil {
|
|
return fmt.Errorf("server returned no skill version")
|
|
}
|
|
return a.outputSkill(r.SkillVersion, jsonMode)
|
|
}
|
|
if sub != "list" && sub != "show" {
|
|
return fmt.Errorf("unknown skills command %q", sub)
|
|
}
|
|
c, e := client.Open(ctx, url)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
defer c.Close()
|
|
if sub == "list" {
|
|
if len(pos) != 0 {
|
|
return fmt.Errorf("skills list takes no arguments")
|
|
}
|
|
r, e := c.Call(ctx, protocol.Message{Type: protocol.ListSkills})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if jsonMode {
|
|
return a.output(r)
|
|
}
|
|
for _, s := range r.SkillSummaries {
|
|
fmt.Fprintf(a.Out, "%s\tv%d\t%s\n", s.Name, s.LatestVersion, s.Digest)
|
|
}
|
|
return nil
|
|
}
|
|
if len(pos) != 1 {
|
|
return fmt.Errorf("skills show requires a name")
|
|
}
|
|
r, e := c.Call(ctx, protocol.Message{Type: protocol.ShowSkill, Name: pos[0], Version: version})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if r.SkillVersion == nil {
|
|
return fmt.Errorf("server returned no skill")
|
|
}
|
|
return a.outputSkill(r.SkillVersion, jsonMode)
|
|
}
|
|
func (a App) outputSkill(s *protocol.SkillVersion, jsonMode bool) error {
|
|
if jsonMode {
|
|
return a.output(s)
|
|
}
|
|
fmt.Fprintf(a.Out, "%s v%d\t%s\n%s\n", s.Name, s.Version, s.Digest, s.SkillMD)
|
|
return nil
|
|
}
|