PROPERLY implement doublestar
This commit is contained in:
@@ -57,6 +57,24 @@ func Matches(path string, glob string) (bool, error) {
|
||||
return matches, nil
|
||||
}
|
||||
|
||||
func SplitPattern(pattern string) (string, string) {
|
||||
static, pattern := doublestar.SplitPattern(pattern)
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
if static == "" {
|
||||
static = cwd
|
||||
}
|
||||
if !filepath.IsAbs(static) {
|
||||
static = filepath.Join(cwd, static)
|
||||
static = filepath.Clean(static)
|
||||
}
|
||||
static = strings.ReplaceAll(static, "\\", "/")
|
||||
return static, pattern
|
||||
}
|
||||
|
||||
type FileCommandAssociation struct {
|
||||
File string
|
||||
IsolateCommands []ModifyCommand
|
||||
@@ -75,7 +93,10 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
|
||||
}
|
||||
for _, command := range commands {
|
||||
for _, glob := range command.Files {
|
||||
matches, err := Matches(file, glob)
|
||||
static, pattern := SplitPattern(glob)
|
||||
file = strings.ReplaceAll(file, "\\", "/")
|
||||
file = strings.Replace(file, static+`/`, "", 1)
|
||||
matches, err := Matches(file, pattern)
|
||||
if err != nil {
|
||||
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
||||
continue
|
||||
@@ -132,9 +153,11 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
||||
logger.Debug("Expanding patterns from directory: %s", cwd)
|
||||
for pattern := range patterns {
|
||||
logger.Trace("Processing pattern: %s", pattern)
|
||||
matches, _ := doublestar.Glob(os.DirFS(cwd), pattern)
|
||||
static, pattern := SplitPattern(pattern)
|
||||
matches, _ := doublestar.Glob(os.DirFS(static), pattern)
|
||||
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
|
||||
for _, m := range matches {
|
||||
m = filepath.Join(static, m)
|
||||
info, err := os.Stat(m)
|
||||
if err != nil {
|
||||
logger.Warning("Error getting file info for %s: %v", m, err)
|
||||
@@ -172,18 +195,15 @@ func LoadCommands(args []string) ([]ModifyCommand, error) {
|
||||
}
|
||||
|
||||
func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get current working directory: %w", err)
|
||||
}
|
||||
|
||||
static, pattern := SplitPattern(pattern)
|
||||
commands := []ModifyCommand{}
|
||||
cookFiles, err := doublestar.Glob(os.DirFS(cwd), pattern)
|
||||
cookFiles, err := doublestar.Glob(os.DirFS(static), pattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to glob cook files: %w", err)
|
||||
}
|
||||
|
||||
for _, cookFile := range cookFiles {
|
||||
cookFile = filepath.Join(static, cookFile)
|
||||
cookFile = filepath.Clean(cookFile)
|
||||
cookFile = strings.ReplaceAll(cookFile, "\\", "/")
|
||||
logger.Info("Loading commands from cook file: %s", cookFile)
|
||||
|
Reference in New Issue
Block a user