Remove the vestiges of xml and json

This commit is contained in:
2025-03-27 21:31:45 +01:00
parent 25326ea11b
commit 912950d463
2 changed files with 3 additions and 67 deletions

54
main.go
View File

@@ -24,12 +24,9 @@ type GlobalStats struct {
FailedFiles int FailedFiles int
} }
var stats GlobalStats var stats GlobalStats = GlobalStats{}
var stdLogger *log.Logger // Legacy logger for compatibility
var ( var (
jsonFlag = flag.Bool("json", false, "Process JSON files")
xmlFlag = flag.Bool("xml", false, "Process XML files")
gitFlag = flag.Bool("git", false, "Use git to manage files") gitFlag = flag.Bool("git", false, "Use git to manage files")
resetFlag = flag.Bool("reset", false, "Reset files to their original state") resetFlag = flag.Bool("reset", false, "Reset files to their original state")
logLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE") logLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
@@ -37,59 +34,27 @@ var (
worktree *git.Worktree worktree *git.Worktree
) )
func init() {
// Keep standard logger setup for compatibility with legacy code
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
stdLogger = log.New(os.Stdout, "", log.Lmicroseconds|log.Lshortfile)
stats = GlobalStats{}
}
func main() { func main() {
// TODO: Implement some sort of git integration
// Maybe use go-git
// Specify a -git flag
// If we are operating with git then:
// Inmitialize a repo if one doesn't exist (try to open right?)
// For each file matched by glob first figure out if it's already tracked
// If not tracked then track it and commit (either it alone or maybe multiple together somehow)
// Then reset the file (to undo previous modifications)
// THEN change the file
// In addition add a -undo flag that will ONLY reset the files without changing them
// Only for the ones matched by glob
// ^ important because binary files would fuck us up
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <pattern> <lua_expression> <...files_or_globs>\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage: %s [options] <pattern> <lua_expression> <...files_or_globs>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nOptions:\n") fmt.Fprintf(os.Stderr, "\nOptions:\n")
fmt.Fprintf(os.Stderr, " -json\n")
fmt.Fprintf(os.Stderr, " Process JSON files\n")
fmt.Fprintf(os.Stderr, " -xml\n")
fmt.Fprintf(os.Stderr, " Process XML files\n")
fmt.Fprintf(os.Stderr, " -git\n") fmt.Fprintf(os.Stderr, " -git\n")
fmt.Fprintf(os.Stderr, " Use git to manage files\n") fmt.Fprintf(os.Stderr, " Use git to manage files\n")
fmt.Fprintf(os.Stderr, " -reset\n") fmt.Fprintf(os.Stderr, " -reset\n")
fmt.Fprintf(os.Stderr, " Reset files to their original state\n") fmt.Fprintf(os.Stderr, " Reset files to their original state\n")
fmt.Fprintf(os.Stderr, " -loglevel string\n") fmt.Fprintf(os.Stderr, " -loglevel string\n")
fmt.Fprintf(os.Stderr, " Set logging level: ERROR, WARNING, INFO, DEBUG, TRACE (default \"INFO\")\n") fmt.Fprintf(os.Stderr, " Set logging level: ERROR, WARNING, INFO, DEBUG, TRACE (default \"INFO\")\n")
fmt.Fprintf(os.Stderr, " -mode string\n")
fmt.Fprintf(os.Stderr, " Processing mode: regex, xml, json (default \"regex\")\n")
fmt.Fprintf(os.Stderr, "\nExamples:\n") fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " Regex mode (default):\n") fmt.Fprintf(os.Stderr, " Regex mode (default):\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\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " XML mode:\n")
fmt.Fprintf(os.Stderr, " %s -xml \"//value\" \"*1.5\" data.xml\n", os.Args[0])
fmt.Fprintf(os.Stderr, " JSON mode:\n")
fmt.Fprintf(os.Stderr, " %s -json \"$.items[*].value\" \"*1.5\" data.json\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, "\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, " 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, " 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, " is_number(str) checks if a string is numeric\n")
fmt.Fprintf(os.Stderr, " For XML and JSON, the captured values are exposed as 'v', which can be of any type we capture (string, number, table).\n")
fmt.Fprintf(os.Stderr, " If expression starts with an operator like *, /, +, -, =, etc., v1 is automatically prepended\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, " 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") fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
} }
flag.Parse() flag.Parse()
// Initialize logger with the specified log level // Initialize logger with the specified log level
@@ -98,6 +63,7 @@ func main() {
logger.Info("Initializing with log level: %s", level.String()) logger.Info("Initializing with log level: %s", level.String())
args := flag.Args() args := flag.Args()
// Cannot reset without git, right?
if *resetFlag { if *resetFlag {
*gitFlag = true *gitFlag = true
} }
@@ -108,7 +74,6 @@ func main() {
return return
} }
// Get the appropriate pattern and expression based on mode
var pattern, luaExpr string var pattern, luaExpr string
var filePatterns []string var filePatterns []string
@@ -116,13 +81,6 @@ func main() {
luaExpr = args[1] luaExpr = args[1]
filePatterns = args[2:] filePatterns = args[2:]
// Prepare the Lua expression
originalLuaExpr := luaExpr
luaExpr = processor.BuildLuaScript(luaExpr)
if originalLuaExpr != luaExpr {
logger.Debug("Transformed Lua expression from %q to %q", originalLuaExpr, luaExpr)
}
if *gitFlag { if *gitFlag {
logger.Info("Git integration enabled, setting up git repository") logger.Info("Git integration enabled, setting up git repository")
err := setupGit() err := setupGit()
@@ -166,14 +124,6 @@ func main() {
// Create the processor based on mode // Create the processor based on mode
var proc processor.Processor var proc processor.Processor
switch { switch {
case *xmlFlag:
proc = &processor.XMLProcessor{}
logger.Info("Starting XML modifier with XPath %q, expression %q on %d files",
pattern, luaExpr, len(files))
case *jsonFlag:
proc = &processor.JSONProcessor{}
logger.Info("Starting JSON modifier with JSONPath %q, expression %q on %d files",
pattern, luaExpr, len(files))
default: default:
proc = &processor.RegexProcessor{} proc = &processor.RegexProcessor{}
logger.Info("Starting regex modifier with pattern %q, expression %q on %d files", logger.Info("Starting regex modifier with pattern %q, expression %q on %d files",

View File

@@ -99,7 +99,7 @@ func Process(p Processor, filename string, pattern string, luaExpr string) (int,
} }
// Process the content // Process the content
logger.Debug("Starting content processing with %s processor", getProcessorType(p)) logger.Debug("Starting content processing")
modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr) modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr)
if err != nil { if err != nil {
logger.Error("Processing error: %v", err) logger.Error("Processing error: %v", err)
@@ -178,20 +178,6 @@ func detectFileType(filename string, content string) string {
} }
} }
// Helper function to get processor type name
func getProcessorType(p Processor) string {
switch p.(type) {
case *RegexProcessor:
return "Regex"
case *XMLProcessor:
return "XML"
case *JSONProcessor:
return "JSON"
default:
return "Unknown"
}
}
// ToLua converts a struct or map to a Lua table recursively // ToLua converts a struct or map to a Lua table recursively
func ToLua(L *lua.LState, data interface{}) (lua.LValue, error) { func ToLua(L *lua.LState, data interface{}) (lua.LValue, error) {
switch v := data.(type) { switch v := data.(type) {