preset glob non-recursive issue; supporting more syntax
This commit is contained in:
+6
-2
@@ -41,12 +41,16 @@ var runCmd = &cobra.Command{
|
|||||||
builder.WriteString("\n\n")
|
builder.WriteString("\n\n")
|
||||||
|
|
||||||
// 2. preset files
|
// 2. preset files
|
||||||
rawFiles, ok := cfg.Preset[delineate]
|
var patterns []string
|
||||||
|
|
||||||
|
raw, ok := cfg.Preset[delineate]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("preset not found: %s", delineate)
|
return fmt.Errorf("preset not found: %s", delineate)
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := utils.ExpandGlob(rawFiles)
|
patterns = append(patterns, raw...)
|
||||||
|
|
||||||
|
files, err := utils.ExpandGlob(patterns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,36 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type StringOrSlice []string
|
||||||
|
|
||||||
|
func (s *StringOrSlice) UnmarshalYAML(value *yaml.Node) error {
|
||||||
|
switch value.Kind {
|
||||||
|
|
||||||
|
// "preset: *"
|
||||||
|
case yaml.ScalarNode:
|
||||||
|
*s = []string{value.Value}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
// "preset: [a, b]"
|
||||||
|
case yaml.SequenceNode:
|
||||||
|
var out []string
|
||||||
|
for _, v := range value.Content {
|
||||||
|
out = append(out, v.Value)
|
||||||
|
}
|
||||||
|
*s = out
|
||||||
|
return nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid preset format")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Config struct {
|
Config struct {
|
||||||
PathMode string `yaml:"path_mode"`
|
PathMode string `yaml:"path_mode"`
|
||||||
@@ -15,7 +40,7 @@ type Config struct {
|
|||||||
Preset string `yaml:"preset"`
|
Preset string `yaml:"preset"`
|
||||||
} `yaml:"prompts"`
|
} `yaml:"prompts"`
|
||||||
|
|
||||||
Preset map[string][]string `yaml:"preset"`
|
Preset map[string]StringOrSlice `yaml:"preset"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load() (*Config, error) {
|
func Load() (*Config, error) {
|
||||||
@@ -26,5 +51,14 @@ func Load() (*Config, error) {
|
|||||||
|
|
||||||
var cfg Config
|
var cfg Config
|
||||||
err = yaml.Unmarshal(data, &cfg)
|
err = yaml.Unmarshal(data, &cfg)
|
||||||
return &cfg, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DEFAULT PROMPT TEMPLATE
|
||||||
|
if cfg.Prompts.Preset == "" {
|
||||||
|
cfg.Prompts.Preset = "The following code is from file {{path}}:\n---\n{{content}}"
|
||||||
|
}
|
||||||
|
|
||||||
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
+55
-9
@@ -3,30 +3,76 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExpandGlob(patterns []string) ([]string, error) {
|
func ExpandGlob(patterns []string) ([]string, error) {
|
||||||
var result []string
|
var result []string
|
||||||
|
seen := make(map[string]struct{})
|
||||||
|
|
||||||
for _, p := range patterns {
|
for _, pattern := range patterns {
|
||||||
matches, err := filepath.Glob(p)
|
|
||||||
|
if pattern == "*" {
|
||||||
|
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
add(path, seen, &result)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
isRecursive := strings.Contains(pattern, "**")
|
||||||
|
|
||||||
|
if isRecursive {
|
||||||
|
root := strings.Split(pattern, "**")[0]
|
||||||
|
if root == "" {
|
||||||
|
root = "."
|
||||||
|
}
|
||||||
|
|
||||||
|
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil || info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
matched, _ := filepath.Match(
|
||||||
|
strings.Replace(pattern, "**", "*", 1),
|
||||||
|
path,
|
||||||
|
)
|
||||||
|
|
||||||
|
if matched {
|
||||||
|
add(path, seen, &result)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
matches, err := filepath.Glob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, m := range matches {
|
for _, m := range matches {
|
||||||
info, err := os.Stat(m)
|
info, err := os.Stat(m)
|
||||||
if err != nil {
|
if err != nil || info.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
add(m, seen, &result)
|
||||||
if info.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
result = append(result, m)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func add(path string, seen map[string]struct{}, result *[]string) {
|
||||||
|
if _, ok := seen[path]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seen[path] = struct{}{}
|
||||||
|
*result = append(*result, path)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user