package cli import ( "bufio" "fmt" "strings" "unicode/utf8" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/transfer" "git.a-wan.net/alan/pipelines/internal/tui" "github.com/pmezard/go-difflib/difflib" ) type importPlan struct { entry transfer.Entry expected int } type importSkip struct { Name string `json:"name"` Reason string `json:"reason"` } func importCopyName(name string, reserved map[string]bool) string { for i := 1; ; i++ { suffix := " (imported)" if i > 1 { suffix = fmt.Sprintf(" (imported %d)", i) } base := name if len(base)+len(suffix) > 200 { base = base[:200-len(suffix)] for !utf8.ValidString(base) { base = base[:len(base)-1] } } copy := base + suffix if !reserved[copy] { return copy } } } func (a App) chooseImportConflict(reader *bufio.Reader, e transfer.Entry, existing *protocol.PipelineDetail) (string, error) { fmt.Fprintf(a.Out, "\nPipeline %q differs: existing v%d · imported v%d.\n", e.Name, existing.Version, e.Version) for { fmt.Fprint(a.Err, "[k] Keep existing · [b] Keep both (new name) · [i] Use imported (new version) · [d] View diff · [c] Cancel: ") line, err := reader.ReadString('\n') if err != nil && len(line) == 0 { return "", fmt.Errorf("import canceled; choose --on-conflict for automation") } switch strings.ToLower(strings.TrimSpace(line)) { case "k", "keep": return "keep", nil case "b", "both": return "both", nil case "i", "import": return "import", nil case "c", "cancel", "": return "", fmt.Errorf("import canceled; no definitions published") case "d", "diff": if strings.Count(existing.Source, "\n")+strings.Count(e.Source, "\n") > 10000 { fmt.Fprintln(a.Out, "Diff too large for this preview; export the existing source to compare locally.") continue } patch, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{A: difflib.SplitLines(existing.Source), B: difflib.SplitLines(e.Source), FromFile: "existing", ToFile: "imported", Context: 3}) if err != nil { return "", err } if len(patch) > 128<<10 { patch = patch[:128<<10] fmt.Fprintln(a.Out, "Diff preview truncated to 128 KiB.") } fmt.Fprintln(a.Out, tui.HighlightDiff(patch)) default: fmt.Fprintln(a.Err, "Choose k, b, i, d, or c.") } } }