a version with known bug fixed but not sure any more obscure ones
This commit is contained in:
+63
-14
@@ -4,14 +4,16 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"cliprepo/internal/config"
|
||||
"cliprepo/internal/resolver"
|
||||
"cliprepo/internal/tree"
|
||||
"cliprepo/internal/utils"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/atotto/clipboard"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var delineate string
|
||||
@@ -35,29 +37,74 @@ var runCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
// builder.WriteString("===== PROJECT TREE =====\n")
|
||||
builder.WriteString(treeStr)
|
||||
builder.WriteString("\n\n")
|
||||
|
||||
// 2. preset files
|
||||
var patterns []string
|
||||
// 2. resolver (GLOBAL FILE REGISTRY)
|
||||
r := resolver.New(".")
|
||||
|
||||
raw, ok := cfg.Preset[delineate]
|
||||
if !ok {
|
||||
return fmt.Errorf("preset not found: %s", delineate)
|
||||
}
|
||||
|
||||
patterns = append(patterns, raw...)
|
||||
patterns := []string(raw)
|
||||
|
||||
files, err := utils.ExpandGlob(patterns)
|
||||
if err != nil {
|
||||
return err
|
||||
var files []string
|
||||
|
||||
for _, pattern := range patterns {
|
||||
|
||||
// =========================
|
||||
// FULL REPO SCAN
|
||||
// =========================
|
||||
if pattern == "*" {
|
||||
all, err := r.WalkAll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files = append(files, all...)
|
||||
continue
|
||||
}
|
||||
|
||||
// =========================
|
||||
// recursive glob support
|
||||
// =========================
|
||||
if strings.Contains(pattern, "**") {
|
||||
matches, err := utils.ExpandGlob([]string{pattern})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, m := range matches {
|
||||
r.AddFile(m, &files)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// =========================
|
||||
// normal glob
|
||||
// =========================
|
||||
matches, err := utils.ExpandGlob([]string{pattern})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, m := range matches {
|
||||
r.AddFile(m, &files)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. template
|
||||
template := cfg.Prompts.Preset
|
||||
// 3. dedup + stable order
|
||||
files = r.Unique(files)
|
||||
sort.Strings(files)
|
||||
|
||||
// 4. template
|
||||
template := cfg.Prompts.Preset
|
||||
if template == "" {
|
||||
template = "The following code is from file {{path}}:\n---\n{{content}}"
|
||||
}
|
||||
|
||||
// 5. render
|
||||
for _, file := range files {
|
||||
|
||||
content, err := os.ReadFile(file)
|
||||
@@ -65,13 +112,14 @@ var runCmd = &cobra.Command{
|
||||
continue
|
||||
}
|
||||
|
||||
path := file
|
||||
// 先初始化 filePath 变量,避免作用域及同名包冲突
|
||||
filePath := file
|
||||
if cfg.Config.PathMode == "absolute" {
|
||||
abs, _ := filepath.Abs(file)
|
||||
path = abs
|
||||
filePath = abs
|
||||
}
|
||||
|
||||
block := strings.ReplaceAll(template, "{{path}}", path)
|
||||
block := strings.ReplaceAll(template, "{{path}}", filePath)
|
||||
block = strings.ReplaceAll(block, "{{content}}", string(content))
|
||||
|
||||
builder.WriteString(block)
|
||||
@@ -80,6 +128,7 @@ var runCmd = &cobra.Command{
|
||||
|
||||
result := builder.String()
|
||||
|
||||
// 6. output
|
||||
if useClipboard {
|
||||
return clipboard.WriteAll(result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user