Rework input args to be glob instead of files
So we don't have to do the whole find | shit
This commit is contained in:
48
main.go
48
main.go
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
@@ -71,30 +72,44 @@ func init() {
|
||||
func main() {
|
||||
// Define flags
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s <regex_with_capture_groups> <lua_expression> <...files>\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "Example: %s \"<value>(\\d+)</value>,(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " or simplified: %s \"<value>(\\d+)</value>,(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " or even simpler: %s \"<value>(\\d+)</value>\" \"*1.5\" data.xml\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " or direct assignment: %s \"<value>(\\d+)</value>\" \"=0\" data.xml\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s <regex_with_capture_groups> <lua_expression> <...files_or_globs>\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "\nExamples:\n")
|
||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" data.xml\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"*1.5\" \"*.xml\"\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>,(\\d+)\" \"v1 * 1.5 * v2\" data.xml\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s \"<value>(\\d+)</value>\" \"=0\" data.xml\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "\nNote: v1, v2, etc. are used to refer to capture groups as numbers.\n")
|
||||
fmt.Fprintf(os.Stderr, " s1, s2, etc. are used to refer to capture groups as strings.\n")
|
||||
fmt.Fprintf(os.Stderr, " Helper functions: num(str) converts string to number, str(num) converts number to string\n")
|
||||
fmt.Fprintf(os.Stderr, " is_number(str) checks if a string is numeric\n")
|
||||
fmt.Fprintf(os.Stderr, " If expression starts with an operator like *, /, +, -, =, etc., v1 is automatically prepended\n")
|
||||
fmt.Fprintf(os.Stderr, " You can use any valid Lua code, including if statements, loops, etc.\n")
|
||||
fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
if len(args) < 3 {
|
||||
Error.Println("Insufficient arguments - need regex pattern, lua expression, and at least one file")
|
||||
Error.Println("Insufficient arguments - need regex pattern, lua expression, and at least one file or glob pattern")
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
regexPattern := args[0]
|
||||
luaExpr := args[1]
|
||||
files := args[2:]
|
||||
filePatterns := args[2:]
|
||||
|
||||
// Expand file patterns with glob support
|
||||
files, err := expandFilePatterns(filePatterns)
|
||||
if err != nil {
|
||||
Error.Printf("Error expanding file patterns: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
Error.Println("No files found matching the specified patterns")
|
||||
return
|
||||
}
|
||||
|
||||
Info.Printf("Starting modifier with pattern '%s', expression '%s' on %d files", regexPattern, luaExpr, len(files))
|
||||
|
||||
@@ -506,3 +521,22 @@ func min(a, b int) int {
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func expandFilePatterns(patterns []string) ([]string, error) {
|
||||
var files []string
|
||||
filesMap := make(map[string]bool)
|
||||
|
||||
for _, pattern := range patterns {
|
||||
matches, _ := doublestar.Glob(os.DirFS("."), pattern)
|
||||
for _, m := range matches {
|
||||
if info, err := os.Stat(m); err == nil && !info.IsDir() && !filesMap[m] {
|
||||
filesMap[m], files = true, append(files, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(files) > 0 {
|
||||
Info.Printf("Found %d files to process", len(files))
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user