Compare commits
2 Commits
b4c0284734
...
58586395fb
Author | SHA1 | Date | |
---|---|---|---|
58586395fb | |||
c5a68af5e6 |
24
utils/file.go
Normal file
24
utils/file.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CleanPath(path string) string {
|
||||||
|
path = filepath.Clean(path)
|
||||||
|
path = strings.ReplaceAll(path, "\\", "/")
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToAbs(path string) string {
|
||||||
|
if filepath.IsAbs(path) {
|
||||||
|
return CleanPath(path)
|
||||||
|
}
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return CleanPath(path)
|
||||||
|
}
|
||||||
|
return CleanPath(filepath.Join(cwd, path))
|
||||||
|
}
|
@@ -57,6 +57,24 @@ func Matches(path string, glob string) (bool, error) {
|
|||||||
return matches, nil
|
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 {
|
type FileCommandAssociation struct {
|
||||||
File string
|
File string
|
||||||
IsolateCommands []ModifyCommand
|
IsolateCommands []ModifyCommand
|
||||||
@@ -75,7 +93,10 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
|
|||||||
}
|
}
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
for _, glob := range command.Files {
|
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 {
|
if err != nil {
|
||||||
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
||||||
continue
|
continue
|
||||||
@@ -132,9 +153,11 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
|||||||
logger.Debug("Expanding patterns from directory: %s", cwd)
|
logger.Debug("Expanding patterns from directory: %s", cwd)
|
||||||
for pattern := range patterns {
|
for pattern := range patterns {
|
||||||
logger.Trace("Processing pattern: %s", pattern)
|
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)
|
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
|
||||||
for _, m := range matches {
|
for _, m := range matches {
|
||||||
|
m = filepath.Join(static, m)
|
||||||
info, err := os.Stat(m)
|
info, err := os.Stat(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warning("Error getting file info for %s: %v", m, err)
|
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) {
|
func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
|
||||||
cwd, err := os.Getwd()
|
static, pattern := SplitPattern(pattern)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get current working directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
commands := []ModifyCommand{}
|
commands := []ModifyCommand{}
|
||||||
cookFiles, err := doublestar.Glob(os.DirFS(cwd), pattern)
|
cookFiles, err := doublestar.Glob(os.DirFS(static), pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to glob cook files: %w", err)
|
return nil, fmt.Errorf("failed to glob cook files: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cookFile := range cookFiles {
|
for _, cookFile := range cookFiles {
|
||||||
|
cookFile = filepath.Join(static, cookFile)
|
||||||
cookFile = filepath.Clean(cookFile)
|
cookFile = filepath.Clean(cookFile)
|
||||||
cookFile = strings.ReplaceAll(cookFile, "\\", "/")
|
cookFile = strings.ReplaceAll(cookFile, "\\", "/")
|
||||||
logger.Info("Loading commands from cook file: %s", cookFile)
|
logger.Info("Loading commands from cook file: %s", cookFile)
|
||||||
|
Reference in New Issue
Block a user