c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
240 lines
6.9 KiB
Go
240 lines
6.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/transfer"
|
|
"git.a-wan.net/alan/pipelines/internal/tui"
|
|
)
|
|
|
|
func (a App) exportBundle(ctx context.Context, o *options, pos []string, call callFunc, resolve resolveFunc) error {
|
|
if o.all && len(pos) > 0 {
|
|
return fmt.Errorf("--all cannot be combined with names")
|
|
}
|
|
r, err := call(protocol.Message{Type: protocol.List})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var selected []protocol.PipelineSummary
|
|
if len(pos) > 0 {
|
|
for _, name := range pos {
|
|
p, err := resolve(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
selected = append(selected, p)
|
|
}
|
|
} else if o.all {
|
|
selected = r.Pipelines
|
|
} else {
|
|
if o.json {
|
|
return fmt.Errorf("export --json requires names or --all")
|
|
}
|
|
selected, err = tui.PickPipelines(ctx, r.Pipelines, "Select pipelines to export")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if len(selected) == 0 {
|
|
return fmt.Errorf("no pipelines selected")
|
|
}
|
|
var entries []transfer.Entry
|
|
seen := map[int64]bool{}
|
|
for _, p := range selected {
|
|
if seen[p.ID] {
|
|
continue
|
|
}
|
|
seen[p.ID] = true
|
|
version := o.version
|
|
if version == 0 {
|
|
version = p.Version
|
|
}
|
|
detail, err := call(protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID, Version: version})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if detail.Detail == nil {
|
|
return fmt.Errorf("missing definition for %q", p.Name)
|
|
}
|
|
entries = append(entries, transfer.Entry{Name: detail.Detail.Name, Version: detail.Detail.Version, Source: detail.Detail.Source})
|
|
}
|
|
filename := o.output
|
|
if filename == "" {
|
|
filename = "pipelines-export-" + time.Now().Format("20060102-150405.000000000") + ".zip"
|
|
}
|
|
filename, err = filepath.Abs(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = transfer.Write(filename, entries); err != nil {
|
|
return err
|
|
}
|
|
if o.json {
|
|
return a.output(map[string]any{"path": filename, "pipelines": len(entries)})
|
|
}
|
|
_, err = fmt.Fprintf(a.Out, "Exported %d pipelines to %s\n", len(entries), filename)
|
|
return err
|
|
}
|
|
|
|
func (a App) importBundle(ctx context.Context, o *options, pos []string, call callFunc) error {
|
|
if o.conflict != "ask" && o.conflict != "keep" && o.conflict != "both" && o.conflict != "import" {
|
|
return fmt.Errorf("--on-conflict must be ask, keep, both, or import")
|
|
}
|
|
input := a.In
|
|
if input == nil {
|
|
input = os.Stdin
|
|
}
|
|
reader := bufio.NewReader(input)
|
|
a.In = reader
|
|
if len(pos) > 1 {
|
|
return fmt.Errorf("import accepts one ZIP path")
|
|
}
|
|
filename := ""
|
|
if len(pos) == 1 {
|
|
filename = pos[0]
|
|
} else {
|
|
if o.json {
|
|
return fmt.Errorf("import --json requires a ZIP path")
|
|
}
|
|
fmt.Fprint(a.Out, "Pipeline bundle ZIP path: ")
|
|
line, err := reader.ReadString('\n')
|
|
if err != nil && len(line) == 0 {
|
|
return fmt.Errorf("import canceled")
|
|
}
|
|
filename = strings.TrimSpace(line)
|
|
}
|
|
entries, err := transfer.Read(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !o.all {
|
|
if o.json {
|
|
return fmt.Errorf("import --json requires --all")
|
|
}
|
|
items := make([]protocol.PipelineSummary, len(entries))
|
|
for i, e := range entries {
|
|
items[i] = protocol.PipelineSummary{ID: int64(i + 1), Name: e.Name, Version: e.Version}
|
|
}
|
|
picked, err := tui.PickPipelines(ctx, items, "Select pipelines to import")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
selected := map[int64]bool{}
|
|
for _, p := range picked {
|
|
selected[p.ID] = true
|
|
}
|
|
var keep []transfer.Entry
|
|
for i, e := range entries {
|
|
if selected[int64(i+1)] {
|
|
keep = append(keep, e)
|
|
}
|
|
}
|
|
entries = keep
|
|
}
|
|
// All selected sources/names are checked before any publication.
|
|
existing, err := call(protocol.Message{Type: protocol.List})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
names := map[string]protocol.PipelineSummary{}
|
|
reserved := map[string]bool{}
|
|
for _, p := range existing.Pipelines {
|
|
names[p.Name] = p
|
|
reserved[p.Name] = true
|
|
}
|
|
for _, e := range entries {
|
|
reserved[e.Name] = true
|
|
}
|
|
plans := []importPlan{}
|
|
skipped := []importSkip{}
|
|
for _, e := range entries {
|
|
result, err := call(protocol.Message{Type: protocol.Validate, Source: e.Source})
|
|
if err != nil {
|
|
return fmt.Errorf("validate %q: %w", e.Name, err)
|
|
}
|
|
if result.Type != protocol.Validated || result.Graph == nil {
|
|
return fmt.Errorf("pipeline %q failed validation", e.Name)
|
|
}
|
|
plan := importPlan{entry: e}
|
|
if p, exists := names[e.Name]; exists {
|
|
r, err := call(protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID, Version: p.Version})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if r.Detail == nil {
|
|
return fmt.Errorf("missing existing definition for %q", e.Name)
|
|
}
|
|
if r.Detail.Source == e.Source {
|
|
skipped = append(skipped, importSkip{Name: e.Name, Reason: "identical"})
|
|
continue
|
|
}
|
|
choice := o.conflict
|
|
if choice == "ask" {
|
|
if o.json {
|
|
return fmt.Errorf("pipeline %q differs from existing v%d; choose --on-conflict keep, both, or import (no changes published)", e.Name, r.Detail.Version)
|
|
}
|
|
choice, err = a.chooseImportConflict(reader, e, r.Detail)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
switch choice {
|
|
case "keep":
|
|
skipped = append(skipped, importSkip{Name: e.Name, Reason: "kept-existing"})
|
|
continue
|
|
case "both":
|
|
plan.entry.Name = importCopyName(e.Name, reserved)
|
|
reserved[plan.entry.Name] = true
|
|
case "import":
|
|
plan.expected = r.Detail.Version
|
|
}
|
|
}
|
|
plans = append(plans, plan)
|
|
}
|
|
if !o.json {
|
|
for _, s := range skipped {
|
|
fmt.Fprintf(a.Out, "Skipped %q · %s\n", s.Name, s.Reason)
|
|
}
|
|
for _, plan := range plans {
|
|
action := "new pipeline v1"
|
|
if plan.expected > 0 {
|
|
action = fmt.Sprintf("update v%d → v%d (history retained)", plan.expected, plan.expected+1)
|
|
}
|
|
fmt.Fprintf(a.Out, " %q · exported v%d → %s\n", plan.entry.Name, plan.entry.Version, action)
|
|
}
|
|
}
|
|
if len(plans) > 0 {
|
|
if err = a.confirmAction(fmt.Sprintf("Publish %d imported definitions? Schedules in their definitions will activate; existing history is retained.", len(plans)), o.yes); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
imported := []string{}
|
|
updated := []string{}
|
|
for _, plan := range plans {
|
|
e := plan.entry
|
|
_, err := call(protocol.Message{Type: protocol.Submit, Name: e.Name, Source: e.Source, CreateOnly: plan.expected == 0, ExpectedVersion: plan.expected})
|
|
if err != nil {
|
|
if o.json {
|
|
_ = a.output(map[string]any{"imported": imported, "updated": updated, "skipped": skipped, "failed": e.Name})
|
|
}
|
|
return fmt.Errorf("import stopped at %q after publishing %d definitions (%v): %w; accepted definitions were retained", e.Name, len(imported), imported, err)
|
|
}
|
|
imported = append(imported, e.Name)
|
|
if plan.expected > 0 {
|
|
updated = append(updated, e.Name)
|
|
}
|
|
}
|
|
if o.json {
|
|
return a.output(map[string]any{"imported": imported, "updated": updated, "skipped": skipped})
|
|
}
|
|
_, err = fmt.Fprintf(a.Out, "Published %d definitions (%d updates, %d skipped): %v\n", len(imported), len(updated), len(skipped), imported)
|
|
return err
|
|
}
|