Compare commits
	
		
			41 Commits
		
	
	
		
			v3.3.4
			...
			bff7cc2a27
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| bff7cc2a27 | |||
| ff30b00e71 | |||
| e1eb5eeaa6 | |||
| 2a2e11d8e0 | |||
| 6eb4f31127 | |||
| 4b58e00c26 | |||
| 8ffd8af13c | |||
| 67861d4455 | |||
| 299e6d8bfe | |||
| 388822e90a | |||
| 91993b4548 | |||
| bb69558aaa | |||
| 052c670627 | |||
| 67fd215d0e | |||
| 9ecbbff6fa | |||
| 774ac0f0ca | |||
| b785d24a08 | |||
| 22f991e72e | |||
| 5518b27663 | |||
| 0b899dea2c | |||
| 3424fea8ad | |||
| ddc1d83d58 | |||
| 4b0a85411d | |||
| 46e871b626 | |||
| 258dcc88e7 | |||
| 75bf449bed | |||
| 58586395fb | |||
| c5a68af5e6 | |||
| b4c0284734 | |||
| c5d1dad8de | |||
| 4ff2ee80ee | |||
| 633eebfd2a | |||
| 5a31703840 | |||
| 162d0c758d | |||
| 14d64495b6 | |||
| fe6e97e832 | |||
| 35b3d8b099 | |||
| 2e3e958e15 | |||
| 955afc4295 | |||
| 2c487bc443 | |||
| b77224176b | 
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,2 +1,4 @@
 | 
				
			|||||||
*.exe
 | 
					*.exe
 | 
				
			||||||
.qodo
 | 
					.qodo
 | 
				
			||||||
 | 
					*.sqlite
 | 
				
			||||||
 | 
					testfiles
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										70
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										70
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							@@ -12,11 +12,23 @@
 | 
				
			|||||||
			"program": "${workspaceFolder}",
 | 
								"program": "${workspaceFolder}",
 | 
				
			||||||
			"cwd": "C:/Users/Administrator/Seafile/Games-Barotrauma",
 | 
								"cwd": "C:/Users/Administrator/Seafile/Games-Barotrauma",
 | 
				
			||||||
			"args": [
 | 
								"args": [
 | 
				
			||||||
				"loglevel",
 | 
									"-loglevel",
 | 
				
			||||||
				"trace",
 | 
									"trace",
 | 
				
			||||||
				"(?-s)LightComponent!anyrange=\"(!num)\"",
 | 
									"-cook",
 | 
				
			||||||
				"*4",
 | 
									"*.yml",
 | 
				
			||||||
				"**/Outpost*.xml"
 | 
								]
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								"name": "Launch Package (Payday 2)",
 | 
				
			||||||
 | 
								"type": "go",
 | 
				
			||||||
 | 
								"request": "launch",
 | 
				
			||||||
 | 
								"mode": "auto",
 | 
				
			||||||
 | 
								"program": "${workspaceFolder}",
 | 
				
			||||||
 | 
								"cwd": "C:/Users/Administrator/Seafile/Games-Payday2",
 | 
				
			||||||
 | 
								"args": [
 | 
				
			||||||
 | 
									"-loglevel",
 | 
				
			||||||
 | 
									"trace",
 | 
				
			||||||
 | 
									"*.yml",
 | 
				
			||||||
			]
 | 
								]
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@@ -33,6 +45,28 @@
 | 
				
			|||||||
				"cookassistant.yml",
 | 
									"cookassistant.yml",
 | 
				
			||||||
			]
 | 
								]
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								"name": "Launch Package (Quasimorph cookfile)",
 | 
				
			||||||
 | 
								"type": "go",
 | 
				
			||||||
 | 
								"request": "launch",
 | 
				
			||||||
 | 
								"mode": "auto",
 | 
				
			||||||
 | 
								"program": "${workspaceFolder}",
 | 
				
			||||||
 | 
								"cwd": "C:/Users/Administrator/Seafile/Games-Quasimorph",
 | 
				
			||||||
 | 
								"args": [
 | 
				
			||||||
 | 
									"cook.yml",
 | 
				
			||||||
 | 
								]
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								"name": "Launch Package (Rimworld cookfile)",
 | 
				
			||||||
 | 
								"type": "go",
 | 
				
			||||||
 | 
								"request": "launch",
 | 
				
			||||||
 | 
								"mode": "auto",
 | 
				
			||||||
 | 
								"program": "${workspaceFolder}",
 | 
				
			||||||
 | 
								"cwd": "C:/Users/Administrator/Seafile/Games-Rimworld/294100",
 | 
				
			||||||
 | 
								"args": [
 | 
				
			||||||
 | 
									"cookVehicles.yml",
 | 
				
			||||||
 | 
								]
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			"name": "Launch Package (Workspace)",
 | 
								"name": "Launch Package (Workspace)",
 | 
				
			||||||
			"type": "go",
 | 
								"type": "go",
 | 
				
			||||||
@@ -40,11 +74,29 @@
 | 
				
			|||||||
			"mode": "auto",
 | 
								"mode": "auto",
 | 
				
			||||||
			"program": "${workspaceFolder}",
 | 
								"program": "${workspaceFolder}",
 | 
				
			||||||
			"args": [
 | 
								"args": [
 | 
				
			||||||
				"-loglevel",
 | 
									"tester.yml",
 | 
				
			||||||
				"trace",
 | 
								]
 | 
				
			||||||
				"(?-s)LightComponent!anyrange=\"(!num)\"",
 | 
							},
 | 
				
			||||||
				"*4",
 | 
							{
 | 
				
			||||||
				"**/Outpost*.xml"
 | 
								"name": "Launch Package (Avorion)",
 | 
				
			||||||
 | 
								"type": "go",
 | 
				
			||||||
 | 
								"request": "launch",
 | 
				
			||||||
 | 
								"mode": "auto",
 | 
				
			||||||
 | 
								"program": "${workspaceFolder}",
 | 
				
			||||||
 | 
								"cwd": "C:/Users/Administrator/Seafile/Games-Avorion/Avorion",
 | 
				
			||||||
 | 
								"args": [
 | 
				
			||||||
 | 
									"*.yml",
 | 
				
			||||||
 | 
								]
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								"name": "Launch Package (Minecraft)",
 | 
				
			||||||
 | 
								"type": "go",
 | 
				
			||||||
 | 
								"request": "launch",
 | 
				
			||||||
 | 
								"mode": "auto",
 | 
				
			||||||
 | 
								"program": "${workspaceFolder}",
 | 
				
			||||||
 | 
								"cwd": "C:/Users/Administrator/Seafile/Games-Minecraft",
 | 
				
			||||||
 | 
								"args": [
 | 
				
			||||||
 | 
									"cook_tacz.yml",
 | 
				
			||||||
			]
 | 
								]
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	]
 | 
						]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,9 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"modify/logger"
 | 
					 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"modify/utils"
 | 
						"cook/utils"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										42
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,38 +1,32 @@
 | 
				
			|||||||
module modify
 | 
					module cook
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.24.1
 | 
					go 1.23.2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
 | 
						git.site.quack-lab.dev/dave/cylogger v1.3.0
 | 
				
			||||||
	github.com/bmatcuk/doublestar/v4 v4.8.1
 | 
						github.com/bmatcuk/doublestar/v4 v4.8.1
 | 
				
			||||||
	github.com/stretchr/testify v1.10.0
 | 
						github.com/stretchr/testify v1.10.0
 | 
				
			||||||
	github.com/yuin/gopher-lua v1.1.1
 | 
						github.com/yuin/gopher-lua v1.1.1
 | 
				
			||||||
	gopkg.in/yaml.v3 v3.0.1
 | 
						gopkg.in/yaml.v3 v3.0.1
 | 
				
			||||||
 | 
						gorm.io/gorm v1.30.0
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	dario.cat/mergo v1.0.0 // indirect
 | 
					 | 
				
			||||||
	github.com/Microsoft/go-winio v0.6.2 // indirect
 | 
					 | 
				
			||||||
	github.com/ProtonMail/go-crypto v1.1.5 // indirect
 | 
					 | 
				
			||||||
	github.com/cloudflare/circl v1.6.0 // indirect
 | 
					 | 
				
			||||||
	github.com/cyphar/filepath-securejoin v0.4.1 // indirect
 | 
					 | 
				
			||||||
	github.com/davecgh/go-spew v1.1.1 // indirect
 | 
						github.com/davecgh/go-spew v1.1.1 // indirect
 | 
				
			||||||
	github.com/emirpasic/gods v1.18.1 // indirect
 | 
						github.com/google/go-cmp v0.6.0 // indirect
 | 
				
			||||||
	github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
 | 
						github.com/hexops/valast v1.5.0 // indirect
 | 
				
			||||||
	github.com/go-git/go-billy/v5 v5.6.2 // indirect
 | 
						github.com/jinzhu/inflection v1.0.0 // indirect
 | 
				
			||||||
	github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
 | 
						github.com/jinzhu/now v1.1.5 // indirect
 | 
				
			||||||
	github.com/kevinburke/ssh_config v1.2.0 // indirect
 | 
						github.com/kr/pretty v0.3.1 // indirect
 | 
				
			||||||
	github.com/pjbgf/sha1cd v0.3.2 // indirect
 | 
						github.com/mattn/go-sqlite3 v1.14.22 // indirect
 | 
				
			||||||
	github.com/pmezard/go-difflib v1.0.0 // indirect
 | 
						github.com/pmezard/go-difflib v1.0.0 // indirect
 | 
				
			||||||
	github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
 | 
						github.com/rogpeppe/go-internal v1.14.1 // indirect
 | 
				
			||||||
	github.com/skeema/knownhosts v1.3.1 // indirect
 | 
						golang.org/x/mod v0.21.0 // indirect
 | 
				
			||||||
	github.com/xanzy/ssh-agent v0.3.3 // indirect
 | 
						golang.org/x/sync v0.11.0 // indirect
 | 
				
			||||||
	golang.org/x/crypto v0.35.0 // indirect
 | 
						golang.org/x/text v0.22.0 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.30.0 // indirect
 | 
						golang.org/x/tools v0.26.0 // indirect
 | 
				
			||||||
	gopkg.in/warnings.v0 v0.1.2 // indirect
 | 
						gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
 | 
				
			||||||
 | 
						mvdan.cc/gofumpt v0.4.0 // indirect
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require gorm.io/driver/sqlite v1.6.0
 | 
				
			||||||
	github.com/go-git/go-git/v5 v5.14.0
 | 
					 | 
				
			||||||
	github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
 | 
					 | 
				
			||||||
	golang.org/x/net v0.35.0 // indirect
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										115
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										115
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,106 +1,59 @@
 | 
				
			|||||||
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
 | 
					git.site.quack-lab.dev/dave/cylogger v1.3.0 h1:eTWPUD+ThVi8kGIsRcE0XDeoH3yFb5miFEODyKUdWJw=
 | 
				
			||||||
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
 | 
					git.site.quack-lab.dev/dave/cylogger v1.3.0/go.mod h1:wctgZplMvroA4X6p8f4B/LaCKtiBcT1Pp+L14kcS8jk=
 | 
				
			||||||
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
 | 
					 | 
				
			||||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
 | 
					 | 
				
			||||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
 | 
					 | 
				
			||||||
github.com/ProtonMail/go-crypto v1.1.5 h1:eoAQfK2dwL+tFSFpr7TbOaPNUbPiJj4fLYwwGE1FQO4=
 | 
					 | 
				
			||||||
github.com/ProtonMail/go-crypto v1.1.5/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
 | 
					 | 
				
			||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
 | 
					 | 
				
			||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
 | 
					 | 
				
			||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
 | 
					 | 
				
			||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
 | 
					 | 
				
			||||||
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
 | 
					github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
 | 
				
			||||||
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
 | 
					github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
 | 
				
			||||||
github.com/cloudflare/circl v1.6.0 h1:cr5JKic4HI+LkINy2lg3W2jF8sHCVTBncJr5gIIq7qk=
 | 
					github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
 | 
				
			||||||
github.com/cloudflare/circl v1.6.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
 | 
					 | 
				
			||||||
github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
 | 
					 | 
				
			||||||
github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
 | 
					 | 
				
			||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
					 | 
				
			||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 | 
					github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 | 
				
			||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
					github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
				
			||||||
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
 | 
					github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
 | 
				
			||||||
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
 | 
					github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
 | 
				
			||||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
 | 
					github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
 | 
				
			||||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
 | 
					github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 | 
				
			||||||
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
 | 
					github.com/hexops/autogold v0.8.1 h1:wvyd/bAJ+Dy+DcE09BoLk6r4Fa5R5W+O+GUzmR985WM=
 | 
				
			||||||
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
 | 
					github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY=
 | 
				
			||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
 | 
					github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
 | 
				
			||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
 | 
					github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
 | 
				
			||||||
github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UNbRM=
 | 
					github.com/hexops/valast v1.5.0 h1:FBTuvVi0wjTngtXJRZXMbkN/Dn6DgsUsBwch2DUJU8Y=
 | 
				
			||||||
github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU=
 | 
					github.com/hexops/valast v1.5.0/go.mod h1:Jcy1pNH7LNraVaAZDLyv21hHg2WBv9Nf9FL6fGxU7o4=
 | 
				
			||||||
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
 | 
					github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
 | 
				
			||||||
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
 | 
					github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
 | 
				
			||||||
github.com/go-git/go-git/v5 v5.14.0 h1:/MD3lCrGjCen5WfEAzKg00MJJffKhC8gzS80ycmCi60=
 | 
					github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
 | 
				
			||||||
github.com/go-git/go-git/v5 v5.14.0/go.mod h1:Z5Xhoia5PcWA3NF8vRLURn9E5FRhSl7dGj9ItW3Wk5k=
 | 
					github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
 | 
				
			||||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
 | 
					github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
 | 
				
			||||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
 | 
					 | 
				
			||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
 | 
					 | 
				
			||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
 | 
					 | 
				
			||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
 | 
					 | 
				
			||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
 | 
					 | 
				
			||||||
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
 | 
					 | 
				
			||||||
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
 | 
					 | 
				
			||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 | 
					 | 
				
			||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
 | 
					github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
 | 
				
			||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
 | 
					github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
 | 
				
			||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 | 
					github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 | 
				
			||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 | 
					github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 | 
				
			||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 | 
					github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 | 
				
			||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 | 
					github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 | 
				
			||||||
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
 | 
					github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
 | 
				
			||||||
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
 | 
					github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
 | 
				
			||||||
github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
 | 
					github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
 | 
				
			||||||
github.com/pjbgf/sha1cd v0.3.2/go.mod h1:zQWigSxVmsHEZow5qaLtPYxpcKMMQpa09ixqBxuCS6A=
 | 
					 | 
				
			||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
 | 
					 | 
				
			||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 | 
					 | 
				
			||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 | 
					github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 | 
				
			||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
					github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
				
			||||||
 | 
					github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
 | 
				
			||||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
 | 
					github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
 | 
				
			||||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
 | 
					github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
 | 
				
			||||||
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
 | 
					 | 
				
			||||||
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
 | 
					 | 
				
			||||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
 | 
					 | 
				
			||||||
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
 | 
					 | 
				
			||||||
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
 | 
					 | 
				
			||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
					 | 
				
			||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 | 
					 | 
				
			||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 | 
					 | 
				
			||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
 | 
					github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
 | 
				
			||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 | 
					github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 | 
				
			||||||
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
 | 
					 | 
				
			||||||
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
 | 
					 | 
				
			||||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
 | 
					github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
 | 
				
			||||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
 | 
					github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
 | 
				
			||||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 | 
					golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
 | 
				
			||||||
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
 | 
					golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
 | 
				
			||||||
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
 | 
					golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
 | 
				
			||||||
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
 | 
					golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
 | 
				
			||||||
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
 | 
					 | 
				
			||||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
 | 
					 | 
				
			||||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
 | 
					 | 
				
			||||||
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
 | 
					 | 
				
			||||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 | 
					 | 
				
			||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 | 
					 | 
				
			||||||
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
 | 
					 | 
				
			||||||
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
 | 
					 | 
				
			||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 | 
					 | 
				
			||||||
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
 | 
					golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
 | 
				
			||||||
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
 | 
					golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
 | 
				
			||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
					golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
 | 
				
			||||||
 | 
					golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
 | 
				
			||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
					gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
				
			||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
					 | 
				
			||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
 | 
					gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
 | 
				
			||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
 | 
					gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
 | 
				
			||||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
 | 
					 | 
				
			||||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
 | 
					 | 
				
			||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
					 | 
				
			||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 | 
					 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
					gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
					gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
				
			||||||
 | 
					gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
 | 
				
			||||||
 | 
					gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
 | 
				
			||||||
 | 
					gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
 | 
				
			||||||
 | 
					gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
 | 
				
			||||||
 | 
					mvdan.cc/gofumpt v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
 | 
				
			||||||
 | 
					mvdan.cc/gofumpt v0.4.0/go.mod h1:PljLOHDeZqgS8opHRKLzp2It2VBuSdteAgqUfzMTxlQ=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										465
									
								
								logger/logger.go
									
									
									
									
									
								
							
							
						
						
									
										465
									
								
								logger/logger.go
									
									
									
									
									
								
							@@ -1,465 +0,0 @@
 | 
				
			|||||||
package logger
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"bytes"
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"io"
 | 
					 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"path/filepath"
 | 
					 | 
				
			||||||
	"runtime"
 | 
					 | 
				
			||||||
	"strconv"
 | 
					 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
	"sync"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// LogLevel defines the severity of log messages
 | 
					 | 
				
			||||||
type LogLevel int
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const (
 | 
					 | 
				
			||||||
	// LevelError is for critical errors that should always be displayed
 | 
					 | 
				
			||||||
	LevelError LogLevel = iota
 | 
					 | 
				
			||||||
	// LevelWarning is for important warnings
 | 
					 | 
				
			||||||
	LevelWarning
 | 
					 | 
				
			||||||
	// LevelInfo is for informational messages
 | 
					 | 
				
			||||||
	LevelInfo
 | 
					 | 
				
			||||||
	// LevelDebug is for detailed debugging information
 | 
					 | 
				
			||||||
	LevelDebug
 | 
					 | 
				
			||||||
	// LevelTrace is for very detailed tracing information
 | 
					 | 
				
			||||||
	LevelTrace
 | 
					 | 
				
			||||||
	// LevelLua is specifically for output from Lua scripts
 | 
					 | 
				
			||||||
	LevelLua
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var levelNames = map[LogLevel]string{
 | 
					 | 
				
			||||||
	LevelError:   "ERROR",
 | 
					 | 
				
			||||||
	LevelWarning: "WARNING",
 | 
					 | 
				
			||||||
	LevelInfo:    "INFO",
 | 
					 | 
				
			||||||
	LevelDebug:   "DEBUG",
 | 
					 | 
				
			||||||
	LevelTrace:   "TRACE",
 | 
					 | 
				
			||||||
	LevelLua:     "LUA",
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var levelColors = map[LogLevel]string{
 | 
					 | 
				
			||||||
	LevelError:   "\033[1;31m", // Bold Red
 | 
					 | 
				
			||||||
	LevelWarning: "\033[1;33m", // Bold Yellow
 | 
					 | 
				
			||||||
	LevelInfo:    "\033[1;32m", // Bold Green
 | 
					 | 
				
			||||||
	LevelDebug:   "\033[1;36m", // Bold Cyan
 | 
					 | 
				
			||||||
	LevelTrace:   "\033[1;35m", // Bold Magenta
 | 
					 | 
				
			||||||
	LevelLua:     "\033[1;34m", // Bold Blue
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ResetColor is the ANSI code to reset text color
 | 
					 | 
				
			||||||
const ResetColor = "\033[0m"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Logger is our custom logger with level support
 | 
					 | 
				
			||||||
type Logger struct {
 | 
					 | 
				
			||||||
	mu            sync.Mutex
 | 
					 | 
				
			||||||
	out           io.Writer
 | 
					 | 
				
			||||||
	currentLevel  LogLevel
 | 
					 | 
				
			||||||
	prefix        string
 | 
					 | 
				
			||||||
	flag          int
 | 
					 | 
				
			||||||
	useColors     bool
 | 
					 | 
				
			||||||
	callerOffset  int
 | 
					 | 
				
			||||||
	defaultFields map[string]interface{}
 | 
					 | 
				
			||||||
	showGoroutine bool
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var (
 | 
					 | 
				
			||||||
	// DefaultLogger is the global logger instance
 | 
					 | 
				
			||||||
	DefaultLogger *Logger
 | 
					 | 
				
			||||||
	// defaultLogLevel is the default log level if not specified
 | 
					 | 
				
			||||||
	defaultLogLevel = LevelInfo
 | 
					 | 
				
			||||||
	// Global mutex for DefaultLogger initialization
 | 
					 | 
				
			||||||
	initMutex sync.Mutex
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ParseLevel converts a string log level to LogLevel
 | 
					 | 
				
			||||||
func ParseLevel(levelStr string) LogLevel {
 | 
					 | 
				
			||||||
	switch strings.ToUpper(levelStr) {
 | 
					 | 
				
			||||||
	case "ERROR":
 | 
					 | 
				
			||||||
		return LevelError
 | 
					 | 
				
			||||||
	case "WARNING", "WARN":
 | 
					 | 
				
			||||||
		return LevelWarning
 | 
					 | 
				
			||||||
	case "INFO":
 | 
					 | 
				
			||||||
		return LevelInfo
 | 
					 | 
				
			||||||
	case "DEBUG":
 | 
					 | 
				
			||||||
		return LevelDebug
 | 
					 | 
				
			||||||
	case "TRACE":
 | 
					 | 
				
			||||||
		return LevelTrace
 | 
					 | 
				
			||||||
	case "LUA":
 | 
					 | 
				
			||||||
		return LevelLua
 | 
					 | 
				
			||||||
	default:
 | 
					 | 
				
			||||||
		return defaultLogLevel
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// String returns the string representation of the log level
 | 
					 | 
				
			||||||
func (l LogLevel) String() string {
 | 
					 | 
				
			||||||
	if name, ok := levelNames[l]; ok {
 | 
					 | 
				
			||||||
		return name
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return fmt.Sprintf("Level(%d)", l)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// New creates a new Logger instance
 | 
					 | 
				
			||||||
func New(out io.Writer, prefix string, flag int) *Logger {
 | 
					 | 
				
			||||||
	return &Logger{
 | 
					 | 
				
			||||||
		out:           out,
 | 
					 | 
				
			||||||
		currentLevel:  defaultLogLevel,
 | 
					 | 
				
			||||||
		prefix:        prefix,
 | 
					 | 
				
			||||||
		flag:          flag,
 | 
					 | 
				
			||||||
		useColors:     true,
 | 
					 | 
				
			||||||
		callerOffset:  0,
 | 
					 | 
				
			||||||
		defaultFields: make(map[string]interface{}),
 | 
					 | 
				
			||||||
		showGoroutine: true,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Init initializes the DefaultLogger
 | 
					 | 
				
			||||||
func Init(level LogLevel) {
 | 
					 | 
				
			||||||
	initMutex.Lock()
 | 
					 | 
				
			||||||
	defer initMutex.Unlock()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		DefaultLogger = New(os.Stdout, "", log.Lmicroseconds|log.Lshortfile)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.SetLevel(level)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SetLevel sets the current log level
 | 
					 | 
				
			||||||
func (l *Logger) SetLevel(level LogLevel) {
 | 
					 | 
				
			||||||
	l.mu.Lock()
 | 
					 | 
				
			||||||
	defer l.mu.Unlock()
 | 
					 | 
				
			||||||
	l.currentLevel = level
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// GetLevel returns the current log level
 | 
					 | 
				
			||||||
func (l *Logger) GetLevel() LogLevel {
 | 
					 | 
				
			||||||
	l.mu.Lock()
 | 
					 | 
				
			||||||
	defer l.mu.Unlock()
 | 
					 | 
				
			||||||
	return l.currentLevel
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SetCallerOffset sets the caller offset for correct file and line reporting
 | 
					 | 
				
			||||||
func (l *Logger) SetCallerOffset(offset int) {
 | 
					 | 
				
			||||||
	l.mu.Lock()
 | 
					 | 
				
			||||||
	defer l.mu.Unlock()
 | 
					 | 
				
			||||||
	l.callerOffset = offset
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SetShowGoroutine sets whether to include goroutine ID in log messages
 | 
					 | 
				
			||||||
func (l *Logger) SetShowGoroutine(show bool) {
 | 
					 | 
				
			||||||
	l.mu.Lock()
 | 
					 | 
				
			||||||
	defer l.mu.Unlock()
 | 
					 | 
				
			||||||
	l.showGoroutine = show
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ShowGoroutine returns whether goroutine ID is included in log messages
 | 
					 | 
				
			||||||
func (l *Logger) ShowGoroutine() bool {
 | 
					 | 
				
			||||||
	l.mu.Lock()
 | 
					 | 
				
			||||||
	defer l.mu.Unlock()
 | 
					 | 
				
			||||||
	return l.showGoroutine
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// WithField adds a field to the logger's context
 | 
					 | 
				
			||||||
func (l *Logger) WithField(key string, value interface{}) *Logger {
 | 
					 | 
				
			||||||
	newLogger := &Logger{
 | 
					 | 
				
			||||||
		out:           l.out,
 | 
					 | 
				
			||||||
		currentLevel:  l.currentLevel,
 | 
					 | 
				
			||||||
		prefix:        l.prefix,
 | 
					 | 
				
			||||||
		flag:          l.flag,
 | 
					 | 
				
			||||||
		useColors:     l.useColors,
 | 
					 | 
				
			||||||
		callerOffset:  l.callerOffset,
 | 
					 | 
				
			||||||
		defaultFields: make(map[string]interface{}),
 | 
					 | 
				
			||||||
		showGoroutine: l.showGoroutine,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Copy existing fields
 | 
					 | 
				
			||||||
	for k, v := range l.defaultFields {
 | 
					 | 
				
			||||||
		newLogger.defaultFields[k] = v
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Add new field
 | 
					 | 
				
			||||||
	newLogger.defaultFields[key] = value
 | 
					 | 
				
			||||||
	return newLogger
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// WithFields adds multiple fields to the logger's context
 | 
					 | 
				
			||||||
func (l *Logger) WithFields(fields map[string]interface{}) *Logger {
 | 
					 | 
				
			||||||
	newLogger := &Logger{
 | 
					 | 
				
			||||||
		out:           l.out,
 | 
					 | 
				
			||||||
		currentLevel:  l.currentLevel,
 | 
					 | 
				
			||||||
		prefix:        l.prefix,
 | 
					 | 
				
			||||||
		flag:          l.flag,
 | 
					 | 
				
			||||||
		useColors:     l.useColors,
 | 
					 | 
				
			||||||
		callerOffset:  l.callerOffset,
 | 
					 | 
				
			||||||
		defaultFields: make(map[string]interface{}),
 | 
					 | 
				
			||||||
		showGoroutine: l.showGoroutine,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Copy existing fields
 | 
					 | 
				
			||||||
	for k, v := range l.defaultFields {
 | 
					 | 
				
			||||||
		newLogger.defaultFields[k] = v
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Add new fields
 | 
					 | 
				
			||||||
	for k, v := range fields {
 | 
					 | 
				
			||||||
		newLogger.defaultFields[k] = v
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return newLogger
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// GetGoroutineID extracts the goroutine ID from the runtime stack
 | 
					 | 
				
			||||||
func GetGoroutineID() string {
 | 
					 | 
				
			||||||
	buf := make([]byte, 64)
 | 
					 | 
				
			||||||
	n := runtime.Stack(buf, false)
 | 
					 | 
				
			||||||
	// Format of first line is "goroutine N [state]:"
 | 
					 | 
				
			||||||
	// We only need the N part
 | 
					 | 
				
			||||||
	buf = buf[:n]
 | 
					 | 
				
			||||||
	idField := bytes.Fields(bytes.Split(buf, []byte{':'})[0])[1]
 | 
					 | 
				
			||||||
	return string(idField)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// formatMessage formats a log message with level, time, file, and line information
 | 
					 | 
				
			||||||
func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{}) string {
 | 
					 | 
				
			||||||
	var msg string
 | 
					 | 
				
			||||||
	if len(args) > 0 {
 | 
					 | 
				
			||||||
		msg = fmt.Sprintf(format, args...)
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		msg = format
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Format default fields if any
 | 
					 | 
				
			||||||
	var fields string
 | 
					 | 
				
			||||||
	if len(l.defaultFields) > 0 {
 | 
					 | 
				
			||||||
		var pairs []string
 | 
					 | 
				
			||||||
		for k, v := range l.defaultFields {
 | 
					 | 
				
			||||||
			pairs = append(pairs, fmt.Sprintf("%s=%v", k, v))
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		fields = " " + strings.Join(pairs, " ")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var levelColor, resetColor string
 | 
					 | 
				
			||||||
	if l.useColors {
 | 
					 | 
				
			||||||
		levelColor = levelColors[level]
 | 
					 | 
				
			||||||
		resetColor = ResetColor
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var caller string
 | 
					 | 
				
			||||||
	if l.flag&log.Lshortfile != 0 || l.flag&log.Llongfile != 0 {
 | 
					 | 
				
			||||||
		// Find the actual caller by scanning up the stack
 | 
					 | 
				
			||||||
		// until we find a function outside the logger package
 | 
					 | 
				
			||||||
		var file string
 | 
					 | 
				
			||||||
		var line int
 | 
					 | 
				
			||||||
		var ok bool
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Start at a reasonable depth and scan up to 10 frames
 | 
					 | 
				
			||||||
		for depth := 4; depth < 15; depth++ {
 | 
					 | 
				
			||||||
			_, file, line, ok = runtime.Caller(depth)
 | 
					 | 
				
			||||||
			if !ok {
 | 
					 | 
				
			||||||
				break
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			// If the caller is not in the logger package, we found our caller
 | 
					 | 
				
			||||||
			if !strings.Contains(file, "logger/logger.go") {
 | 
					 | 
				
			||||||
				break
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if !ok {
 | 
					 | 
				
			||||||
			file = "???"
 | 
					 | 
				
			||||||
			line = 0
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if l.flag&log.Lshortfile != 0 {
 | 
					 | 
				
			||||||
			file = filepath.Base(file)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		caller = fmt.Sprintf("%-25s ", file+":"+strconv.Itoa(line))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Format the timestamp with fixed width
 | 
					 | 
				
			||||||
	var timeStr string
 | 
					 | 
				
			||||||
	if l.flag&(log.Ldate|log.Ltime|log.Lmicroseconds) != 0 {
 | 
					 | 
				
			||||||
		t := time.Now()
 | 
					 | 
				
			||||||
		if l.flag&log.Ldate != 0 {
 | 
					 | 
				
			||||||
			timeStr += fmt.Sprintf("%04d/%02d/%02d ", t.Year(), t.Month(), t.Day())
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if l.flag&(log.Ltime|log.Lmicroseconds) != 0 {
 | 
					 | 
				
			||||||
			timeStr += fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
 | 
					 | 
				
			||||||
			if l.flag&log.Lmicroseconds != 0 {
 | 
					 | 
				
			||||||
				timeStr += fmt.Sprintf(".%06d", t.Nanosecond()/1000)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		timeStr = fmt.Sprintf("%-15s ", timeStr)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Add goroutine ID if enabled, with fixed width
 | 
					 | 
				
			||||||
	var goroutineStr string
 | 
					 | 
				
			||||||
	if l.showGoroutine {
 | 
					 | 
				
			||||||
		goroutineID := GetGoroutineID()
 | 
					 | 
				
			||||||
		goroutineStr = fmt.Sprintf("[g:%-4s] ", goroutineID)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Create a colored level indicator with both brackets colored
 | 
					 | 
				
			||||||
	levelStr := fmt.Sprintf("%s[%s]%s", levelColor, levelNames[level], levelColor)
 | 
					 | 
				
			||||||
	// Add a space after the level and before the reset color
 | 
					 | 
				
			||||||
	levelColumn := fmt.Sprintf("%s %s", levelStr, resetColor)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return fmt.Sprintf("%s%s%s%s%s%s%s\n",
 | 
					 | 
				
			||||||
		l.prefix, timeStr, caller, goroutineStr, levelColumn, msg, fields)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// log logs a message at the specified level
 | 
					 | 
				
			||||||
func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	// Always show LUA level logs regardless of the current log level
 | 
					 | 
				
			||||||
	if level != LevelLua && level > l.currentLevel {
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	l.mu.Lock()
 | 
					 | 
				
			||||||
	defer l.mu.Unlock()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	msg := l.formatMessage(level, format, args...)
 | 
					 | 
				
			||||||
	fmt.Fprint(l.out, msg)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Error logs an error message
 | 
					 | 
				
			||||||
func (l *Logger) Error(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	l.log(LevelError, format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Warning logs a warning message
 | 
					 | 
				
			||||||
func (l *Logger) Warning(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	l.log(LevelWarning, format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Info logs an informational message
 | 
					 | 
				
			||||||
func (l *Logger) Info(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	l.log(LevelInfo, format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Debug logs a debug message
 | 
					 | 
				
			||||||
func (l *Logger) Debug(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	l.log(LevelDebug, format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Trace logs a trace message
 | 
					 | 
				
			||||||
func (l *Logger) Trace(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	l.log(LevelTrace, format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Lua logs a Lua message
 | 
					 | 
				
			||||||
func (l *Logger) Lua(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	l.log(LevelLua, format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Global log functions that use DefaultLogger
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Error logs an error message using the default logger
 | 
					 | 
				
			||||||
func Error(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.Error(format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Warning logs a warning message using the default logger
 | 
					 | 
				
			||||||
func Warning(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.Warning(format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Info logs an informational message using the default logger
 | 
					 | 
				
			||||||
func Info(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.Info(format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Debug logs a debug message using the default logger
 | 
					 | 
				
			||||||
func Debug(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.Debug(format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Trace logs a trace message using the default logger
 | 
					 | 
				
			||||||
func Trace(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.Trace(format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Lua logs a Lua message using the default logger
 | 
					 | 
				
			||||||
func Lua(format string, args ...interface{}) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.Lua(format, args...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// LogPanic logs a panic error and its stack trace
 | 
					 | 
				
			||||||
func LogPanic(r interface{}) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	stack := make([]byte, 4096)
 | 
					 | 
				
			||||||
	n := runtime.Stack(stack, false)
 | 
					 | 
				
			||||||
	DefaultLogger.Error("PANIC: %v\n%s", r, stack[:n])
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SetLevel sets the log level for the default logger
 | 
					 | 
				
			||||||
func SetLevel(level LogLevel) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(level)
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.SetLevel(level)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// GetLevel gets the log level for the default logger
 | 
					 | 
				
			||||||
func GetLevel() LogLevel {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return DefaultLogger.GetLevel()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// WithField returns a new logger with the field added to the default logger's context
 | 
					 | 
				
			||||||
func WithField(key string, value interface{}) *Logger {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return DefaultLogger.WithField(key, value)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// WithFields returns a new logger with the fields added to the default logger's context
 | 
					 | 
				
			||||||
func WithFields(fields map[string]interface{}) *Logger {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return DefaultLogger.WithFields(fields)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SetShowGoroutine enables or disables goroutine ID display in the default logger
 | 
					 | 
				
			||||||
func SetShowGoroutine(show bool) {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	DefaultLogger.SetShowGoroutine(show)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ShowGoroutine returns whether goroutine ID is included in default logger's messages
 | 
					 | 
				
			||||||
func ShowGoroutine() bool {
 | 
					 | 
				
			||||||
	if DefaultLogger == nil {
 | 
					 | 
				
			||||||
		Init(defaultLogLevel)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return DefaultLogger.ShowGoroutine()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,49 +0,0 @@
 | 
				
			|||||||
package logger
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"runtime/debug"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// PanicHandler handles a panic and logs it
 | 
					 | 
				
			||||||
func PanicHandler() {
 | 
					 | 
				
			||||||
	if r := recover(); r != nil {
 | 
					 | 
				
			||||||
		goroutineID := GetGoroutineID()
 | 
					 | 
				
			||||||
		stackTrace := debug.Stack()
 | 
					 | 
				
			||||||
		Error("PANIC in goroutine %s: %v\n%s", goroutineID, r, stackTrace)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SafeGo launches a goroutine with panic recovery
 | 
					 | 
				
			||||||
// Usage: logger.SafeGo(func() { ... your code ... })
 | 
					 | 
				
			||||||
func SafeGo(f func()) {
 | 
					 | 
				
			||||||
	go func() {
 | 
					 | 
				
			||||||
		defer PanicHandler()
 | 
					 | 
				
			||||||
		f()
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SafeGoWithArgs launches a goroutine with panic recovery and passes arguments
 | 
					 | 
				
			||||||
// Usage: logger.SafeGoWithArgs(func(arg1, arg2 interface{}) { ... }, "value1", 42)
 | 
					 | 
				
			||||||
func SafeGoWithArgs(f func(...interface{}), args ...interface{}) {
 | 
					 | 
				
			||||||
	go func() {
 | 
					 | 
				
			||||||
		defer PanicHandler()
 | 
					 | 
				
			||||||
		f(args...)
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SafeExec executes a function with panic recovery
 | 
					 | 
				
			||||||
// Useful for code that should not panic
 | 
					 | 
				
			||||||
func SafeExec(f func()) (err error) {
 | 
					 | 
				
			||||||
	defer func() {
 | 
					 | 
				
			||||||
		if r := recover(); r != nil {
 | 
					 | 
				
			||||||
			goroutineID := GetGoroutineID()
 | 
					 | 
				
			||||||
			stackTrace := debug.Stack()
 | 
					 | 
				
			||||||
			Error("PANIC in goroutine %s: %v\n%s", goroutineID, r, stackTrace)
 | 
					 | 
				
			||||||
			err = fmt.Errorf("panic recovered: %v", r)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	f()
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										622
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										622
									
								
								main.go
									
									
									
									
									
								
							@@ -1,32 +1,35 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"flag"
 | 
						"flag"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
						"sync/atomic"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"modify/processor"
 | 
						"cook/processor"
 | 
				
			||||||
	"modify/utils"
 | 
						"cook/utils"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/go-git/go-git/v5"
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"modify/logger"
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// mainLogger is a scoped logger for the main package.
 | 
				
			||||||
 | 
					var mainLogger = logger.Default.WithPrefix("main")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type GlobalStats struct {
 | 
					type GlobalStats struct {
 | 
				
			||||||
	TotalMatches            int
 | 
						TotalMatches            int64
 | 
				
			||||||
	TotalModifications      int
 | 
						TotalModifications      int64
 | 
				
			||||||
	ProcessedFiles          int
 | 
						ProcessedFiles          int64
 | 
				
			||||||
	FailedFiles             int
 | 
						FailedFiles             int64
 | 
				
			||||||
	ModificationsPerCommand sync.Map
 | 
						ModificationsPerCommand sync.Map
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	repo     *git.Repository
 | 
					 | 
				
			||||||
	worktree *git.Worktree
 | 
					 | 
				
			||||||
	stats GlobalStats = GlobalStats{
 | 
						stats GlobalStats = GlobalStats{
 | 
				
			||||||
		ModificationsPerCommand: sync.Map{},
 | 
							ModificationsPerCommand: sync.Map{},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -34,17 +37,20 @@ var (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	flag.Usage = func() {
 | 
						flag.Usage = func() {
 | 
				
			||||||
 | 
							CreateExampleConfig()
 | 
				
			||||||
		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, "  -git\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, "  -json\n")
 | 
				
			||||||
 | 
							fmt.Fprintf(os.Stderr, "        Enable JSON mode for processing JSON files\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, "  JSON mode:\n")
 | 
				
			||||||
 | 
							fmt.Fprintf(os.Stderr, "    %s -json 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")
 | 
				
			||||||
@@ -57,124 +63,240 @@ func main() {
 | 
				
			|||||||
	flag.Parse()
 | 
						flag.Parse()
 | 
				
			||||||
	args := flag.Args()
 | 
						args := flag.Args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	level := logger.ParseLevel(*utils.LogLevel)
 | 
						logger.InitFlag()
 | 
				
			||||||
	logger.Init(level)
 | 
						mainLogger.Info("Initializing with log level: %s", logger.GetLevel().String())
 | 
				
			||||||
	logger.Info("Initializing with log level: %s", level.String())
 | 
						mainLogger.Trace("Full argv: %v", os.Args)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// The plan is:
 | 
						if flag.NArg() == 0 {
 | 
				
			||||||
	// Load all commands
 | 
					 | 
				
			||||||
	commands, err := utils.LoadCommands(args)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Error("Failed to load commands: %v", err)
 | 
					 | 
				
			||||||
		flag.Usage()
 | 
							flag.Usage()
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mainLogger.Debug("Getting database connection")
 | 
				
			||||||
 | 
						db, err := utils.GetDB()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							mainLogger.Error("Failed to get database: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						mainLogger.Debug("Database connection established")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						workdone, err := HandleSpecialArgs(args, err, db)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							mainLogger.Error("Failed to handle special args: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if workdone {
 | 
				
			||||||
 | 
							mainLogger.Info("Special arguments handled, exiting.")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// The plan is:
 | 
				
			||||||
 | 
						// Load all commands
 | 
				
			||||||
 | 
						mainLogger.Debug("Loading commands from arguments")
 | 
				
			||||||
 | 
						mainLogger.Trace("Arguments: %v", args)
 | 
				
			||||||
 | 
						commands, err := utils.LoadCommands(args)
 | 
				
			||||||
 | 
						if err != nil || len(commands) == 0 {
 | 
				
			||||||
 | 
							mainLogger.Error("Failed to load commands: %v", err)
 | 
				
			||||||
 | 
							flag.Usage()
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Collect global modifiers from special entries and filter them out
 | 
				
			||||||
 | 
						vars := map[string]interface{}{}
 | 
				
			||||||
 | 
						filtered := make([]utils.ModifyCommand, 0, len(commands))
 | 
				
			||||||
 | 
						for _, c := range commands {
 | 
				
			||||||
 | 
							if len(c.Modifiers) > 0 && c.Name == "" && c.Regex == "" && len(c.Regexes) == 0 && c.Lua == "" && len(c.Files) == 0 {
 | 
				
			||||||
 | 
								for k, v := range c.Modifiers {
 | 
				
			||||||
 | 
									vars[k] = v
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							filtered = append(filtered, c)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(vars) > 0 {
 | 
				
			||||||
 | 
							mainLogger.Info("Loaded %d global modifiers", len(vars))
 | 
				
			||||||
 | 
							processor.SetVariables(vars)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						commands = filtered
 | 
				
			||||||
 | 
						mainLogger.Info("Loaded %d commands", len(commands))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if *utils.Filter != "" {
 | 
						if *utils.Filter != "" {
 | 
				
			||||||
		logger.Info("Filtering commands by name: %s", *utils.Filter)
 | 
							mainLogger.Info("Filtering commands by name: %s", *utils.Filter)
 | 
				
			||||||
		commands = utils.FilterCommands(commands, *utils.Filter)
 | 
							commands = utils.FilterCommands(commands, *utils.Filter)
 | 
				
			||||||
		logger.Info("Filtered %d commands", len(commands))
 | 
							mainLogger.Info("Filtered %d commands", len(commands))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Then aggregate all the globs and deduplicate them
 | 
						// Then aggregate all the globs and deduplicate them
 | 
				
			||||||
 | 
						mainLogger.Debug("Aggregating globs and deduplicating")
 | 
				
			||||||
	globs := utils.AggregateGlobs(commands)
 | 
						globs := utils.AggregateGlobs(commands)
 | 
				
			||||||
	logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands))
 | 
						mainLogger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, command := range commands {
 | 
				
			||||||
 | 
							mainLogger.Trace("Command: %s", command.Name)
 | 
				
			||||||
 | 
							if len(command.Regexes) > 0 {
 | 
				
			||||||
 | 
								mainLogger.Trace("Regexes: %v", command.Regexes)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								mainLogger.Trace("Regex: %s", command.Regex)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mainLogger.Trace("Files: %v", command.Files)
 | 
				
			||||||
 | 
							mainLogger.Trace("Lua: %s", command.Lua)
 | 
				
			||||||
 | 
							mainLogger.Trace("Reset: %t", command.Reset)
 | 
				
			||||||
 | 
							mainLogger.Trace("Isolate: %t", command.Isolate)
 | 
				
			||||||
 | 
							mainLogger.Trace("LogLevel: %s", command.LogLevel)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Resolve all the files for all the globs
 | 
						// Resolve all the files for all the globs
 | 
				
			||||||
	logger.Info("Found %d unique file patterns", len(globs))
 | 
						mainLogger.Info("Found %d unique file patterns", len(globs))
 | 
				
			||||||
 | 
						mainLogger.Debug("Expanding glob patterns to files")
 | 
				
			||||||
	files, err := utils.ExpandGLobs(globs)
 | 
						files, err := utils.ExpandGLobs(globs)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Error("Failed to expand file patterns: %v", err)
 | 
							mainLogger.Error("Failed to expand file patterns: %v", err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	logger.Info("Found %d files to process", len(files))
 | 
						mainLogger.Info("Found %d files to process", len(files))
 | 
				
			||||||
 | 
						mainLogger.Trace("Files to process: %v", files)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Somehow connect files to commands via globs..
 | 
						// Somehow connect files to commands via globs..
 | 
				
			||||||
	// For each file check every glob of every command
 | 
						// For each file check every glob of every command
 | 
				
			||||||
	// Maybe memoize this part
 | 
						// Maybe memoize this part
 | 
				
			||||||
	// That way we know what commands affect what files
 | 
						// That way we know what commands affect what files
 | 
				
			||||||
 | 
						mainLogger.Debug("Associating files with commands")
 | 
				
			||||||
	associations, err := utils.AssociateFilesWithCommands(files, commands)
 | 
						associations, err := utils.AssociateFilesWithCommands(files, commands)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Error("Failed to associate files with commands: %v", err)
 | 
							mainLogger.Error("Failed to associate files with commands: %v", err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						mainLogger.Debug("Files associated with commands")
 | 
				
			||||||
 | 
						mainLogger.Trace("File-command associations: %v", associations)
 | 
				
			||||||
 | 
						// Per-file association summary for better visibility when debugging
 | 
				
			||||||
 | 
						for file, assoc := range associations {
 | 
				
			||||||
 | 
							cmdNames := make([]string, 0, len(assoc.Commands))
 | 
				
			||||||
 | 
							for _, c := range assoc.Commands {
 | 
				
			||||||
 | 
								cmdNames = append(cmdNames, c.Name)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							isoNames := make([]string, 0, len(assoc.IsolateCommands))
 | 
				
			||||||
 | 
							for _, c := range assoc.IsolateCommands {
 | 
				
			||||||
 | 
								isoNames = append(isoNames, c.Name)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mainLogger.Debug("File %q has %d regular and %d isolate commands", file, len(assoc.Commands), len(assoc.IsolateCommands))
 | 
				
			||||||
 | 
							mainLogger.Trace("\tRegular: %v", cmdNames)
 | 
				
			||||||
 | 
							mainLogger.Trace("\tIsolate: %v", isoNames)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mainLogger.Debug("Resetting files where necessary")
 | 
				
			||||||
 | 
						err = utils.ResetWhereNecessary(associations, db)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							mainLogger.Error("Failed to reset files where necessary: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						mainLogger.Debug("Files reset where necessary")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Then for each file run all commands associated with the file
 | 
						// Then for each file run all commands associated with the file
 | 
				
			||||||
	workers := make(chan struct{}, *utils.ParallelFiles)
 | 
						workers := make(chan struct{}, *utils.ParallelFiles)
 | 
				
			||||||
	wg := sync.WaitGroup{}
 | 
						wg := sync.WaitGroup{}
 | 
				
			||||||
 | 
						mainLogger.Debug("Starting file processing with %d parallel workers", *utils.ParallelFiles)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Add performance tracking
 | 
						// Add performance tracking
 | 
				
			||||||
	startTime := time.Now()
 | 
						startTime := time.Now()
 | 
				
			||||||
	var fileMutex sync.Mutex
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for file, commands := range associations {
 | 
						// Create a map to store loggers for each command
 | 
				
			||||||
 | 
						commandLoggers := make(map[string]*logger.Logger)
 | 
				
			||||||
 | 
						for _, command := range commands {
 | 
				
			||||||
 | 
							// Create a named logger for each command
 | 
				
			||||||
 | 
							cmdName := command.Name
 | 
				
			||||||
 | 
							if cmdName == "" {
 | 
				
			||||||
 | 
								// If no name is provided, use a short version of the regex pattern
 | 
				
			||||||
 | 
								if len(command.Regex) > 20 {
 | 
				
			||||||
 | 
									cmdName = command.Regex[:17] + "..."
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									cmdName = command.Regex
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Parse the log level for this specific command
 | 
				
			||||||
 | 
							cmdLogLevel := logger.ParseLevel(command.LogLevel)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Create a logger with the command name as a field
 | 
				
			||||||
 | 
							commandLoggers[command.Name] = logger.Default.WithField("command", cmdName)
 | 
				
			||||||
 | 
							commandLoggers[command.Name].SetLevel(cmdLogLevel)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							mainLogger.Debug("Created logger for command %q with log level %s", cmdName, cmdLogLevel.String())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for file, association := range associations {
 | 
				
			||||||
		workers <- struct{}{}
 | 
							workers <- struct{}{}
 | 
				
			||||||
		wg.Add(1)
 | 
							wg.Add(1)
 | 
				
			||||||
		logger.SafeGoWithArgs(func(args ...interface{}) {
 | 
							logger.SafeGoWithArgs(func(args ...interface{}) {
 | 
				
			||||||
			defer func() { <-workers }()
 | 
								defer func() { <-workers }()
 | 
				
			||||||
			defer wg.Done()
 | 
								defer wg.Done()
 | 
				
			||||||
 | 
					 | 
				
			||||||
			// Track per-file processing time
 | 
								// Track per-file processing time
 | 
				
			||||||
			fileStartTime := time.Now()
 | 
								fileStartTime := time.Now()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								mainLogger.Debug("Reading file %q", file)
 | 
				
			||||||
			fileData, err := os.ReadFile(file)
 | 
								fileData, err := os.ReadFile(file)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				logger.Error("Failed to read file %q: %v", file, err)
 | 
									mainLogger.Error("Failed to read file %q: %v", file, err)
 | 
				
			||||||
 | 
									atomic.AddInt64(&stats.FailedFiles, 1)
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
 | 
					 | 
				
			||||||
			fileDataStr := string(fileData)
 | 
								fileDataStr := string(fileData)
 | 
				
			||||||
 | 
								mainLogger.Trace("File %q content: %s", file, utils.LimitString(fileDataStr, 500))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Aggregate all the modifications and execute them
 | 
								isChanged := false
 | 
				
			||||||
			modifications := []utils.ReplaceCommand{}
 | 
								mainLogger.Debug("Running isolate commands for file %q", file)
 | 
				
			||||||
			for _, command := range commands {
 | 
								fileDataStr, err = RunIsolateCommands(association, file, fileDataStr)
 | 
				
			||||||
				logger.Info("Processing file %q with command %q", file, command.Regex)
 | 
								if err != nil && err != NothingToDo {
 | 
				
			||||||
				commands, err := processor.ProcessRegex(fileDataStr, command)
 | 
									mainLogger.Error("Failed to run isolate commands for file %q: %v", file, err)
 | 
				
			||||||
 | 
									atomic.AddInt64(&stats.FailedFiles, 1)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if err != NothingToDo {
 | 
				
			||||||
 | 
									isChanged = true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								mainLogger.Debug("Running other commands for file %q", file)
 | 
				
			||||||
 | 
								fileDataStr, err = RunOtherCommands(file, fileDataStr, association, commandLoggers)
 | 
				
			||||||
 | 
								if err != nil && err != NothingToDo {
 | 
				
			||||||
 | 
									mainLogger.Error("Failed to run other commands for file %q: %v", file, err)
 | 
				
			||||||
 | 
									atomic.AddInt64(&stats.FailedFiles, 1)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if err != NothingToDo {
 | 
				
			||||||
 | 
									isChanged = true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if isChanged {
 | 
				
			||||||
 | 
									mainLogger.Debug("Saving file %q to database", file)
 | 
				
			||||||
 | 
									err = db.SaveFile(file, fileData)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
					logger.Error("Failed to process file %q with command %q: %v", file, command.Regex, err)
 | 
										mainLogger.Error("Failed to save file %q to database: %v", file, err)
 | 
				
			||||||
 | 
										atomic.AddInt64(&stats.FailedFiles, 1)
 | 
				
			||||||
					return
 | 
										return
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				modifications = append(modifications, commands...)
 | 
									mainLogger.Debug("File %q saved to database", file)
 | 
				
			||||||
				// It is not guranteed that all the commands will be executed...
 | 
					 | 
				
			||||||
				// TODO: Make this better
 | 
					 | 
				
			||||||
				// We'd have to pass the map to executemodifications or something...
 | 
					 | 
				
			||||||
				count, ok := stats.ModificationsPerCommand.Load(command.Name)
 | 
					 | 
				
			||||||
				if !ok {
 | 
					 | 
				
			||||||
					count = 0
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(commands))
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if len(modifications) == 0 {
 | 
								mainLogger.Debug("Writing file %q", file)
 | 
				
			||||||
				logger.Info("No modifications found for file %q", file)
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			// Sort commands in reverse order for safe replacements
 | 
					 | 
				
			||||||
			fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			fileMutex.Lock()
 | 
					 | 
				
			||||||
			stats.ProcessedFiles++
 | 
					 | 
				
			||||||
			stats.TotalModifications += count
 | 
					 | 
				
			||||||
			fileMutex.Unlock()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			logger.Info("Executed %d modifications for file %q", count, file)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			err = os.WriteFile(file, []byte(fileDataStr), 0644)
 | 
								err = os.WriteFile(file, []byte(fileDataStr), 0644)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				logger.Error("Failed to write file %q: %v", file, err)
 | 
									mainLogger.Error("Failed to write file %q: %v", file, err)
 | 
				
			||||||
 | 
									atomic.AddInt64(&stats.FailedFiles, 1)
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								mainLogger.Debug("File %q written", file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			logger.Debug("File %q processed in %v", file, time.Since(fileStartTime))
 | 
								// Only increment ProcessedFiles once per file, after all processing is complete
 | 
				
			||||||
 | 
								atomic.AddInt64(&stats.ProcessedFiles, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								mainLogger.Debug("File %q processed in %v", file, time.Since(fileStartTime))
 | 
				
			||||||
		}, file, commands)
 | 
							}, file, commands)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	wg.Wait()
 | 
						wg.Wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	processingTime := time.Since(startTime)
 | 
						processingTime := time.Since(startTime)
 | 
				
			||||||
	logger.Info("Processing completed in %v", processingTime)
 | 
						mainLogger.Info("Processing completed in %v", processingTime)
 | 
				
			||||||
	if stats.ProcessedFiles > 0 {
 | 
						processedFiles := atomic.LoadInt64(&stats.ProcessedFiles)
 | 
				
			||||||
		logger.Info("Average time per file: %v", processingTime/time.Duration(stats.ProcessedFiles))
 | 
						if processedFiles > 0 {
 | 
				
			||||||
 | 
							mainLogger.Info("Average time per file: %v", processingTime/time.Duration(processedFiles))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO: Also give each command its own logger, maybe prefix it with something... Maybe give commands a name?
 | 
						// TODO: Also give each command its own logger, maybe prefix it with something... Maybe give commands a name?
 | 
				
			||||||
@@ -184,44 +306,46 @@ func main() {
 | 
				
			|||||||
	// TODO: What to do with git? Figure it out ....
 | 
						// TODO: What to do with git? Figure it out ....
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// if *gitFlag {
 | 
						// if *gitFlag {
 | 
				
			||||||
	// 	logger.Info("Git integration enabled, setting up git repository")
 | 
						// 	mainLogger.Info("Git integration enabled, setting up git repository")
 | 
				
			||||||
	// 	err := setupGit()
 | 
						// 	err := setupGit()
 | 
				
			||||||
	// 	if err != nil {
 | 
						// 	if err != nil {
 | 
				
			||||||
	// 		logger.Error("Failed to setup git: %v", err)
 | 
						// 		mainLogger.Error("Failed to setup git: %v", err)
 | 
				
			||||||
	// 		fmt.Fprintf(os.Stderr, "Error setting up git: %v\n", err)
 | 
						// 		fmt.Fprintf(os.Stderr, "Error setting up git: %v\n", err)
 | 
				
			||||||
	// 		return
 | 
						// 		return
 | 
				
			||||||
	// 	}
 | 
						// 	}
 | 
				
			||||||
	// }
 | 
						// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// logger.Debug("Expanding file patterns")
 | 
						// mainLogger.Debug("Expanding file patterns")
 | 
				
			||||||
	// files, err := expandFilePatterns(filePatterns)
 | 
						// files, err := expandFilePatterns(filePatterns)
 | 
				
			||||||
	// if err != nil {
 | 
						// if err != nil {
 | 
				
			||||||
	// 	logger.Error("Failed to expand file patterns: %v", err)
 | 
						// 	mainLogger.Error("Failed to expand file patterns: %v", err)
 | 
				
			||||||
	// 	fmt.Fprintf(os.Stderr, "Error expanding file patterns: %v\n", err)
 | 
						// 	fmt.Fprintf(os.Stderr, "Error expanding file patterns: %v\n", err)
 | 
				
			||||||
	// 	return
 | 
						// 	return
 | 
				
			||||||
	// }
 | 
						// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// if *gitFlag {
 | 
						// if *gitFlag {
 | 
				
			||||||
	// 	logger.Info("Cleaning up git files before processing")
 | 
						// 	mainLogger.Info("Cleaning up git files before processing")
 | 
				
			||||||
	// 	err := cleanupGitFiles(files)
 | 
						// 	err := cleanupGitFiles(files)
 | 
				
			||||||
	// 	if err != nil {
 | 
						// 	if err != nil {
 | 
				
			||||||
	// 		logger.Error("Failed to cleanup git files: %v", err)
 | 
						// 		mainLogger.Error("Failed to cleanup git files: %v", err)
 | 
				
			||||||
	// 		fmt.Fprintf(os.Stderr, "Error cleaning up git files: %v\n", err)
 | 
						// 		fmt.Fprintf(os.Stderr, "Error cleaning up git files: %v\n", err)
 | 
				
			||||||
	// 		return
 | 
						// 		return
 | 
				
			||||||
	// 	}
 | 
						// 	}
 | 
				
			||||||
	// }
 | 
						// }
 | 
				
			||||||
	// if *resetFlag {
 | 
						// if *resetFlag {
 | 
				
			||||||
	// 	logger.Info("Files reset to their original state, nothing more to do")
 | 
						// 	mainLogger.Info("Files reset to their original state, nothing more to do")
 | 
				
			||||||
	// 	log.Printf("Files reset to their original state, nothing more to do")
 | 
						// 	log.Printf("Files reset to their original state, nothing more to do")
 | 
				
			||||||
	// 	return
 | 
						// 	return
 | 
				
			||||||
	// }
 | 
						// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Print summary
 | 
						// Print summary
 | 
				
			||||||
	if stats.TotalModifications == 0 {
 | 
						totalModifications := atomic.LoadInt64(&stats.TotalModifications)
 | 
				
			||||||
		logger.Warning("No modifications were made in any files")
 | 
						if totalModifications == 0 {
 | 
				
			||||||
 | 
							mainLogger.Warning("No modifications were made in any files")
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		logger.Info("Operation complete! Modified %d values in %d/%d files",
 | 
							failedFiles := atomic.LoadInt64(&stats.FailedFiles)
 | 
				
			||||||
			stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
 | 
							mainLogger.Info("Operation complete! Modified %d values in %d/%d files",
 | 
				
			||||||
 | 
								totalModifications, processedFiles, processedFiles+failedFiles)
 | 
				
			||||||
		sortedCommands := []string{}
 | 
							sortedCommands := []string{}
 | 
				
			||||||
		stats.ModificationsPerCommand.Range(func(key, value interface{}) bool {
 | 
							stats.ModificationsPerCommand.Range(func(key, value interface{}) bool {
 | 
				
			||||||
			sortedCommands = append(sortedCommands, key.(string))
 | 
								sortedCommands = append(sortedCommands, key.(string))
 | 
				
			||||||
@@ -232,10 +356,346 @@ func main() {
 | 
				
			|||||||
		for _, command := range sortedCommands {
 | 
							for _, command := range sortedCommands {
 | 
				
			||||||
			count, _ := stats.ModificationsPerCommand.Load(command)
 | 
								count, _ := stats.ModificationsPerCommand.Load(command)
 | 
				
			||||||
			if count.(int) > 0 {
 | 
								if count.(int) > 0 {
 | 
				
			||||||
				logger.Info("\tCommand %q made %d modifications", command, count)
 | 
									mainLogger.Info("\tCommand %q made %d modifications", command, count)
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				logger.Warning("\tCommand %q made no modifications", command)
 | 
									mainLogger.Warning("\tCommand %q made no modifications", command)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func HandleSpecialArgs(args []string, err error, db utils.DB) (bool, error) {
 | 
				
			||||||
 | 
						handleSpecialArgsLogger := logger.Default.WithPrefix("HandleSpecialArgs")
 | 
				
			||||||
 | 
						handleSpecialArgsLogger.Debug("Handling special arguments: %v", args)
 | 
				
			||||||
 | 
						switch args[0] {
 | 
				
			||||||
 | 
						case "reset":
 | 
				
			||||||
 | 
							handleSpecialArgsLogger.Info("Resetting all files")
 | 
				
			||||||
 | 
							err = utils.ResetAllFiles(db)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								handleSpecialArgsLogger.Error("Failed to reset all files: %v", err)
 | 
				
			||||||
 | 
								return true, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							handleSpecialArgsLogger.Info("All files reset")
 | 
				
			||||||
 | 
							return true, nil
 | 
				
			||||||
 | 
						case "dump":
 | 
				
			||||||
 | 
							handleSpecialArgsLogger.Info("Dumping all files from database")
 | 
				
			||||||
 | 
							err = db.RemoveAllFiles()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								handleSpecialArgsLogger.Error("Failed to remove all files from database: %v", err)
 | 
				
			||||||
 | 
								return true, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							handleSpecialArgsLogger.Info("All files removed from database")
 | 
				
			||||||
 | 
							return true, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						handleSpecialArgsLogger.Debug("No special arguments handled, returning false")
 | 
				
			||||||
 | 
						return false, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CreateExampleConfig() {
 | 
				
			||||||
 | 
						createExampleConfigLogger := logger.Default.WithPrefix("CreateExampleConfig")
 | 
				
			||||||
 | 
						createExampleConfigLogger.Debug("Creating example configuration file")
 | 
				
			||||||
 | 
						commands := []utils.ModifyCommand{
 | 
				
			||||||
 | 
							// Global modifiers only entry (no name/regex/lua/files)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Modifiers: map[string]interface{}{
 | 
				
			||||||
 | 
									"foobar":   4,
 | 
				
			||||||
 | 
									"multiply": 1.5,
 | 
				
			||||||
 | 
									"prefix":   "NEW_",
 | 
				
			||||||
 | 
									"enabled":  true,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Multi-regex example using $variable in Lua
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:    "RFToolsMultiply",
 | 
				
			||||||
 | 
								Regexes: []string{"generatePerTick = !num", "ticksPer\\w+ = !num", "generatorRFPerTick = !num"},
 | 
				
			||||||
 | 
								Lua:     "* $foobar",
 | 
				
			||||||
 | 
								Files:   []string{"polymc/instances/**/rftools*.toml", `polymc\\instances\\**\\rftools*.toml`},
 | 
				
			||||||
 | 
								Reset:   true,
 | 
				
			||||||
 | 
								// LogLevel defaults to INFO
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Named capture groups with arithmetic and string ops
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "UpdateAmountsAndItems",
 | 
				
			||||||
 | 
								Regex: `(?P<amount>!num)\s+units\s+of\s+(?P<item>[A-Za-z_\-]+)`,
 | 
				
			||||||
 | 
								Lua:   `amount = amount * $multiply; item = upper(item); return true`,
 | 
				
			||||||
 | 
								Files: []string{"data/**/*.txt"},
 | 
				
			||||||
 | 
								// INFO log level
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Full replacement via Lua 'replacement' variable
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "BumpMinorVersion",
 | 
				
			||||||
 | 
								Regex: `version\s*=\s*"(?P<major>!num)\.(?P<minor>!num)\.(?P<patch>!num)"`,
 | 
				
			||||||
 | 
								Lua:   `replacement = format("version=\"%s.%s.%s\"", major, num(minor)+1, 0); return true`,
 | 
				
			||||||
 | 
								Files: []string{"config/*.ini", "config/*.cfg"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Multiline regex example (DOTALL is auto-enabled). Captures numeric in nested XML.
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "XMLNestedValueMultiply",
 | 
				
			||||||
 | 
								Regex: `<item>\s*\s*<name>!any<\/name>\s*\s*<value>(!num)<\/value>\s*\s*<\/item>`,
 | 
				
			||||||
 | 
								Lua:   `* $multiply`,
 | 
				
			||||||
 | 
								Files: []string{"data/**/*.xml"},
 | 
				
			||||||
 | 
								// Demonstrates multiline regex in YAML
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Multiline regexES array, with different patterns handled by same Lua
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name: "MultiLinePatterns",
 | 
				
			||||||
 | 
								Regexes: []string{
 | 
				
			||||||
 | 
									`<entry>\s*\n\s*<id>(?P<id>!num)</id>\s*\n\s*<score>(?P<score>!num)</score>\s*\n\s*</entry>`,
 | 
				
			||||||
 | 
									`\[block\]\nkey=(?P<key>[A-Za-z_]+)\nvalue=(?P<val>!num)`,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								Lua:      `if is_number(score) then score = score * 2 end; if is_number(val) then val = val * 3 end; return true`,
 | 
				
			||||||
 | 
								Files:    []string{"examples/**/*.*"},
 | 
				
			||||||
 | 
								LogLevel: "DEBUG",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Use equals operator shorthand and boolean variable
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "EnableFlags",
 | 
				
			||||||
 | 
								Regex: `enabled\s*=\s*(true|false)`,
 | 
				
			||||||
 | 
								Lua:   `= $enabled`,
 | 
				
			||||||
 | 
								Files: []string{"**/*.toml"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Demonstrate NoDedup to allow overlapping replacements
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:    "OverlappingGroups",
 | 
				
			||||||
 | 
								Regex:   `(?P<a>!num)(?P<b>!num)`,
 | 
				
			||||||
 | 
								Lua:     `a = num(a) + 1; b = num(b) + 1; return true`,
 | 
				
			||||||
 | 
								Files:   []string{"overlap/**/*.txt"},
 | 
				
			||||||
 | 
								NoDedup: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Isolate command example operating on entire matched block
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:     "IsolateUppercaseBlock",
 | 
				
			||||||
 | 
								Regex:    `BEGIN\n(?P<block>!any)\nEND`,
 | 
				
			||||||
 | 
								Lua:      `block = upper(block); return true`,
 | 
				
			||||||
 | 
								Files:    []string{"logs/**/*.log"},
 | 
				
			||||||
 | 
								Isolate:  true,
 | 
				
			||||||
 | 
								LogLevel: "TRACE",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Using !rep placeholder and arrays of files
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "RepeatPlaceholderExample",
 | 
				
			||||||
 | 
								Regex: `name: (.*) !rep(, .* , 2)`,
 | 
				
			||||||
 | 
								Lua:   `-- no-op, just demonstrate placeholder; return false`,
 | 
				
			||||||
 | 
								Files: []string{"lists/**/*.yml", "lists/**/*.yaml"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Using string variable in Lua expression
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "PrefixKeys",
 | 
				
			||||||
 | 
								Regex: `(?P<key>[A-Za-z0-9_]+)\s*=`,
 | 
				
			||||||
 | 
								Lua:   `key = $prefix .. key; return true`,
 | 
				
			||||||
 | 
								Files: []string{"**/*.properties"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// JSON mode examples
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "JSONArrayMultiply",
 | 
				
			||||||
 | 
								JSON:  true,
 | 
				
			||||||
 | 
								Lua:   `for i, item in ipairs(data.items) do data.items[i].value = item.value * 2 end; return true`,
 | 
				
			||||||
 | 
								Files: []string{"data/**/*.json"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "JSONObjectUpdate",
 | 
				
			||||||
 | 
								JSON:  true,
 | 
				
			||||||
 | 
								Lua:   `data.version = "2.0.0"; data.enabled = true; return true`,
 | 
				
			||||||
 | 
								Files: []string{"config/**/*.json"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name:  "JSONNestedModify",
 | 
				
			||||||
 | 
								JSON:  true,
 | 
				
			||||||
 | 
								Lua:   `if data.settings and data.settings.performance then data.settings.performance.multiplier = data.settings.performance.multiplier * 1.5 end; return true`,
 | 
				
			||||||
 | 
								Files: []string{"settings/**/*.json"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data, err := yaml.Marshal(commands)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							createExampleConfigLogger.Error("Failed to marshal example config: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						createExampleConfigLogger.Debug("Writing example_cook.yml")
 | 
				
			||||||
 | 
						err = os.WriteFile("example_cook.yml", data, 0644)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							createExampleConfigLogger.Error("Failed to write example_cook.yml: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						createExampleConfigLogger.Info("Wrote example_cook.yml")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var NothingToDo = errors.New("nothing to do")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, commandLoggers map[string]*logger.Logger) (string, error) {
 | 
				
			||||||
 | 
						runOtherCommandsLogger := mainLogger.WithPrefix("RunOtherCommands").WithField("file", file)
 | 
				
			||||||
 | 
						runOtherCommandsLogger.Debug("Running other commands for file")
 | 
				
			||||||
 | 
						runOtherCommandsLogger.Trace("File data before modifications: %s", utils.LimitString(fileDataStr, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Separate JSON and regex commands for different processing approaches
 | 
				
			||||||
 | 
						jsonCommands := []utils.ModifyCommand{}
 | 
				
			||||||
 | 
						regexCommands := []utils.ModifyCommand{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, command := range association.Commands {
 | 
				
			||||||
 | 
							if command.JSON || *utils.JSON {
 | 
				
			||||||
 | 
								jsonCommands = append(jsonCommands, command)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								regexCommands = append(regexCommands, command)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Process JSON commands sequentially (each operates on the entire file)
 | 
				
			||||||
 | 
						for _, command := range jsonCommands {
 | 
				
			||||||
 | 
							cmdLogger := logger.Default
 | 
				
			||||||
 | 
							if cmdLog, ok := commandLoggers[command.Name]; ok {
 | 
				
			||||||
 | 
								cmdLogger = cmdLog
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							cmdLogger.Debug("Processing file with JSON mode for command %q", command.Name)
 | 
				
			||||||
 | 
							newModifications, err := processor.ProcessJSON(fileDataStr, command, file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								runOtherCommandsLogger.Error("Failed to process file with JSON command %q: %v", command.Name, err)
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Apply JSON modifications immediately
 | 
				
			||||||
 | 
							if len(newModifications) > 0 {
 | 
				
			||||||
 | 
								var count int
 | 
				
			||||||
 | 
								fileDataStr, count = utils.ExecuteModifications(newModifications, fileDataStr)
 | 
				
			||||||
 | 
								atomic.AddInt64(&stats.TotalModifications, int64(count))
 | 
				
			||||||
 | 
								cmdLogger.Debug("Applied %d JSON modifications for command %q", count, command.Name)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							count, ok := stats.ModificationsPerCommand.Load(command.Name)
 | 
				
			||||||
 | 
							if !ok {
 | 
				
			||||||
 | 
								count = 0
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Aggregate regex modifications and execute them
 | 
				
			||||||
 | 
						modifications := []utils.ReplaceCommand{}
 | 
				
			||||||
 | 
						numCommandsConsidered := 0
 | 
				
			||||||
 | 
						for _, command := range regexCommands {
 | 
				
			||||||
 | 
							cmdLogger := logger.Default
 | 
				
			||||||
 | 
							if cmdLog, ok := commandLoggers[command.Name]; ok {
 | 
				
			||||||
 | 
								cmdLogger = cmdLog
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							patterns := command.Regexes
 | 
				
			||||||
 | 
							if len(patterns) == 0 {
 | 
				
			||||||
 | 
								patterns = []string{command.Regex}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for idx, pattern := range patterns {
 | 
				
			||||||
 | 
								tmpCmd := command
 | 
				
			||||||
 | 
								tmpCmd.Regex = pattern
 | 
				
			||||||
 | 
								cmdLogger.Debug("Begin processing file with command %q (pattern %d/%d)", command.Name, idx+1, len(patterns))
 | 
				
			||||||
 | 
								numCommandsConsidered++
 | 
				
			||||||
 | 
								newModifications, err := processor.ProcessRegex(fileDataStr, tmpCmd, file)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									runOtherCommandsLogger.Error("Failed to process file with command %q: %v", command.Name, err)
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								modifications = append(modifications, newModifications...)
 | 
				
			||||||
 | 
								count, ok := stats.ModificationsPerCommand.Load(command.Name)
 | 
				
			||||||
 | 
								if !ok {
 | 
				
			||||||
 | 
									count = 0
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								cmdLogger.Debug("Command %q generated %d modifications (pattern %d/%d)", command.Name, len(newModifications), idx+1, len(patterns))
 | 
				
			||||||
 | 
								cmdLogger.Trace("Modifications generated by command %q: %v", command.Name, newModifications)
 | 
				
			||||||
 | 
								if len(newModifications) == 0 {
 | 
				
			||||||
 | 
									cmdLogger.Debug("No modifications yielded by command %q (pattern %d/%d)", command.Name, idx+1, len(patterns))
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						runOtherCommandsLogger.Debug("Aggregated %d modifications from %d command-pattern runs", len(modifications), numCommandsConsidered)
 | 
				
			||||||
 | 
						runOtherCommandsLogger.Trace("All aggregated modifications: %v", modifications)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(modifications) == 0 {
 | 
				
			||||||
 | 
							runOtherCommandsLogger.Warning("No modifications found for file")
 | 
				
			||||||
 | 
							return fileDataStr, NothingToDo
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						runOtherCommandsLogger.Debug("Executing %d modifications for file", len(modifications))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Sort commands in reverse order for safe replacements
 | 
				
			||||||
 | 
						var count int
 | 
				
			||||||
 | 
						fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
 | 
				
			||||||
 | 
						runOtherCommandsLogger.Trace("File data after modifications: %s", utils.LimitString(fileDataStr, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						atomic.AddInt64(&stats.TotalModifications, int64(count))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						runOtherCommandsLogger.Info("Executed %d modifications for file", count)
 | 
				
			||||||
 | 
						return fileDataStr, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileDataStr string) (string, error) {
 | 
				
			||||||
 | 
						runIsolateCommandsLogger := mainLogger.WithPrefix("RunIsolateCommands").WithField("file", file)
 | 
				
			||||||
 | 
						runIsolateCommandsLogger.Debug("Running isolate commands for file")
 | 
				
			||||||
 | 
						runIsolateCommandsLogger.Trace("File data before isolate modifications: %s", utils.LimitString(fileDataStr, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						anythingDone := false
 | 
				
			||||||
 | 
						for _, isolateCommand := range association.IsolateCommands {
 | 
				
			||||||
 | 
							// Check if this isolate command should use JSON mode
 | 
				
			||||||
 | 
							if isolateCommand.JSON || *utils.JSON {
 | 
				
			||||||
 | 
								runIsolateCommandsLogger.Debug("Begin processing file with JSON isolate command %q", isolateCommand.Name)
 | 
				
			||||||
 | 
								modifications, err := processor.ProcessJSON(fileDataStr, isolateCommand, file)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									runIsolateCommandsLogger.Error("Failed to process file with JSON isolate command %q: %v", isolateCommand.Name, err)
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if len(modifications) == 0 {
 | 
				
			||||||
 | 
									runIsolateCommandsLogger.Debug("JSON isolate command %q produced no modifications", isolateCommand.Name)
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								anythingDone = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								runIsolateCommandsLogger.Debug("Executing %d JSON isolate modifications for file", len(modifications))
 | 
				
			||||||
 | 
								runIsolateCommandsLogger.Trace("JSON isolate modifications: %v", modifications)
 | 
				
			||||||
 | 
								var count int
 | 
				
			||||||
 | 
								fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
 | 
				
			||||||
 | 
								runIsolateCommandsLogger.Trace("File data after JSON isolate modifications: %s", utils.LimitString(fileDataStr, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								atomic.AddInt64(&stats.TotalModifications, int64(count))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								runIsolateCommandsLogger.Info("Executed %d JSON isolate modifications for file", count)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								// Regular regex processing for isolate commands
 | 
				
			||||||
 | 
								runIsolateCommandsLogger.Debug("Begin processing file with isolate command %q", isolateCommand.Regex)
 | 
				
			||||||
 | 
								patterns := isolateCommand.Regexes
 | 
				
			||||||
 | 
								if len(patterns) == 0 {
 | 
				
			||||||
 | 
									patterns = []string{isolateCommand.Regex}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								for idx, pattern := range patterns {
 | 
				
			||||||
 | 
									tmpCmd := isolateCommand
 | 
				
			||||||
 | 
									tmpCmd.Regex = pattern
 | 
				
			||||||
 | 
									modifications, err := processor.ProcessRegex(fileDataStr, tmpCmd, file)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										runIsolateCommandsLogger.Error("Failed to process file with isolate command %q (pattern %d/%d): %v", isolateCommand.Name, idx+1, len(patterns), err)
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if len(modifications) == 0 {
 | 
				
			||||||
 | 
										runIsolateCommandsLogger.Debug("Isolate command %q produced no modifications (pattern %d/%d)", isolateCommand.Name, idx+1, len(patterns))
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									anythingDone = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									runIsolateCommandsLogger.Debug("Executing %d isolate modifications for file", len(modifications))
 | 
				
			||||||
 | 
									runIsolateCommandsLogger.Trace("Isolate modifications: %v", modifications)
 | 
				
			||||||
 | 
									var count int
 | 
				
			||||||
 | 
									fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
 | 
				
			||||||
 | 
									runIsolateCommandsLogger.Trace("File data after isolate modifications: %s", utils.LimitString(fileDataStr, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									atomic.AddInt64(&stats.TotalModifications, int64(count))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									runIsolateCommandsLogger.Info("Executed %d isolate modifications for file", count)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if !anythingDone {
 | 
				
			||||||
 | 
							runIsolateCommandsLogger.Debug("No isolate modifications were made for file")
 | 
				
			||||||
 | 
							return fileDataStr, NothingToDo
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return fileDataStr, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										216
									
								
								processor/json.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										216
									
								
								processor/json.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,216 @@
 | 
				
			|||||||
 | 
					package processor
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"cook/utils"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
 | 
						lua "github.com/yuin/gopher-lua"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// jsonLogger is a scoped logger for the processor/json package.
 | 
				
			||||||
 | 
					var jsonLogger = logger.Default.WithPrefix("processor/json")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ProcessJSON applies Lua processing to JSON content
 | 
				
			||||||
 | 
					func ProcessJSON(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
 | 
				
			||||||
 | 
						processJsonLogger := jsonLogger.WithPrefix("ProcessJSON").WithField("commandName", command.Name).WithField("file", filename)
 | 
				
			||||||
 | 
						processJsonLogger.Debug("Starting JSON processing for file")
 | 
				
			||||||
 | 
						processJsonLogger.Trace("Initial file content length: %d", len(content))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var commands []utils.ReplaceCommand
 | 
				
			||||||
 | 
						startTime := time.Now()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Parse JSON content
 | 
				
			||||||
 | 
						var jsonData interface{}
 | 
				
			||||||
 | 
						err := json.Unmarshal([]byte(content), &jsonData)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							processJsonLogger.Error("Failed to parse JSON content: %v", err)
 | 
				
			||||||
 | 
							return commands, fmt.Errorf("failed to parse JSON: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						processJsonLogger.Debug("Successfully parsed JSON content")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create Lua state
 | 
				
			||||||
 | 
						L, err := NewLuaState()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							processJsonLogger.Error("Error creating Lua state: %v", err)
 | 
				
			||||||
 | 
							return commands, fmt.Errorf("error creating Lua state: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer L.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Set filename global
 | 
				
			||||||
 | 
						L.SetGlobal("file", lua.LString(filename))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Convert JSON data to Lua table
 | 
				
			||||||
 | 
						luaTable, err := ToLuaTable(L, jsonData)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							processJsonLogger.Error("Failed to convert JSON to Lua table: %v", err)
 | 
				
			||||||
 | 
							return commands, fmt.Errorf("failed to convert JSON to Lua table: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Set the JSON data as a global variable
 | 
				
			||||||
 | 
						L.SetGlobal("data", luaTable)
 | 
				
			||||||
 | 
						processJsonLogger.Debug("Set JSON data as Lua global 'data'")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Build and execute Lua script for JSON mode
 | 
				
			||||||
 | 
						luaExpr := BuildJSONLuaScript(command.Lua)
 | 
				
			||||||
 | 
						processJsonLogger.Debug("Built Lua script from expression: %q", command.Lua)
 | 
				
			||||||
 | 
						processJsonLogger.Trace("Full Lua script: %q", utils.LimitString(luaExpr, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := L.DoString(luaExpr); err != nil {
 | 
				
			||||||
 | 
							processJsonLogger.Error("Lua script execution failed: %v\nScript: %s", err, utils.LimitString(luaExpr, 200))
 | 
				
			||||||
 | 
							return commands, fmt.Errorf("lua script execution failed: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						processJsonLogger.Debug("Lua script executed successfully")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Check if modification flag is set
 | 
				
			||||||
 | 
						modifiedVal := L.GetGlobal("modified")
 | 
				
			||||||
 | 
						if modifiedVal.Type() != lua.LTBool || !lua.LVAsBool(modifiedVal) {
 | 
				
			||||||
 | 
							processJsonLogger.Debug("Skipping - no modifications indicated by Lua script")
 | 
				
			||||||
 | 
							return commands, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Get the modified data from Lua
 | 
				
			||||||
 | 
						modifiedData := L.GetGlobal("data")
 | 
				
			||||||
 | 
						if modifiedData.Type() != lua.LTTable {
 | 
				
			||||||
 | 
							processJsonLogger.Error("Expected 'data' to be a table after Lua processing, got %s", modifiedData.Type().String())
 | 
				
			||||||
 | 
							return commands, fmt.Errorf("expected 'data' to be a table after Lua processing")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Convert back to Go interface
 | 
				
			||||||
 | 
						goData, err := FromLua(L, modifiedData)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							processJsonLogger.Error("Failed to convert Lua table back to Go: %v", err)
 | 
				
			||||||
 | 
							return commands, fmt.Errorf("failed to convert Lua table back to Go: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Marshal back to JSON
 | 
				
			||||||
 | 
						modifiedJSON, err := json.MarshalIndent(goData, "", "  ")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							processJsonLogger.Error("Failed to marshal modified data to JSON: %v", err)
 | 
				
			||||||
 | 
							return commands, fmt.Errorf("failed to marshal modified data to JSON: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create replacement command for the entire file
 | 
				
			||||||
 | 
						// For JSON mode, we always replace the entire content
 | 
				
			||||||
 | 
						commands = append(commands, utils.ReplaceCommand{
 | 
				
			||||||
 | 
							From: 0,
 | 
				
			||||||
 | 
							To:   len(content),
 | 
				
			||||||
 | 
							With: string(modifiedJSON),
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						processJsonLogger.Debug("Total JSON processing time: %v", time.Since(startTime))
 | 
				
			||||||
 | 
						processJsonLogger.Debug("Generated %d total modifications", len(commands))
 | 
				
			||||||
 | 
						return commands, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ToLuaTable converts a Go interface{} to a Lua table recursively
 | 
				
			||||||
 | 
					func ToLuaTable(L *lua.LState, data interface{}) (*lua.LTable, error) {
 | 
				
			||||||
 | 
						toLuaTableLogger := jsonLogger.WithPrefix("ToLuaTable")
 | 
				
			||||||
 | 
						toLuaTableLogger.Debug("Converting Go interface to Lua table")
 | 
				
			||||||
 | 
						toLuaTableLogger.Trace("Input data type: %T", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						switch v := data.(type) {
 | 
				
			||||||
 | 
						case map[string]interface{}:
 | 
				
			||||||
 | 
							toLuaTableLogger.Debug("Converting map to Lua table")
 | 
				
			||||||
 | 
							table := L.CreateTable(0, len(v))
 | 
				
			||||||
 | 
							for key, value := range v {
 | 
				
			||||||
 | 
								luaValue, err := ToLuaValue(L, value)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									toLuaTableLogger.Error("Failed to convert map value for key %q: %v", key, err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								table.RawSetString(key, luaValue)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return table, nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case []interface{}:
 | 
				
			||||||
 | 
							toLuaTableLogger.Debug("Converting slice to Lua table")
 | 
				
			||||||
 | 
							table := L.CreateTable(len(v), 0)
 | 
				
			||||||
 | 
							for i, value := range v {
 | 
				
			||||||
 | 
								luaValue, err := ToLuaValue(L, value)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									toLuaTableLogger.Error("Failed to convert slice value at index %d: %v", i, err)
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								table.RawSetInt(i+1, luaValue) // Lua arrays are 1-indexed
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return table, nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case string:
 | 
				
			||||||
 | 
							toLuaTableLogger.Debug("Converting string to Lua string")
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("expected table or array, got string")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case float64:
 | 
				
			||||||
 | 
							toLuaTableLogger.Debug("Converting float64 to Lua number")
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("expected table or array, got number")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case bool:
 | 
				
			||||||
 | 
							toLuaTableLogger.Debug("Converting bool to Lua boolean")
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("expected table or array, got boolean")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case nil:
 | 
				
			||||||
 | 
							toLuaTableLogger.Debug("Converting nil to Lua nil")
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("expected table or array, got nil")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							toLuaTableLogger.Error("Unsupported type for Lua table conversion: %T", v)
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("unsupported type for Lua table conversion: %T", v)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ToLuaValue converts a Go interface{} to a Lua value
 | 
				
			||||||
 | 
					func ToLuaValue(L *lua.LState, data interface{}) (lua.LValue, error) {
 | 
				
			||||||
 | 
						toLuaValueLogger := jsonLogger.WithPrefix("ToLuaValue")
 | 
				
			||||||
 | 
						toLuaValueLogger.Debug("Converting Go interface to Lua value")
 | 
				
			||||||
 | 
						toLuaValueLogger.Trace("Input data type: %T", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						switch v := data.(type) {
 | 
				
			||||||
 | 
						case map[string]interface{}:
 | 
				
			||||||
 | 
							toLuaValueLogger.Debug("Converting map to Lua table")
 | 
				
			||||||
 | 
							table := L.CreateTable(0, len(v))
 | 
				
			||||||
 | 
							for key, value := range v {
 | 
				
			||||||
 | 
								luaValue, err := ToLuaValue(L, value)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									toLuaValueLogger.Error("Failed to convert map value for key %q: %v", key, err)
 | 
				
			||||||
 | 
									return lua.LNil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								table.RawSetString(key, luaValue)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return table, nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case []interface{}:
 | 
				
			||||||
 | 
							toLuaValueLogger.Debug("Converting slice to Lua table")
 | 
				
			||||||
 | 
							table := L.CreateTable(len(v), 0)
 | 
				
			||||||
 | 
							for i, value := range v {
 | 
				
			||||||
 | 
								luaValue, err := ToLuaValue(L, value)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									toLuaValueLogger.Error("Failed to convert slice value at index %d: %v", i, err)
 | 
				
			||||||
 | 
									return lua.LNil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								table.RawSetInt(i+1, luaValue) // Lua arrays are 1-indexed
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return table, nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case string:
 | 
				
			||||||
 | 
							toLuaValueLogger.Debug("Converting string to Lua string")
 | 
				
			||||||
 | 
							return lua.LString(v), nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case float64:
 | 
				
			||||||
 | 
							toLuaValueLogger.Debug("Converting float64 to Lua number")
 | 
				
			||||||
 | 
							return lua.LNumber(v), nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case bool:
 | 
				
			||||||
 | 
							toLuaValueLogger.Debug("Converting bool to Lua boolean")
 | 
				
			||||||
 | 
							return lua.LBool(v), nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case nil:
 | 
				
			||||||
 | 
							toLuaValueLogger.Debug("Converting nil to Lua nil")
 | 
				
			||||||
 | 
							return lua.LNil, nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							toLuaValueLogger.Error("Unsupported type for Lua value conversion: %T", v)
 | 
				
			||||||
 | 
							return lua.LNil, fmt.Errorf("unsupported type for Lua value conversion: %T", v)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										117
									
								
								processor/json_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								processor/json_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,117 @@
 | 
				
			|||||||
 | 
					package processor
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"cook/utils"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestProcessJSON(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name           string
 | 
				
			||||||
 | 
							input          string
 | 
				
			||||||
 | 
							luaExpression  string
 | 
				
			||||||
 | 
							expectedOutput string
 | 
				
			||||||
 | 
							expectedMods   int
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:          "Basic JSON object modification",
 | 
				
			||||||
 | 
								input:         `{"name": "test", "value": 42}`,
 | 
				
			||||||
 | 
								luaExpression: `data.value = data.value * 2; return true`,
 | 
				
			||||||
 | 
								expectedOutput: `{
 | 
				
			||||||
 | 
					  "name": "test",
 | 
				
			||||||
 | 
					  "value": 84
 | 
				
			||||||
 | 
					}`,
 | 
				
			||||||
 | 
								expectedMods: 1,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:          "JSON array modification",
 | 
				
			||||||
 | 
								input:         `{"items": [{"id": 1, "value": 10}, {"id": 2, "value": 20}]}`,
 | 
				
			||||||
 | 
								luaExpression: `for i, item in ipairs(data.items) do data.items[i].value = item.value * 1.5 end; return true`,
 | 
				
			||||||
 | 
								expectedOutput: `{
 | 
				
			||||||
 | 
					  "items": [
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 1,
 | 
				
			||||||
 | 
					      "value": 15
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 2,
 | 
				
			||||||
 | 
					      "value": 30
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  ]
 | 
				
			||||||
 | 
					}`,
 | 
				
			||||||
 | 
								expectedMods: 1,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:          "JSON nested object modification",
 | 
				
			||||||
 | 
								input:         `{"config": {"settings": {"enabled": false, "timeout": 30}}}`,
 | 
				
			||||||
 | 
								luaExpression: `data.config.settings.enabled = true; data.config.settings.timeout = 60; return true`,
 | 
				
			||||||
 | 
								expectedOutput: `{
 | 
				
			||||||
 | 
					  "config": {
 | 
				
			||||||
 | 
					    "settings": {
 | 
				
			||||||
 | 
					      "enabled": true,
 | 
				
			||||||
 | 
					      "timeout": 60
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}`,
 | 
				
			||||||
 | 
								expectedMods: 1,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:           "JSON no modification",
 | 
				
			||||||
 | 
								input:          `{"name": "test", "value": 42}`,
 | 
				
			||||||
 | 
								luaExpression:  `return false`,
 | 
				
			||||||
 | 
								expectedOutput: `{"name": "test", "value": 42}`,
 | 
				
			||||||
 | 
								expectedMods:   0,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								command := utils.ModifyCommand{
 | 
				
			||||||
 | 
									Name: tt.name,
 | 
				
			||||||
 | 
									JSON: true,
 | 
				
			||||||
 | 
									Lua:  tt.luaExpression,
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								modifications, err := ProcessJSON(tt.input, command, "test.json")
 | 
				
			||||||
 | 
								assert.NoError(t, err, "ProcessJSON failed: %v", err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if len(modifications) > 0 {
 | 
				
			||||||
 | 
									// Execute modifications
 | 
				
			||||||
 | 
									result, count := utils.ExecuteModifications(modifications, tt.input)
 | 
				
			||||||
 | 
									assert.Equal(t, tt.expectedMods, count, "Expected %d modifications, got %d", tt.expectedMods, count)
 | 
				
			||||||
 | 
									assert.Equal(t, tt.expectedOutput, result, "Expected output: %s, got: %s", tt.expectedOutput, result)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									assert.Equal(t, 0, tt.expectedMods, "Expected no modifications but got some")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestToLuaValue(t *testing.T) {
 | 
				
			||||||
 | 
						L, err := NewLuaState()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("Failed to create Lua state: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer L.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name     string
 | 
				
			||||||
 | 
							input    interface{}
 | 
				
			||||||
 | 
							expected string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{"string", "hello", "hello"},
 | 
				
			||||||
 | 
							{"number", 42.0, "42"},
 | 
				
			||||||
 | 
							{"boolean", true, "true"},
 | 
				
			||||||
 | 
							{"nil", nil, "nil"},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								result, err := ToLuaValue(L, tt.input)
 | 
				
			||||||
 | 
								assert.NoError(t, err)
 | 
				
			||||||
 | 
								assert.Equal(t, tt.expected, result.String())
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -2,154 +2,147 @@ package processor
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	lua "github.com/yuin/gopher-lua"
 | 
						"cook/utils"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"modify/logger"
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
 | 
						lua "github.com/yuin/gopher-lua"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// processorLogger is a scoped logger for the processor package.
 | 
				
			||||||
 | 
					var processorLogger = logger.Default.WithPrefix("processor")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Maybe we make this an interface again for the shits and giggles
 | 
					// Maybe we make this an interface again for the shits and giggles
 | 
				
			||||||
// We will see, it could easily be...
 | 
					// We will see, it could easily be...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var globalVariables = map[string]interface{}{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func SetVariables(vars map[string]interface{}) {
 | 
				
			||||||
 | 
						for k, v := range vars {
 | 
				
			||||||
 | 
							globalVariables[k] = v
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewLuaState() (*lua.LState, error) {
 | 
					func NewLuaState() (*lua.LState, error) {
 | 
				
			||||||
 | 
						newLStateLogger := processorLogger.WithPrefix("NewLuaState")
 | 
				
			||||||
 | 
						newLStateLogger.Debug("Creating new Lua state")
 | 
				
			||||||
	L := lua.NewState()
 | 
						L := lua.NewState()
 | 
				
			||||||
	// defer L.Close()
 | 
						// defer L.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Load math library
 | 
						// Load math library
 | 
				
			||||||
	logger.Debug("Loading Lua math library")
 | 
						newLStateLogger.Debug("Loading Lua math library")
 | 
				
			||||||
	L.Push(L.GetGlobal("require"))
 | 
						L.Push(L.GetGlobal("require"))
 | 
				
			||||||
	L.Push(lua.LString("math"))
 | 
						L.Push(lua.LString("math"))
 | 
				
			||||||
	if err := L.PCall(1, 1, nil); err != nil {
 | 
						if err := L.PCall(1, 1, nil); err != nil {
 | 
				
			||||||
		logger.Error("Failed to load Lua math library: %v", err)
 | 
							newLStateLogger.Error("Failed to load Lua math library: %v", err)
 | 
				
			||||||
		return nil, fmt.Errorf("error loading Lua math library: %v", err)
 | 
							return nil, fmt.Errorf("error loading Lua math library: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						newLStateLogger.Debug("Lua math library loaded")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Initialize helper functions
 | 
						// Initialize helper functions
 | 
				
			||||||
	logger.Debug("Initializing Lua helper functions")
 | 
						newLStateLogger.Debug("Initializing Lua helper functions")
 | 
				
			||||||
	if err := InitLuaHelpers(L); err != nil {
 | 
						if err := InitLuaHelpers(L); err != nil {
 | 
				
			||||||
		logger.Error("Failed to initialize Lua helper functions: %v", err)
 | 
							newLStateLogger.Error("Failed to initialize Lua helper functions: %v", err)
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						newLStateLogger.Debug("Lua helper functions initialized")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Inject global variables
 | 
				
			||||||
 | 
						if len(globalVariables) > 0 {
 | 
				
			||||||
 | 
							newLStateLogger.Debug("Injecting %d global variables into Lua state", len(globalVariables))
 | 
				
			||||||
 | 
							for k, v := range globalVariables {
 | 
				
			||||||
 | 
								switch val := v.(type) {
 | 
				
			||||||
 | 
								case int:
 | 
				
			||||||
 | 
									L.SetGlobal(k, lua.LNumber(float64(val)))
 | 
				
			||||||
 | 
								case int64:
 | 
				
			||||||
 | 
									L.SetGlobal(k, lua.LNumber(float64(val)))
 | 
				
			||||||
 | 
								case float32:
 | 
				
			||||||
 | 
									L.SetGlobal(k, lua.LNumber(float64(val)))
 | 
				
			||||||
 | 
								case float64:
 | 
				
			||||||
 | 
									L.SetGlobal(k, lua.LNumber(val))
 | 
				
			||||||
 | 
								case string:
 | 
				
			||||||
 | 
									L.SetGlobal(k, lua.LString(val))
 | 
				
			||||||
 | 
								case bool:
 | 
				
			||||||
 | 
									if val {
 | 
				
			||||||
 | 
										L.SetGlobal(k, lua.LTrue)
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										L.SetGlobal(k, lua.LFalse)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									// Fallback to string representation
 | 
				
			||||||
 | 
									L.SetGlobal(k, lua.LString(fmt.Sprintf("%v", val)))
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						newLStateLogger.Debug("New Lua state created successfully")
 | 
				
			||||||
	return L, nil
 | 
						return L, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// func Process(filename string, pattern string, luaExpr string) (int, int, error) {
 | 
					 | 
				
			||||||
// 	logger.Debug("Processing file %q with pattern %q", filename, pattern)
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	// Read file content
 | 
					 | 
				
			||||||
// 	cwd, err := os.Getwd()
 | 
					 | 
				
			||||||
// 	if err != nil {
 | 
					 | 
				
			||||||
// 		logger.Error("Failed to get current working directory: %v", err)
 | 
					 | 
				
			||||||
// 		return 0, 0, fmt.Errorf("error getting current working directory: %v", err)
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	fullPath := filepath.Join(cwd, filename)
 | 
					 | 
				
			||||||
// 	logger.Trace("Reading file from: %s", fullPath)
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	stat, err := os.Stat(fullPath)
 | 
					 | 
				
			||||||
// 	if err != nil {
 | 
					 | 
				
			||||||
// 		logger.Error("Failed to stat file %s: %v", fullPath, err)
 | 
					 | 
				
			||||||
// 		return 0, 0, fmt.Errorf("error getting file info: %v", err)
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
// 	logger.Debug("File size: %d bytes, modified: %s", stat.Size(), stat.ModTime().Format(time.RFC3339))
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	content, err := os.ReadFile(fullPath)
 | 
					 | 
				
			||||||
// 	if err != nil {
 | 
					 | 
				
			||||||
// 		logger.Error("Failed to read file %s: %v", fullPath, err)
 | 
					 | 
				
			||||||
// 		return 0, 0, fmt.Errorf("error reading file: %v", err)
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	fileContent := string(content)
 | 
					 | 
				
			||||||
// 	logger.Trace("File read successfully: %d bytes, hash: %x", len(content), md5sum(content))
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	// Detect and log file type
 | 
					 | 
				
			||||||
// 	fileType := detectFileType(filename, fileContent)
 | 
					 | 
				
			||||||
// 	if fileType != "" {
 | 
					 | 
				
			||||||
// 		logger.Debug("Detected file type: %s", fileType)
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	// Process the content
 | 
					 | 
				
			||||||
// 	logger.Debug("Starting content processing")
 | 
					 | 
				
			||||||
// 	modifiedContent, modCount, matchCount, err := ProcessContent(fileContent, pattern, luaExpr)
 | 
					 | 
				
			||||||
// 	if err != nil {
 | 
					 | 
				
			||||||
// 		logger.Error("Processing error: %v", err)
 | 
					 | 
				
			||||||
// 		return 0, 0, err
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	logger.Debug("Processing results: %d matches, %d modifications", matchCount, modCount)
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	// If we made modifications, save the file
 | 
					 | 
				
			||||||
// 	if modCount > 0 {
 | 
					 | 
				
			||||||
// 		// Calculate changes summary
 | 
					 | 
				
			||||||
// 		changePercent := float64(len(modifiedContent)) / float64(len(fileContent)) * 100
 | 
					 | 
				
			||||||
// 		logger.Info("File size change: %d → %d bytes (%.1f%%)",
 | 
					 | 
				
			||||||
// 			len(fileContent), len(modifiedContent), changePercent)
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 		logger.Debug("Writing modified content to %s", fullPath)
 | 
					 | 
				
			||||||
// 		err = os.WriteFile(fullPath, []byte(modifiedContent), 0644)
 | 
					 | 
				
			||||||
// 		if err != nil {
 | 
					 | 
				
			||||||
// 			logger.Error("Failed to write to file %s: %v", fullPath, err)
 | 
					 | 
				
			||||||
// 			return 0, 0, fmt.Errorf("error writing file: %v", err)
 | 
					 | 
				
			||||||
// 		}
 | 
					 | 
				
			||||||
// 		logger.Debug("File written successfully, new hash: %x", md5sum([]byte(modifiedContent)))
 | 
					 | 
				
			||||||
// 	} else if matchCount > 0 {
 | 
					 | 
				
			||||||
// 		logger.Debug("No content modifications needed for %d matches", matchCount)
 | 
					 | 
				
			||||||
// 	} else {
 | 
					 | 
				
			||||||
// 		logger.Debug("No matches found in file")
 | 
					 | 
				
			||||||
// 	}
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
// 	return modCount, matchCount, nil
 | 
					 | 
				
			||||||
// }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// FromLua converts a Lua table to a struct or map recursively
 | 
					// FromLua converts a Lua table to a struct or map recursively
 | 
				
			||||||
func FromLua(L *lua.LState, luaValue lua.LValue) (interface{}, error) {
 | 
					func FromLua(L *lua.LState, luaValue lua.LValue) (interface{}, error) {
 | 
				
			||||||
 | 
						fromLuaLogger := processorLogger.WithPrefix("FromLua").WithField("luaType", luaValue.Type().String())
 | 
				
			||||||
 | 
						fromLuaLogger.Debug("Converting Lua value to Go interface")
 | 
				
			||||||
	switch v := luaValue.(type) {
 | 
						switch v := luaValue.(type) {
 | 
				
			||||||
	// Well shit...
 | 
					 | 
				
			||||||
	// Tables in lua are both maps and arrays
 | 
					 | 
				
			||||||
	// As arrays they are ordered and as maps, obviously, not
 | 
					 | 
				
			||||||
	// So when we parse them to a go map we fuck up the order for arrays
 | 
					 | 
				
			||||||
	// We have to find a better way....
 | 
					 | 
				
			||||||
	case *lua.LTable:
 | 
						case *lua.LTable:
 | 
				
			||||||
 | 
							fromLuaLogger.Debug("Processing Lua table")
 | 
				
			||||||
		isArray, err := IsLuaTableArray(L, v)
 | 
							isArray, err := IsLuaTableArray(L, v)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 | 
								fromLuaLogger.Error("Failed to determine if Lua table is array: %v", err)
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							fromLuaLogger.Debug("Lua table is array: %t", isArray)
 | 
				
			||||||
		if isArray {
 | 
							if isArray {
 | 
				
			||||||
 | 
								fromLuaLogger.Debug("Converting Lua table to Go array")
 | 
				
			||||||
			result := make([]interface{}, 0)
 | 
								result := make([]interface{}, 0)
 | 
				
			||||||
			v.ForEach(func(key lua.LValue, value lua.LValue) {
 | 
								v.ForEach(func(key lua.LValue, value lua.LValue) {
 | 
				
			||||||
				converted, _ := FromLua(L, value)
 | 
									converted, _ := FromLua(L, value)
 | 
				
			||||||
				result = append(result, converted)
 | 
									result = append(result, converted)
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
 | 
								fromLuaLogger.Trace("Converted Go array: %v", result)
 | 
				
			||||||
			return result, nil
 | 
								return result, nil
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
 | 
								fromLuaLogger.Debug("Converting Lua table to Go map")
 | 
				
			||||||
			result := make(map[string]interface{})
 | 
								result := make(map[string]interface{})
 | 
				
			||||||
			v.ForEach(func(key lua.LValue, value lua.LValue) {
 | 
								v.ForEach(func(key lua.LValue, value lua.LValue) {
 | 
				
			||||||
				converted, _ := FromLua(L, value)
 | 
									converted, _ := FromLua(L, value)
 | 
				
			||||||
				result[key.String()] = converted
 | 
									result[key.String()] = converted
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
 | 
								fromLuaLogger.Trace("Converted Go map: %v", result)
 | 
				
			||||||
			return result, nil
 | 
								return result, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case lua.LString:
 | 
						case lua.LString:
 | 
				
			||||||
 | 
							fromLuaLogger.Debug("Converting Lua string to Go string")
 | 
				
			||||||
 | 
							fromLuaLogger.Trace("Lua string: %q", string(v))
 | 
				
			||||||
		return string(v), nil
 | 
							return string(v), nil
 | 
				
			||||||
	case lua.LBool:
 | 
						case lua.LBool:
 | 
				
			||||||
 | 
							fromLuaLogger.Debug("Converting Lua boolean to Go boolean")
 | 
				
			||||||
 | 
							fromLuaLogger.Trace("Lua boolean: %t", bool(v))
 | 
				
			||||||
		return bool(v), nil
 | 
							return bool(v), nil
 | 
				
			||||||
	case lua.LNumber:
 | 
						case lua.LNumber:
 | 
				
			||||||
 | 
							fromLuaLogger.Debug("Converting Lua number to Go float64")
 | 
				
			||||||
 | 
							fromLuaLogger.Trace("Lua number: %f", float64(v))
 | 
				
			||||||
		return float64(v), nil
 | 
							return float64(v), nil
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
 | 
							fromLuaLogger.Debug("Unsupported Lua type, returning nil")
 | 
				
			||||||
		return nil, nil
 | 
							return nil, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func IsLuaTableArray(L *lua.LState, v *lua.LTable) (bool, error) {
 | 
					func IsLuaTableArray(L *lua.LState, v *lua.LTable) (bool, error) {
 | 
				
			||||||
	logger.Trace("Checking if Lua table is an array")
 | 
						isLuaTableArrayLogger := processorLogger.WithPrefix("IsLuaTableArray")
 | 
				
			||||||
 | 
						isLuaTableArrayLogger.Debug("Checking if Lua table is an array")
 | 
				
			||||||
 | 
						isLuaTableArrayLogger.Trace("Lua table input: %v", v)
 | 
				
			||||||
	L.SetGlobal("table_to_check", v)
 | 
						L.SetGlobal("table_to_check", v)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Use our predefined helper function from InitLuaHelpers
 | 
						// Use our predefined helper function from InitLuaHelpers
 | 
				
			||||||
	err := L.DoString(`is_array = isArray(table_to_check)`)
 | 
						err := L.DoString(`is_array = isArray(table_to_check)`)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Error("Error determining if table is an array: %v", err)
 | 
							isLuaTableArrayLogger.Error("Error determining if table is an array: %v", err)
 | 
				
			||||||
		return false, fmt.Errorf("error determining if table is array: %w", err)
 | 
							return false, fmt.Errorf("error determining if table is array: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -157,13 +150,15 @@ func IsLuaTableArray(L *lua.LState, v *lua.LTable) (bool, error) {
 | 
				
			|||||||
	isArray := L.GetGlobal("is_array")
 | 
						isArray := L.GetGlobal("is_array")
 | 
				
			||||||
	// LVIsFalse returns true if a given LValue is a nil or false otherwise false.
 | 
						// LVIsFalse returns true if a given LValue is a nil or false otherwise false.
 | 
				
			||||||
	result := !lua.LVIsFalse(isArray)
 | 
						result := !lua.LVIsFalse(isArray)
 | 
				
			||||||
	logger.Trace("Lua table is array: %v", result)
 | 
						isLuaTableArrayLogger.Debug("Lua table is array: %t", result)
 | 
				
			||||||
 | 
						isLuaTableArrayLogger.Trace("isArray result Lua value: %v", isArray)
 | 
				
			||||||
	return result, nil
 | 
						return result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// InitLuaHelpers initializes common Lua helper functions
 | 
					// InitLuaHelpers initializes common Lua helper functions
 | 
				
			||||||
func InitLuaHelpers(L *lua.LState) error {
 | 
					func InitLuaHelpers(L *lua.LState) error {
 | 
				
			||||||
	logger.Debug("Loading Lua helper functions")
 | 
						initLuaHelpersLogger := processorLogger.WithPrefix("InitLuaHelpers")
 | 
				
			||||||
 | 
						initLuaHelpersLogger.Debug("Loading Lua helper functions")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	helperScript := `
 | 
						helperScript := `
 | 
				
			||||||
-- Custom Lua helpers for math operations
 | 
					-- Custom Lua helpers for math operations
 | 
				
			||||||
@@ -178,6 +173,7 @@ function ceil(x) return math.ceil(x) end
 | 
				
			|||||||
function upper(s) return string.upper(s) end
 | 
					function upper(s) return string.upper(s) end
 | 
				
			||||||
function lower(s) return string.lower(s) end
 | 
					function lower(s) return string.lower(s) end
 | 
				
			||||||
function format(s, ...) return string.format(s, ...) end
 | 
					function format(s, ...) return string.format(s, ...) end
 | 
				
			||||||
 | 
					function trim(s) return string.gsub(s, "^%s*(.-)%s*$", "%1") end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- String split helper
 | 
					-- String split helper
 | 
				
			||||||
function strsplit(inputstr, sep)
 | 
					function strsplit(inputstr, sep)
 | 
				
			||||||
@@ -243,25 +239,21 @@ end
 | 
				
			|||||||
modified = false
 | 
					modified = false
 | 
				
			||||||
`
 | 
					`
 | 
				
			||||||
	if err := L.DoString(helperScript); err != nil {
 | 
						if err := L.DoString(helperScript); err != nil {
 | 
				
			||||||
		logger.Error("Failed to load Lua helper functions: %v", err)
 | 
							initLuaHelpersLogger.Error("Failed to load Lua helper functions: %v", err)
 | 
				
			||||||
		return fmt.Errorf("error loading helper functions: %v", err)
 | 
							return fmt.Errorf("error loading helper functions: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						initLuaHelpersLogger.Debug("Lua helper functions loaded")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Debug("Setting up Lua print function to Go")
 | 
						initLuaHelpersLogger.Debug("Setting up Lua print function to Go")
 | 
				
			||||||
	L.SetGlobal("print", L.NewFunction(printToGo))
 | 
						L.SetGlobal("print", L.NewFunction(printToGo))
 | 
				
			||||||
 | 
						L.SetGlobal("fetch", L.NewFunction(fetch))
 | 
				
			||||||
 | 
						initLuaHelpersLogger.Debug("Lua print and fetch functions bound to Go")
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// LimitString truncates a string to maxLen and adds "..." if truncated
 | 
					 | 
				
			||||||
func LimitString(s string, maxLen int) string {
 | 
					 | 
				
			||||||
	s = strings.ReplaceAll(s, "\n", "\\n")
 | 
					 | 
				
			||||||
	if len(s) <= maxLen {
 | 
					 | 
				
			||||||
		return s
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return s[:maxLen-3] + "..."
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func PrependLuaAssignment(luaExpr string) string {
 | 
					func PrependLuaAssignment(luaExpr string) string {
 | 
				
			||||||
 | 
						prependLuaAssignmentLogger := processorLogger.WithPrefix("PrependLuaAssignment").WithField("originalLuaExpr", luaExpr)
 | 
				
			||||||
 | 
						prependLuaAssignmentLogger.Debug("Prepending Lua assignment if necessary")
 | 
				
			||||||
	// Auto-prepend v1 for expressions starting with operators
 | 
						// Auto-prepend v1 for expressions starting with operators
 | 
				
			||||||
	if strings.HasPrefix(luaExpr, "*") ||
 | 
						if strings.HasPrefix(luaExpr, "*") ||
 | 
				
			||||||
		strings.HasPrefix(luaExpr, "/") ||
 | 
							strings.HasPrefix(luaExpr, "/") ||
 | 
				
			||||||
@@ -270,30 +262,32 @@ func PrependLuaAssignment(luaExpr string) string {
 | 
				
			|||||||
		strings.HasPrefix(luaExpr, "^") ||
 | 
							strings.HasPrefix(luaExpr, "^") ||
 | 
				
			||||||
		strings.HasPrefix(luaExpr, "%") {
 | 
							strings.HasPrefix(luaExpr, "%") {
 | 
				
			||||||
		luaExpr = "v1 = v1" + luaExpr
 | 
							luaExpr = "v1 = v1" + luaExpr
 | 
				
			||||||
 | 
							prependLuaAssignmentLogger.Debug("Prepended 'v1 = v1' due to operator prefix")
 | 
				
			||||||
	} else if strings.HasPrefix(luaExpr, "=") {
 | 
						} else if strings.HasPrefix(luaExpr, "=") {
 | 
				
			||||||
		// Handle direct assignment with = operator
 | 
							// Handle direct assignment with = operator
 | 
				
			||||||
		luaExpr = "v1 " + luaExpr
 | 
							luaExpr = "v1 " + luaExpr
 | 
				
			||||||
 | 
							prependLuaAssignmentLogger.Debug("Prepended 'v1' due to direct assignment operator")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Add assignment if needed
 | 
						// Add assignment if needed
 | 
				
			||||||
	if !strings.Contains(luaExpr, "=") {
 | 
						if !strings.Contains(luaExpr, "=") {
 | 
				
			||||||
		luaExpr = "v1 = " + luaExpr
 | 
							luaExpr = "v1 = " + luaExpr
 | 
				
			||||||
 | 
							prependLuaAssignmentLogger.Debug("Prepended 'v1 =' as no assignment was found")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						prependLuaAssignmentLogger.Trace("Final Lua expression after prepending: %q", luaExpr)
 | 
				
			||||||
	return luaExpr
 | 
						return luaExpr
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// BuildLuaScript prepares a Lua expression from shorthand notation
 | 
					// BuildLuaScript prepares a Lua expression from shorthand notation
 | 
				
			||||||
func BuildLuaScript(luaExpr string) string {
 | 
					func BuildLuaScript(luaExpr string) string {
 | 
				
			||||||
	logger.Debug("Building Lua script from expression: %s", luaExpr)
 | 
						buildLuaScriptLogger := processorLogger.WithPrefix("BuildLuaScript").WithField("inputLuaExpr", luaExpr)
 | 
				
			||||||
 | 
						buildLuaScriptLogger.Debug("Building full Lua script from expression")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Perform $var substitutions from globalVariables
 | 
				
			||||||
 | 
						luaExpr = replaceVariables(luaExpr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	luaExpr = PrependLuaAssignment(luaExpr)
 | 
						luaExpr = PrependLuaAssignment(luaExpr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// This allows the user to specify whether or not they modified a value
 | 
					 | 
				
			||||||
	// If they do nothing we assume they did modify (no return at all)
 | 
					 | 
				
			||||||
	// If they return before our return then they themselves specify what they did
 | 
					 | 
				
			||||||
	// If nothing is returned lua assumes nil
 | 
					 | 
				
			||||||
	// So we can say our value was modified if the return value is either nil or true
 | 
					 | 
				
			||||||
	// If the return value is false then the user wants to keep the original
 | 
					 | 
				
			||||||
	fullScript := fmt.Sprintf(`
 | 
						fullScript := fmt.Sprintf(`
 | 
				
			||||||
		function run()
 | 
							function run()
 | 
				
			||||||
			%s
 | 
								%s
 | 
				
			||||||
@@ -301,11 +295,60 @@ func BuildLuaScript(luaExpr string) string {
 | 
				
			|||||||
		local res = run()
 | 
							local res = run()
 | 
				
			||||||
		modified = res == nil or res
 | 
							modified = res == nil or res
 | 
				
			||||||
	`, luaExpr)
 | 
						`, luaExpr)
 | 
				
			||||||
 | 
						buildLuaScriptLogger.Trace("Generated full Lua script: %q", utils.LimitString(fullScript, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return fullScript
 | 
						return fullScript
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// BuildJSONLuaScript prepares a Lua expression for JSON mode
 | 
				
			||||||
 | 
					func BuildJSONLuaScript(luaExpr string) string {
 | 
				
			||||||
 | 
						buildJsonLuaScriptLogger := processorLogger.WithPrefix("BuildJSONLuaScript").WithField("inputLuaExpr", luaExpr)
 | 
				
			||||||
 | 
						buildJsonLuaScriptLogger.Debug("Building full Lua script for JSON mode from expression")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Perform $var substitutions from globalVariables
 | 
				
			||||||
 | 
						luaExpr = replaceVariables(luaExpr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fullScript := fmt.Sprintf(`
 | 
				
			||||||
 | 
							function run()
 | 
				
			||||||
 | 
								%s
 | 
				
			||||||
 | 
							end
 | 
				
			||||||
 | 
							local res = run()
 | 
				
			||||||
 | 
							modified = res == nil or res
 | 
				
			||||||
 | 
						`, luaExpr)
 | 
				
			||||||
 | 
						buildJsonLuaScriptLogger.Trace("Generated full JSON Lua script: %q", utils.LimitString(fullScript, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return fullScript
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func replaceVariables(expr string) string {
 | 
				
			||||||
 | 
						// $varName -> literal value
 | 
				
			||||||
 | 
						varNameRe := regexp.MustCompile(`\$(\w+)`)
 | 
				
			||||||
 | 
						return varNameRe.ReplaceAllStringFunc(expr, func(m string) string {
 | 
				
			||||||
 | 
							name := varNameRe.FindStringSubmatch(m)[1]
 | 
				
			||||||
 | 
							if v, ok := globalVariables[name]; ok {
 | 
				
			||||||
 | 
								switch val := v.(type) {
 | 
				
			||||||
 | 
								case int, int64, float32, float64:
 | 
				
			||||||
 | 
									return fmt.Sprintf("%v", val)
 | 
				
			||||||
 | 
								case bool:
 | 
				
			||||||
 | 
									if val {
 | 
				
			||||||
 | 
										return "true"
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										return "false"
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								case string:
 | 
				
			||||||
 | 
									// Quote strings for Lua literal
 | 
				
			||||||
 | 
									return fmt.Sprintf("%q", val)
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									return fmt.Sprintf("%q", fmt.Sprintf("%v", val))
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return m
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func printToGo(L *lua.LState) int {
 | 
					func printToGo(L *lua.LState) int {
 | 
				
			||||||
 | 
						printToGoLogger := processorLogger.WithPrefix("printToGo")
 | 
				
			||||||
 | 
						printToGoLogger.Debug("Lua print function called, redirecting to Go logger")
 | 
				
			||||||
	top := L.GetTop()
 | 
						top := L.GetTop()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	args := make([]interface{}, top)
 | 
						args := make([]interface{}, top)
 | 
				
			||||||
@@ -319,8 +362,122 @@ func printToGo(L *lua.LState) int {
 | 
				
			|||||||
		parts = append(parts, fmt.Sprintf("%v", arg))
 | 
							parts = append(parts, fmt.Sprintf("%v", arg))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	message := strings.Join(parts, " ")
 | 
						message := strings.Join(parts, " ")
 | 
				
			||||||
 | 
						printToGoLogger.Trace("Lua print message: %q", message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Use the LUA log level with a script tag
 | 
						// Use the LUA log level with a script tag
 | 
				
			||||||
	logger.Lua("%s", message)
 | 
						logger.Lua("%s", message)
 | 
				
			||||||
 | 
						printToGoLogger.Debug("Message logged from Lua")
 | 
				
			||||||
	return 0
 | 
						return 0
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func fetch(L *lua.LState) int {
 | 
				
			||||||
 | 
						fetchLogger := processorLogger.WithPrefix("fetch")
 | 
				
			||||||
 | 
						fetchLogger.Debug("Lua fetch function called")
 | 
				
			||||||
 | 
						// Get URL from first argument
 | 
				
			||||||
 | 
						url := L.ToString(1)
 | 
				
			||||||
 | 
						if url == "" {
 | 
				
			||||||
 | 
							fetchLogger.Error("Fetch failed: URL is required")
 | 
				
			||||||
 | 
							L.Push(lua.LNil)
 | 
				
			||||||
 | 
							L.Push(lua.LString("URL is required"))
 | 
				
			||||||
 | 
							return 2
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fetchLogger.Debug("Fetching URL: %q", url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Get options from second argument if provided
 | 
				
			||||||
 | 
						var method string = "GET"
 | 
				
			||||||
 | 
						var headers map[string]string = make(map[string]string)
 | 
				
			||||||
 | 
						var body string = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if L.GetTop() > 1 {
 | 
				
			||||||
 | 
							options := L.ToTable(2)
 | 
				
			||||||
 | 
							if options != nil {
 | 
				
			||||||
 | 
								fetchLogger.Debug("Processing fetch options")
 | 
				
			||||||
 | 
								// Get method
 | 
				
			||||||
 | 
								if methodVal := options.RawGetString("method"); methodVal != lua.LNil {
 | 
				
			||||||
 | 
									method = methodVal.String()
 | 
				
			||||||
 | 
									fetchLogger.Trace("Method from options: %q", method)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Get headers
 | 
				
			||||||
 | 
								if headersVal := options.RawGetString("headers"); headersVal != lua.LNil {
 | 
				
			||||||
 | 
									if headersTable, ok := headersVal.(*lua.LTable); ok {
 | 
				
			||||||
 | 
										fetchLogger.Trace("Processing headers table")
 | 
				
			||||||
 | 
										headersTable.ForEach(func(key lua.LValue, value lua.LValue) {
 | 
				
			||||||
 | 
											headers[key.String()] = value.String()
 | 
				
			||||||
 | 
											fetchLogger.Trace("Header: %q = %q", key.String(), value.String())
 | 
				
			||||||
 | 
										})
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									fetchLogger.Trace("All headers: %v", headers)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Get body
 | 
				
			||||||
 | 
								if bodyVal := options.RawGetString("body"); bodyVal != lua.LNil {
 | 
				
			||||||
 | 
									body = bodyVal.String()
 | 
				
			||||||
 | 
									fetchLogger.Trace("Body from options: %q", utils.LimitString(body, 100))
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fetchLogger.Debug("Fetch request details: Method=%q, URL=%q, BodyLength=%d, Headers=%v", method, url, len(body), headers)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create HTTP request
 | 
				
			||||||
 | 
						req, err := http.NewRequest(method, url, strings.NewReader(body))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fetchLogger.Error("Error creating HTTP request: %v", err)
 | 
				
			||||||
 | 
							L.Push(lua.LNil)
 | 
				
			||||||
 | 
							L.Push(lua.LString(fmt.Sprintf("Error creating request: %v", err)))
 | 
				
			||||||
 | 
							return 2
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Set headers
 | 
				
			||||||
 | 
						for key, value := range headers {
 | 
				
			||||||
 | 
							req.Header.Set(key, value)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fetchLogger.Debug("HTTP request created and headers set")
 | 
				
			||||||
 | 
						fetchLogger.Trace("HTTP Request: %+v", req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Make request
 | 
				
			||||||
 | 
						client := &http.Client{}
 | 
				
			||||||
 | 
						resp, err := client.Do(req)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fetchLogger.Error("Error making HTTP request: %v", err)
 | 
				
			||||||
 | 
							L.Push(lua.LNil)
 | 
				
			||||||
 | 
							L.Push(lua.LString(fmt.Sprintf("Error making request: %v", err)))
 | 
				
			||||||
 | 
							return 2
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer func() {
 | 
				
			||||||
 | 
							fetchLogger.Debug("Closing HTTP response body")
 | 
				
			||||||
 | 
							resp.Body.Close()
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						fetchLogger.Debug("HTTP request executed. Status Code: %d", resp.StatusCode)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Read response body
 | 
				
			||||||
 | 
						bodyBytes, err := io.ReadAll(resp.Body)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fetchLogger.Error("Error reading response body: %v", err)
 | 
				
			||||||
 | 
							L.Push(lua.LNil)
 | 
				
			||||||
 | 
							L.Push(lua.LString(fmt.Sprintf("Error reading response: %v", err)))
 | 
				
			||||||
 | 
							return 2
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fetchLogger.Trace("Response body length: %d", len(bodyBytes))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create response table
 | 
				
			||||||
 | 
						responseTable := L.NewTable()
 | 
				
			||||||
 | 
						responseTable.RawSetString("status", lua.LNumber(resp.StatusCode))
 | 
				
			||||||
 | 
						responseTable.RawSetString("statusText", lua.LString(resp.Status))
 | 
				
			||||||
 | 
						responseTable.RawSetString("ok", lua.LBool(resp.StatusCode >= 200 && resp.StatusCode < 300))
 | 
				
			||||||
 | 
						responseTable.RawSetString("body", lua.LString(string(bodyBytes)))
 | 
				
			||||||
 | 
						fetchLogger.Debug("Created Lua response table")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Set headers in response
 | 
				
			||||||
 | 
						headersTable := L.NewTable()
 | 
				
			||||||
 | 
						for key, values := range resp.Header {
 | 
				
			||||||
 | 
							headersTable.RawSetString(key, lua.LString(values[0]))
 | 
				
			||||||
 | 
							fetchLogger.Trace("Response header: %q = %q", key, values[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						responseTable.RawSetString("headers", headersTable)
 | 
				
			||||||
 | 
						fetchLogger.Trace("Full response table: %v", responseTable)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						L.Push(responseTable)
 | 
				
			||||||
 | 
						fetchLogger.Debug("Pushed response table to Lua stack")
 | 
				
			||||||
 | 
						return 1
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,18 +1,20 @@
 | 
				
			|||||||
package processor
 | 
					package processor
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"cook/utils"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
	lua "github.com/yuin/gopher-lua"
 | 
						lua "github.com/yuin/gopher-lua"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"modify/logger"
 | 
					 | 
				
			||||||
	"modify/utils"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// regexLogger is a scoped logger for the processor/regex package.
 | 
				
			||||||
 | 
					var regexLogger = logger.Default.WithPrefix("processor/regex")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type CaptureGroup struct {
 | 
					type CaptureGroup struct {
 | 
				
			||||||
	Name    string
 | 
						Name    string
 | 
				
			||||||
	Value   string
 | 
						Value   string
 | 
				
			||||||
@@ -21,48 +23,62 @@ type CaptureGroup struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ProcessContent applies regex replacement with Lua processing
 | 
					// ProcessContent applies regex replacement with Lua processing
 | 
				
			||||||
func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) {
 | 
					// The filename here exists ONLY so we can pass it to the lua environment
 | 
				
			||||||
	var commands []utils.ReplaceCommand
 | 
					// It's not used for anything else
 | 
				
			||||||
	logger.Trace("Processing regex: %q", command.Regex)
 | 
					func ProcessRegex(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
 | 
				
			||||||
 | 
						processRegexLogger := regexLogger.WithPrefix("ProcessRegex").WithField("commandName", command.Name).WithField("file", filename)
 | 
				
			||||||
 | 
						processRegexLogger.Debug("Starting regex processing for file")
 | 
				
			||||||
 | 
						processRegexLogger.Trace("Initial file content length: %d", len(content))
 | 
				
			||||||
 | 
						processRegexLogger.Trace("Command details: %+v", command)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var commands []utils.ReplaceCommand
 | 
				
			||||||
	// Start timing the regex processing
 | 
						// Start timing the regex processing
 | 
				
			||||||
	startTime := time.Now()
 | 
						startTime := time.Now()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// We don't HAVE to do this multiple times for a pattern
 | 
						// We don't HAVE to do this multiple times for a pattern
 | 
				
			||||||
	// But it's quick enough for us to not care
 | 
						// But it's quick enough for us to not care
 | 
				
			||||||
	pattern := resolveRegexPlaceholders(command.Regex)
 | 
						pattern := resolveRegexPlaceholders(command.Regex)
 | 
				
			||||||
	logger.Debug("Compiling regex pattern: %s", pattern)
 | 
						processRegexLogger.Debug("Resolved regex placeholders. Pattern: %s", pattern)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// I'm not too happy about having to trim regex, we could have meaningful whitespace or newlines
 | 
				
			||||||
 | 
						// But it's a compromise that allows us to use | in yaml
 | 
				
			||||||
 | 
						// Otherwise we would have to escape every god damn pair of quotation marks
 | 
				
			||||||
 | 
						// And a bunch of other shit
 | 
				
			||||||
 | 
						pattern = strings.TrimSpace(pattern)
 | 
				
			||||||
 | 
						processRegexLogger.Debug("Trimmed regex pattern: %s", pattern)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	patternCompileStart := time.Now()
 | 
						patternCompileStart := time.Now()
 | 
				
			||||||
	compiledPattern, err := regexp.Compile(pattern)
 | 
						compiledPattern, err := regexp.Compile(pattern)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Error("Error compiling pattern: %v", err)
 | 
							processRegexLogger.Error("Error compiling pattern %q: %v", pattern, err)
 | 
				
			||||||
		return commands, fmt.Errorf("error compiling pattern: %v", err)
 | 
							return commands, fmt.Errorf("error compiling pattern: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	logger.Debug("Compiled pattern successfully in %v: %s", time.Since(patternCompileStart), pattern)
 | 
						processRegexLogger.Debug("Compiled pattern successfully in %v", time.Since(patternCompileStart))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Same here, it's just string concatenation, it won't kill us
 | 
						// Same here, it's just string concatenation, it won't kill us
 | 
				
			||||||
	// More important is that we don't fuck up the command
 | 
						// More important is that we don't fuck up the command
 | 
				
			||||||
	// But we shouldn't be able to since it's passed by value
 | 
						// But we shouldn't be able to since it's passed by value
 | 
				
			||||||
	previous := command.Lua
 | 
						previousLuaExpr := command.Lua
 | 
				
			||||||
	luaExpr := BuildLuaScript(command.Lua)
 | 
						luaExpr := BuildLuaScript(command.Lua)
 | 
				
			||||||
	logger.Debug("Transformed Lua expression: %q → %q", previous, luaExpr)
 | 
						processRegexLogger.Debug("Transformed Lua expression: %q → %q", previousLuaExpr, luaExpr)
 | 
				
			||||||
 | 
						processRegexLogger.Trace("Full Lua script: %q", utils.LimitString(luaExpr, 200))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Process all regex matches
 | 
						// Process all regex matches
 | 
				
			||||||
	matchFindStart := time.Now()
 | 
						matchFindStart := time.Now()
 | 
				
			||||||
	indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
 | 
						indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
 | 
				
			||||||
	matchFindDuration := time.Since(matchFindStart)
 | 
						matchFindDuration := time.Since(matchFindStart)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Debug("Found %d matches in content of length %d (search took %v)",
 | 
						processRegexLogger.Debug("Found %d matches in content of length %d (search took %v)",
 | 
				
			||||||
		len(indices), len(content), matchFindDuration)
 | 
							len(indices), len(content), matchFindDuration)
 | 
				
			||||||
 | 
						processRegexLogger.Trace("Match indices: %v", indices)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Log pattern complexity metrics
 | 
						// Log pattern complexity metrics
 | 
				
			||||||
	patternComplexity := estimatePatternComplexity(pattern)
 | 
						patternComplexity := estimatePatternComplexity(pattern)
 | 
				
			||||||
	logger.Debug("Pattern complexity estimate: %d", patternComplexity)
 | 
						processRegexLogger.Debug("Pattern complexity estimate: %d", patternComplexity)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(indices) == 0 {
 | 
						if len(indices) == 0 {
 | 
				
			||||||
		logger.Warning("No matches found for regex: %q", pattern)
 | 
							processRegexLogger.Warning("No matches found for regex: %q", pattern)
 | 
				
			||||||
		logger.Debug("Total regex processing time: %v", time.Since(startTime))
 | 
							processRegexLogger.Debug("Total regex processing time: %v", time.Since(startTime))
 | 
				
			||||||
		return commands, nil
 | 
							return commands, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -71,19 +87,21 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
 | 
				
			|||||||
	// By going backwards we fuck up all the indices to the end of the file that we don't care about
 | 
						// By going backwards we fuck up all the indices to the end of the file that we don't care about
 | 
				
			||||||
	// Because there either aren't any (last match) or they're already modified (subsequent matches)
 | 
						// Because there either aren't any (last match) or they're already modified (subsequent matches)
 | 
				
			||||||
	for i, matchIndices := range indices {
 | 
						for i, matchIndices := range indices {
 | 
				
			||||||
		logger.Debug("Processing match %d of %d", i+1, len(indices))
 | 
							matchLogger := processRegexLogger.WithField("matchNum", i+1)
 | 
				
			||||||
		logger.Trace("Match indices: %v (match position %d-%d)", matchIndices, matchIndices[0], matchIndices[1])
 | 
							matchLogger.Debug("Processing match %d of %d", i+1, len(indices))
 | 
				
			||||||
 | 
							matchLogger.Trace("Match indices: %v (match position %d-%d)", matchIndices, matchIndices[0], matchIndices[1])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		L, err := NewLuaState()
 | 
							L, err := NewLuaState()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			logger.Error("Error creating Lua state: %v", err)
 | 
								matchLogger.Error("Error creating Lua state: %v", err)
 | 
				
			||||||
			return commands, fmt.Errorf("error creating Lua state: %v", err)
 | 
								return commands, fmt.Errorf("error creating Lua state: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							L.SetGlobal("file", lua.LString(filename))
 | 
				
			||||||
		// Hmm... Maybe we don't want to defer this..
 | 
							// Hmm... Maybe we don't want to defer this..
 | 
				
			||||||
		// Maybe we want to close them every iteration
 | 
							// Maybe we want to close them every iteration
 | 
				
			||||||
		// We'll leave it as is for now
 | 
							// We'll leave it as is for now
 | 
				
			||||||
		defer L.Close()
 | 
							defer L.Close()
 | 
				
			||||||
		logger.Trace("Lua state created successfully for match %d", i+1)
 | 
							matchLogger.Trace("Lua state created successfully for match %d", i+1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Why we're doing this whole song and dance of indices is to properly handle empty matches
 | 
							// Why we're doing this whole song and dance of indices is to properly handle empty matches
 | 
				
			||||||
		// Plus it's a little cleaner to surgically replace our matches
 | 
							// Plus it's a little cleaner to surgically replace our matches
 | 
				
			||||||
@@ -92,20 +110,17 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
 | 
				
			|||||||
		// So when we're cutting open the array we say 0:7 + modified + 7:end
 | 
							// So when we're cutting open the array we say 0:7 + modified + 7:end
 | 
				
			||||||
		// As if concatenating in the middle of the array
 | 
							// As if concatenating in the middle of the array
 | 
				
			||||||
		// Plus it supports lookarounds
 | 
							// Plus it supports lookarounds
 | 
				
			||||||
		match := content[matchIndices[0]:matchIndices[1]]
 | 
							matchContent := content[matchIndices[0]:matchIndices[1]]
 | 
				
			||||||
		matchPreview := match
 | 
							matchPreview := utils.LimitString(matchContent, 50)
 | 
				
			||||||
		if len(match) > 50 {
 | 
							matchLogger.Trace("Matched content: %q (length: %d)", matchPreview, len(matchContent))
 | 
				
			||||||
			matchPreview = match[:47] + "..."
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		logger.Trace("Matched content: %q (length: %d)", matchPreview, len(match))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		groups := matchIndices[2:]
 | 
							groups := matchIndices[2:]
 | 
				
			||||||
		if len(groups) <= 0 {
 | 
							if len(groups) <= 0 {
 | 
				
			||||||
			logger.Warning("No capture groups found for match %q and regex %q", matchPreview, pattern)
 | 
								matchLogger.Warning("No capture groups found for match %q and regex %q", matchPreview, pattern)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if len(groups)%2 == 1 {
 | 
							if len(groups)%2 == 1 {
 | 
				
			||||||
			logger.Warning("Invalid number of group indices (%d), should be even: %v", len(groups), groups)
 | 
								matchLogger.Warning("Invalid number of group indices (%d), should be even: %v", len(groups), groups)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -116,11 +131,11 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
 | 
				
			|||||||
				validGroups++
 | 
									validGroups++
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Debug("Found %d valid capture groups in match", validGroups)
 | 
							matchLogger.Debug("Found %d valid capture groups in match", validGroups)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for _, index := range groups {
 | 
							for _, index := range groups {
 | 
				
			||||||
			if index == -1 {
 | 
								if index == -1 {
 | 
				
			||||||
				logger.Warning("Negative index encountered in match indices %v. This may indicate an issue with the regex pattern or an empty/optional capture group.", matchIndices)
 | 
									matchLogger.Warning("Negative index encountered in match indices %v. This may indicate an issue with the regex pattern or an empty/optional capture group.", matchIndices)
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -135,6 +150,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
 | 
				
			|||||||
			start := groups[i*2]
 | 
								start := groups[i*2]
 | 
				
			||||||
			end := groups[i*2+1]
 | 
								end := groups[i*2+1]
 | 
				
			||||||
			if start == -1 || end == -1 {
 | 
								if start == -1 || end == -1 {
 | 
				
			||||||
 | 
									matchLogger.Debug("Skipping empty or unmatched capture group #%d (name: %q)", i+1, name)
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -147,71 +163,80 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			// Include name info in log if available
 | 
								// Include name info in log if available
 | 
				
			||||||
			if name != "" {
 | 
								if name != "" {
 | 
				
			||||||
				logger.Trace("Capture group '%s': %q (pos %d-%d)", name, value, start, end)
 | 
									matchLogger.Trace("Capture group '%s': %q (pos %d-%d)", name, value, start, end)
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				logger.Trace("Capture group #%d: %q (pos %d-%d)", i+1, value, start, end)
 | 
									matchLogger.Trace("Capture group #%d: %q (pos %d-%d)", i+1, value, start, end)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Use the DeduplicateGroups flag to control whether to deduplicate capture groups
 | 
				
			||||||
 | 
							if !command.NoDedup {
 | 
				
			||||||
 | 
								matchLogger.Debug("Deduplicating capture groups as specified in command settings")
 | 
				
			||||||
			captureGroups = deduplicateGroups(captureGroups)
 | 
								captureGroups = deduplicateGroups(captureGroups)
 | 
				
			||||||
 | 
								matchLogger.Trace("Capture groups after deduplication: %v", captureGroups)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								matchLogger.Debug("Skipping deduplication of capture groups (NoDedup is true)")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := toLua(L, captureGroups); err != nil {
 | 
							if err := toLua(L, captureGroups); err != nil {
 | 
				
			||||||
			logger.Error("Failed to set Lua variables: %v", err)
 | 
								matchLogger.Error("Failed to set Lua variables for capture groups: %v", err)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Trace("Set %d capture groups as Lua variables", len(captureGroups))
 | 
							matchLogger.Debug("Set %d capture groups as Lua variables", len(captureGroups))
 | 
				
			||||||
 | 
							matchLogger.Trace("Lua globals set for capture groups")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := L.DoString(luaExpr); err != nil {
 | 
							if err := L.DoString(luaExpr); err != nil {
 | 
				
			||||||
			logger.Error("Lua script execution failed: %v\nScript: %s\nCapture Groups: %+v",
 | 
								matchLogger.Error("Lua script execution failed: %v\nScript: %s\nCapture Groups: %+v",
 | 
				
			||||||
				err, luaExpr, captureGroups)
 | 
									err, utils.LimitString(luaExpr, 200), captureGroups)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Trace("Lua script executed successfully")
 | 
							matchLogger.Debug("Lua script executed successfully")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Get modifications from Lua
 | 
							// Get modifications from Lua
 | 
				
			||||||
		captureGroups, err = fromLua(L, captureGroups)
 | 
							updatedCaptureGroups, err := fromLua(L, captureGroups)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			logger.Error("Failed to retrieve modifications from Lua: %v", err)
 | 
								matchLogger.Error("Failed to retrieve modifications from Lua: %v", err)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Trace("Retrieved updated values from Lua")
 | 
							matchLogger.Debug("Retrieved updated values from Lua")
 | 
				
			||||||
 | 
							matchLogger.Trace("Updated capture groups from Lua: %v", updatedCaptureGroups)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		replacement := ""
 | 
							replacement := ""
 | 
				
			||||||
		replacementVar := L.GetGlobal("replacement")
 | 
							replacementVar := L.GetGlobal("replacement")
 | 
				
			||||||
		if replacementVar.Type() != lua.LTNil {
 | 
							if replacementVar.Type() != lua.LTNil {
 | 
				
			||||||
			replacement = replacementVar.String()
 | 
								replacement = replacementVar.String()
 | 
				
			||||||
			logger.Debug("Using global replacement: %q", replacement)
 | 
								matchLogger.Debug("Using global replacement variable from Lua: %q", replacement)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Check if modification flag is set
 | 
							// Check if modification flag is set
 | 
				
			||||||
		modifiedVal := L.GetGlobal("modified")
 | 
							modifiedVal := L.GetGlobal("modified")
 | 
				
			||||||
		if modifiedVal.Type() != lua.LTBool || !lua.LVAsBool(modifiedVal) {
 | 
							if modifiedVal.Type() != lua.LTBool || !lua.LVAsBool(modifiedVal) {
 | 
				
			||||||
			logger.Debug("Skipping match - no modifications made by Lua script")
 | 
								matchLogger.Debug("Skipping match - no modifications indicated by Lua script")
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if replacement == "" {
 | 
							if replacement == "" {
 | 
				
			||||||
			// Apply the modifications to the original match
 | 
								// Apply the modifications to the original match
 | 
				
			||||||
			replacement = match
 | 
								replacement = matchContent
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Count groups that were actually modified
 | 
								// Count groups that were actually modified
 | 
				
			||||||
			modifiedGroups := 0
 | 
								modifiedGroupsCount := 0
 | 
				
			||||||
			for _, capture := range captureGroups {
 | 
								for _, capture := range updatedCaptureGroups {
 | 
				
			||||||
				if capture.Value != capture.Updated {
 | 
									if capture.Value != capture.Updated {
 | 
				
			||||||
					modifiedGroups++
 | 
										modifiedGroupsCount++
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			logger.Info("%d of %d capture groups identified for modification", modifiedGroups, len(captureGroups))
 | 
								matchLogger.Info("%d of %d capture groups identified for modification", modifiedGroupsCount, len(updatedCaptureGroups))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			for _, capture := range captureGroups {
 | 
								for _, capture := range updatedCaptureGroups {
 | 
				
			||||||
				if capture.Value == capture.Updated {
 | 
									if capture.Value == capture.Updated {
 | 
				
			||||||
					logger.Info("Capture group unchanged: %s", capture.Value)
 | 
										matchLogger.Debug("Capture group unchanged: %s", utils.LimitString(capture.Value, 50))
 | 
				
			||||||
					continue
 | 
										continue
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// Log what changed with context
 | 
									// Log what changed with context
 | 
				
			||||||
				logger.Debug("Capture group %s scheduled for modification: %q → %q",
 | 
									matchLogger.Debug("Capture group %q scheduled for modification: %q → %q",
 | 
				
			||||||
					capture.Name, capture.Value, capture.Updated)
 | 
										capture.Name, utils.LimitString(capture.Value, 50), utils.LimitString(capture.Updated, 50))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// Indices of the group are relative to content
 | 
									// Indices of the group are relative to content
 | 
				
			||||||
				// To relate them to match we have to subtract the match start index
 | 
									// To relate them to match we have to subtract the match start index
 | 
				
			||||||
@@ -221,42 +246,57 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
 | 
				
			|||||||
					To:   capture.Range[1],
 | 
										To:   capture.Range[1],
 | 
				
			||||||
					With: capture.Updated,
 | 
										With: capture.Updated,
 | 
				
			||||||
				})
 | 
									})
 | 
				
			||||||
 | 
									matchLogger.Trace("Added replacement command: %+v", commands[len(commands)-1])
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
 | 
								matchLogger.Debug("Using full replacement string from Lua: %q", utils.LimitString(replacement, 50))
 | 
				
			||||||
			commands = append(commands, utils.ReplaceCommand{
 | 
								commands = append(commands, utils.ReplaceCommand{
 | 
				
			||||||
				From: matchIndices[0],
 | 
									From: matchIndices[0],
 | 
				
			||||||
				To:   matchIndices[1],
 | 
									To:   matchIndices[1],
 | 
				
			||||||
				With: replacement,
 | 
									With: replacement,
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
 | 
								matchLogger.Trace("Added full replacement command: %+v", commands[len(commands)-1])
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Debug("Total regex processing time: %v", time.Since(startTime))
 | 
						processRegexLogger.Debug("Total regex processing time: %v", time.Since(startTime))
 | 
				
			||||||
 | 
						processRegexLogger.Debug("Generated %d total modifications", len(commands))
 | 
				
			||||||
	return commands, nil
 | 
						return commands, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func deduplicateGroups(captureGroups []*CaptureGroup) []*CaptureGroup {
 | 
					func deduplicateGroups(captureGroups []*CaptureGroup) []*CaptureGroup {
 | 
				
			||||||
	deduplicatedGroups := make([]*CaptureGroup, 0)
 | 
						deduplicateGroupsLogger := regexLogger.WithPrefix("deduplicateGroups")
 | 
				
			||||||
 | 
						deduplicateGroupsLogger.Debug("Starting deduplication of capture groups")
 | 
				
			||||||
 | 
						deduplicateGroupsLogger.Trace("Input capture groups: %v", captureGroups)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Preserve input order and drop any group that overlaps with an already accepted group
 | 
				
			||||||
 | 
						accepted := make([]*CaptureGroup, 0, len(captureGroups))
 | 
				
			||||||
	for _, group := range captureGroups {
 | 
						for _, group := range captureGroups {
 | 
				
			||||||
 | 
							groupLogger := deduplicateGroupsLogger.WithField("groupName", group.Name).WithField("groupRange", group.Range)
 | 
				
			||||||
 | 
							groupLogger.Debug("Processing capture group")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		overlaps := false
 | 
							overlaps := false
 | 
				
			||||||
		logger.Debug("Checking capture group: %s with range %v", group.Name, group.Range)
 | 
							for _, kept := range accepted {
 | 
				
			||||||
		for _, existingGroup := range deduplicatedGroups {
 | 
								// Overlap if start < keptEnd and end > keptStart (adjacent is allowed)
 | 
				
			||||||
			logger.Debug("Comparing with existing group: %s with range %v", existingGroup.Name, existingGroup.Range)
 | 
								if group.Range[0] < kept.Range[1] && group.Range[1] > kept.Range[0] {
 | 
				
			||||||
			if group.Range[0] < existingGroup.Range[1] && group.Range[1] > existingGroup.Range[0] {
 | 
					 | 
				
			||||||
				overlaps = true
 | 
									overlaps = true
 | 
				
			||||||
				logger.Warning("Detected overlap between capture group '%s' and existing group '%s' in range %v-%v and %v-%v", group.Name, existingGroup.Name, group.Range[0], group.Range[1], existingGroup.Range[0], existingGroup.Range[1])
 | 
					 | 
				
			||||||
				break
 | 
									break
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if overlaps {
 | 
							if overlaps {
 | 
				
			||||||
			// We CAN just continue despite this fuckup
 | 
								groupLogger.Warning("Overlapping capture group detected and skipped.")
 | 
				
			||||||
			logger.Warning("Overlapping capture group: %s", group.Name)
 | 
					 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Debug("No overlap detected for capture group: %s. Adding to deduplicated groups.", group.Name)
 | 
					
 | 
				
			||||||
		deduplicatedGroups = append(deduplicatedGroups, group)
 | 
							groupLogger.Debug("Capture group does not overlap with previously accepted groups. Adding.")
 | 
				
			||||||
 | 
							accepted = append(accepted, group)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return deduplicatedGroups
 | 
					
 | 
				
			||||||
 | 
						deduplicateGroupsLogger.Debug("Finished deduplication. Original %d groups, %d deduplicated.", len(captureGroups), len(accepted))
 | 
				
			||||||
 | 
						deduplicateGroupsLogger.Trace("Deduplicated groups: %v", accepted)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return accepted
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// The order of these replaces is important
 | 
					// The order of these replaces is important
 | 
				
			||||||
@@ -265,105 +305,183 @@ func deduplicateGroups(captureGroups []*CaptureGroup) []*CaptureGroup {
 | 
				
			|||||||
// Expand to another capture group in the capture group
 | 
					// Expand to another capture group in the capture group
 | 
				
			||||||
// We really only want one (our named) capture group
 | 
					// We really only want one (our named) capture group
 | 
				
			||||||
func resolveRegexPlaceholders(pattern string) string {
 | 
					func resolveRegexPlaceholders(pattern string) string {
 | 
				
			||||||
 | 
						resolveLogger := regexLogger.WithPrefix("resolveRegexPlaceholders").WithField("originalPattern", utils.LimitString(pattern, 100))
 | 
				
			||||||
 | 
						resolveLogger.Debug("Resolving regex placeholders in pattern")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Handle special pattern modifications
 | 
						// Handle special pattern modifications
 | 
				
			||||||
	if !strings.HasPrefix(pattern, "(?s)") {
 | 
						if !strings.HasPrefix(pattern, "(?s)") {
 | 
				
			||||||
		pattern = "(?s)" + pattern
 | 
							pattern = "(?s)" + pattern
 | 
				
			||||||
 | 
							resolveLogger.Debug("Prepended '(?s)' to pattern for single-line mode")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)
 | 
						namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)
 | 
				
			||||||
	pattern = namedGroupNum.ReplaceAllStringFunc(pattern, func(match string) string {
 | 
						pattern = namedGroupNum.ReplaceAllStringFunc(pattern, func(match string) string {
 | 
				
			||||||
 | 
							funcLogger := resolveLogger.WithPrefix("namedGroupNumReplace").WithField("match", utils.LimitString(match, 50))
 | 
				
			||||||
 | 
							funcLogger.Debug("Processing named group !num placeholder")
 | 
				
			||||||
		parts := namedGroupNum.FindStringSubmatch(match)
 | 
							parts := namedGroupNum.FindStringSubmatch(match)
 | 
				
			||||||
		if len(parts) != 3 {
 | 
							if len(parts) != 3 {
 | 
				
			||||||
 | 
								funcLogger.Warning("Unexpected number of submatches for namedGroupNum: %d. Returning original match.", len(parts))
 | 
				
			||||||
			return match
 | 
								return match
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		replacement := `-?\d*\.?\d+`
 | 
							replacement := `-?\d*\.?\d+`
 | 
				
			||||||
 | 
							funcLogger.Trace("Replacing !num in named group with: %q", replacement)
 | 
				
			||||||
		return parts[1] + replacement
 | 
							return parts[1] + replacement
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
						resolveLogger.Debug("Handled named group !num placeholders")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pattern = strings.ReplaceAll(pattern, "!num", `(-?\d*\.?\d+)`)
 | 
						pattern = strings.ReplaceAll(pattern, "!num", `(-?\d*\.?\d+)`)
 | 
				
			||||||
 | 
						resolveLogger.Debug("Replaced !num with numeric capture group")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pattern = strings.ReplaceAll(pattern, "!any", `.*?`)
 | 
						pattern = strings.ReplaceAll(pattern, "!any", `.*?`)
 | 
				
			||||||
 | 
						resolveLogger.Debug("Replaced !any with non-greedy wildcard")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	repPattern := regexp.MustCompile(`!rep\(([^,]+),\s*(\d+)\)`)
 | 
						repPattern := regexp.MustCompile(`!rep\(([^,]+),\s*(\d+)\)`)
 | 
				
			||||||
	// !rep(pattern, count) repeats the pattern n times
 | 
						// !rep(pattern, count) repeats the pattern n times
 | 
				
			||||||
	// Inserting !any between each repetition
 | 
						// Inserting !any between each repetition
 | 
				
			||||||
	pattern = repPattern.ReplaceAllStringFunc(pattern, func(match string) string {
 | 
						pattern = repPattern.ReplaceAllStringFunc(pattern, func(match string) string {
 | 
				
			||||||
 | 
							funcLogger := resolveLogger.WithPrefix("repPatternReplace").WithField("match", utils.LimitString(match, 50))
 | 
				
			||||||
 | 
							funcLogger.Debug("Processing !rep placeholder")
 | 
				
			||||||
		parts := repPattern.FindStringSubmatch(match)
 | 
							parts := repPattern.FindStringSubmatch(match)
 | 
				
			||||||
		if len(parts) != 3 {
 | 
							if len(parts) != 3 {
 | 
				
			||||||
 | 
								funcLogger.Warning("Unexpected number of submatches for repPattern: %d. Returning original match.", len(parts))
 | 
				
			||||||
			return match
 | 
								return match
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		repeatedPattern := parts[1]
 | 
							repeatedPattern := parts[1]
 | 
				
			||||||
		count := parts[2]
 | 
							countStr := parts[2]
 | 
				
			||||||
		repetitions, _ := strconv.Atoi(count)
 | 
							repetitions, err := strconv.Atoi(countStr)
 | 
				
			||||||
		return strings.Repeat(repeatedPattern+".*?", repetitions-1) + repeatedPattern
 | 
							if err != nil {
 | 
				
			||||||
 | 
								funcLogger.Error("Failed to parse repetition count %q: %v. Returning original match.", countStr, err)
 | 
				
			||||||
 | 
								return match
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var finalReplacement string
 | 
				
			||||||
 | 
							if repetitions > 0 {
 | 
				
			||||||
 | 
								finalReplacement = strings.Repeat(repeatedPattern+".*?", repetitions-1) + repeatedPattern
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								finalReplacement = ""
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							funcLogger.Trace("Replaced !rep with %d repetitions of %q: %q", repetitions, utils.LimitString(repeatedPattern, 30), utils.LimitString(finalReplacement, 100))
 | 
				
			||||||
 | 
							return finalReplacement
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
						resolveLogger.Debug("Handled !rep placeholders")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						resolveLogger.Debug("Finished resolving regex placeholders")
 | 
				
			||||||
 | 
						resolveLogger.Trace("Final resolved pattern: %q", utils.LimitString(pattern, 100))
 | 
				
			||||||
	return pattern
 | 
						return pattern
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ToLua sets capture groups as Lua variables (v1, v2, etc. for numeric values and s1, s2, etc. for strings)
 | 
					// ToLua sets capture groups as Lua variables (v1, v2, etc. for numeric values and s1, s2, etc. for strings)
 | 
				
			||||||
func toLua(L *lua.LState, data interface{}) error {
 | 
					func toLua(L *lua.LState, data interface{}) error {
 | 
				
			||||||
 | 
						toLuaLogger := regexLogger.WithPrefix("toLua")
 | 
				
			||||||
 | 
						toLuaLogger.Debug("Setting capture groups as Lua variables")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	captureGroups, ok := data.([]*CaptureGroup)
 | 
						captureGroups, ok := data.([]*CaptureGroup)
 | 
				
			||||||
	if !ok {
 | 
						if !ok {
 | 
				
			||||||
 | 
							toLuaLogger.Error("Invalid data type for toLua. Expected []*CaptureGroup, got %T", data)
 | 
				
			||||||
		return fmt.Errorf("expected []*CaptureGroup for captures, got %T", data)
 | 
							return fmt.Errorf("expected []*CaptureGroup for captures, got %T", data)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						toLuaLogger.Trace("Input capture groups: %v", captureGroups)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	groupindex := 0
 | 
						groupindex := 0
 | 
				
			||||||
	for _, capture := range captureGroups {
 | 
						for _, capture := range captureGroups {
 | 
				
			||||||
 | 
							groupLogger := toLuaLogger.WithField("captureGroup", capture.Name).WithField("value", utils.LimitString(capture.Value, 50))
 | 
				
			||||||
 | 
							groupLogger.Debug("Processing capture group for Lua")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if capture.Name == "" {
 | 
							if capture.Name == "" {
 | 
				
			||||||
			// We don't want to change the name of the capture group
 | 
								// We don't want to change the name of the capture group
 | 
				
			||||||
			// Even if it's empty
 | 
								// Even if it's empty
 | 
				
			||||||
			tempName := fmt.Sprintf("%d", groupindex+1)
 | 
								tempName := fmt.Sprintf("%d", groupindex+1)
 | 
				
			||||||
			groupindex++
 | 
								groupindex++
 | 
				
			||||||
 | 
								groupLogger.Debug("Unnamed capture group, assigning temporary name: %q", tempName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			L.SetGlobal("s"+tempName, lua.LString(capture.Value))
 | 
								L.SetGlobal("s"+tempName, lua.LString(capture.Value))
 | 
				
			||||||
 | 
								groupLogger.Trace("Set Lua global s%s = %q", tempName, capture.Value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			val, err := strconv.ParseFloat(capture.Value, 64)
 | 
								val, err := strconv.ParseFloat(capture.Value, 64)
 | 
				
			||||||
			if err == nil {
 | 
								if err == nil {
 | 
				
			||||||
				L.SetGlobal("v"+tempName, lua.LNumber(val))
 | 
									L.SetGlobal("v"+tempName, lua.LNumber(val))
 | 
				
			||||||
 | 
									groupLogger.Trace("Set Lua global v%s = %f", tempName, val)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									groupLogger.Trace("Value %q is not numeric, skipping v%s assignment", capture.Value, tempName)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			val, err := strconv.ParseFloat(capture.Value, 64)
 | 
								val, err := strconv.ParseFloat(capture.Value, 64)
 | 
				
			||||||
			if err == nil {
 | 
								if err == nil {
 | 
				
			||||||
				L.SetGlobal(capture.Name, lua.LNumber(val))
 | 
									L.SetGlobal(capture.Name, lua.LNumber(val))
 | 
				
			||||||
 | 
									groupLogger.Trace("Set Lua global %s = %f (numeric)", capture.Name, val)
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				L.SetGlobal(capture.Name, lua.LString(capture.Value))
 | 
									L.SetGlobal(capture.Name, lua.LString(capture.Value))
 | 
				
			||||||
 | 
									groupLogger.Trace("Set Lua global %s = %q (string)", capture.Name, capture.Value)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						toLuaLogger.Debug("Finished setting capture groups as Lua variables")
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FromLua implements the Processor interface for RegexProcessor
 | 
					// FromLua implements the Processor interface for RegexProcessor
 | 
				
			||||||
func fromLua(L *lua.LState, captureGroups []*CaptureGroup) ([]*CaptureGroup, error) {
 | 
					func fromLua(L *lua.LState, captureGroups []*CaptureGroup) ([]*CaptureGroup, error) {
 | 
				
			||||||
 | 
						fromLuaLogger := regexLogger.WithPrefix("fromLua")
 | 
				
			||||||
 | 
						fromLuaLogger.Debug("Retrieving modifications from Lua for capture groups")
 | 
				
			||||||
 | 
						fromLuaLogger.Trace("Initial capture groups: %v", captureGroups)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	captureIndex := 0
 | 
						captureIndex := 0
 | 
				
			||||||
	for _, capture := range captureGroups {
 | 
						for _, capture := range captureGroups {
 | 
				
			||||||
		if capture.Name == "" {
 | 
							groupLogger := fromLuaLogger.WithField("originalCaptureName", capture.Name).WithField("originalValue", utils.LimitString(capture.Value, 50))
 | 
				
			||||||
			capture.Name = fmt.Sprintf("%d", captureIndex+1)
 | 
							groupLogger.Debug("Processing capture group to retrieve updated value")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			vVarName := fmt.Sprintf("v%s", capture.Name)
 | 
							if capture.Name == "" {
 | 
				
			||||||
			sVarName := fmt.Sprintf("s%s", capture.Name)
 | 
								// This case means it was an unnamed capture group originally.
 | 
				
			||||||
 | 
								// We need to reconstruct the original temporary name to fetch its updated value.
 | 
				
			||||||
 | 
								// The name will be set to an integer if it was empty, then incremented.
 | 
				
			||||||
 | 
								// So, we use the captureIndex to get the correct 'vX' and 'sX' variables.
 | 
				
			||||||
 | 
								tempName := fmt.Sprintf("%d", captureIndex+1)
 | 
				
			||||||
 | 
								groupLogger.Debug("Retrieving updated value for unnamed group (temp name: %q)", tempName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								vVarName := fmt.Sprintf("v%s", tempName)
 | 
				
			||||||
 | 
								sVarName := fmt.Sprintf("s%s", tempName)
 | 
				
			||||||
			captureIndex++
 | 
								captureIndex++
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			vLuaVal := L.GetGlobal(vVarName)
 | 
								vLuaVal := L.GetGlobal(vVarName)
 | 
				
			||||||
			sLuaVal := L.GetGlobal(sVarName)
 | 
								sLuaVal := L.GetGlobal(sVarName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								groupLogger.Trace("Lua values for unnamed group: v=%v, s=%v", vLuaVal, sLuaVal)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if sLuaVal.Type() == lua.LTString {
 | 
								if sLuaVal.Type() == lua.LTString {
 | 
				
			||||||
				capture.Updated = sLuaVal.String()
 | 
									capture.Updated = sLuaVal.String()
 | 
				
			||||||
 | 
									groupLogger.Trace("Updated value from s%s (string): %q", tempName, capture.Updated)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// Numbers have priority
 | 
								// Numbers have priority
 | 
				
			||||||
			if vLuaVal.Type() == lua.LTNumber {
 | 
								if vLuaVal.Type() == lua.LTNumber {
 | 
				
			||||||
				capture.Updated = vLuaVal.String()
 | 
									capture.Updated = vLuaVal.String()
 | 
				
			||||||
 | 
									groupLogger.Trace("Updated value from v%s (numeric): %q", tempName, capture.Updated)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			// Easy shit
 | 
								// Easy shit, directly use the named capture group
 | 
				
			||||||
			capture.Updated = L.GetGlobal(capture.Name).String()
 | 
								updatedValue := L.GetGlobal(capture.Name)
 | 
				
			||||||
 | 
								if updatedValue.Type() != lua.LTNil {
 | 
				
			||||||
 | 
									capture.Updated = updatedValue.String()
 | 
				
			||||||
 | 
									groupLogger.Trace("Updated value for named group %q: %q", capture.Name, capture.Updated)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									groupLogger.Debug("Named capture group %q not found in Lua globals or is nil. Keeping original value.", capture.Name)
 | 
				
			||||||
 | 
									capture.Updated = capture.Value // Keep original if not found or nil
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							groupLogger.Debug("Finished processing capture group. Original: %q, Updated: %q", utils.LimitString(capture.Value, 50), utils.LimitString(capture.Updated, 50))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fromLuaLogger.Debug("Finished retrieving modifications from Lua")
 | 
				
			||||||
 | 
						fromLuaLogger.Trace("Final updated capture groups: %v", captureGroups)
 | 
				
			||||||
	return captureGroups, nil
 | 
						return captureGroups, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// estimatePatternComplexity gives a rough estimate of regex pattern complexity
 | 
					// estimatePatternComplexity gives a rough estimate of regex pattern complexity
 | 
				
			||||||
// This can help identify potentially problematic patterns
 | 
					// This can help identify potentially problematic patterns
 | 
				
			||||||
func estimatePatternComplexity(pattern string) int {
 | 
					func estimatePatternComplexity(pattern string) int {
 | 
				
			||||||
 | 
						estimateComplexityLogger := regexLogger.WithPrefix("estimatePatternComplexity").WithField("pattern", utils.LimitString(pattern, 100))
 | 
				
			||||||
 | 
						estimateComplexityLogger.Debug("Estimating regex pattern complexity")
 | 
				
			||||||
	complexity := len(pattern)
 | 
						complexity := len(pattern)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Add complexity for potentially expensive operations
 | 
						// Add complexity for potentially expensive operations
 | 
				
			||||||
@@ -376,5 +494,6 @@ func estimatePatternComplexity(pattern string) int {
 | 
				
			|||||||
	complexity += strings.Count(pattern, "\\1") * 3 // Backreferences
 | 
						complexity += strings.Count(pattern, "\\1") * 3 // Backreferences
 | 
				
			||||||
	complexity += strings.Count(pattern, "{") * 2   // Counted repetition
 | 
						complexity += strings.Count(pattern, "{") * 2   // Counted repetition
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						estimateComplexityLogger.Debug("Estimated pattern complexity: %d", complexity)
 | 
				
			||||||
	return complexity
 | 
						return complexity
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,9 +2,8 @@ package processor
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"fmt"
 | 
						"cook/utils"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"modify/utils"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@@ -38,7 +37,7 @@ func ApiAdaptor(content string, regex string, lua string) (string, int, int, err
 | 
				
			|||||||
		LogLevel: "TRACE",
 | 
							LogLevel: "TRACE",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	commands, err := ProcessRegex(content, command)
 | 
						commands, err := ProcessRegex(content, command, "test")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return "", 0, 0, err
 | 
							return "", 0, 0, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,8 +2,9 @@ package processor
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"modify/logger"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
@@ -20,7 +21,7 @@ func init() {
 | 
				
			|||||||
		if disableTestLogs {
 | 
							if disableTestLogs {
 | 
				
			||||||
			// Create a new logger that writes to nowhere
 | 
								// Create a new logger that writes to nowhere
 | 
				
			||||||
			silentLogger := logger.New(io.Discard, "", 0)
 | 
								silentLogger := logger.New(io.Discard, "", 0)
 | 
				
			||||||
			logger.DefaultLogger = silentLogger
 | 
								logger.Default = silentLogger
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,8 @@
 | 
				
			|||||||
package regression
 | 
					package regression
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"modify/processor"
 | 
						"cook/processor"
 | 
				
			||||||
	"modify/utils"
 | 
						"cook/utils"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
@@ -15,7 +15,7 @@ func ApiAdaptor(content string, regex string, lua string) (string, int, int, err
 | 
				
			|||||||
		LogLevel: "TRACE",
 | 
							LogLevel: "TRACE",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	commands, err := processor.ProcessRegex(content, command)
 | 
						commands, err := processor.ProcessRegex(content, command, "test")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return "", 0, 0, err
 | 
							return "", 0, 0, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										157
									
								
								utils/db.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										157
									
								
								utils/db.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,157 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
 | 
						"gorm.io/driver/sqlite"
 | 
				
			||||||
 | 
						"gorm.io/gorm"
 | 
				
			||||||
 | 
						gormlogger "gorm.io/gorm/logger"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// dbLogger is a scoped logger for the utils/db package.
 | 
				
			||||||
 | 
					var dbLogger = logger.Default.WithPrefix("utils/db")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type DB interface {
 | 
				
			||||||
 | 
						DB() *gorm.DB
 | 
				
			||||||
 | 
						Raw(sql string, args ...any) *gorm.DB
 | 
				
			||||||
 | 
						SaveFile(filePath string, fileData []byte) error
 | 
				
			||||||
 | 
						GetFile(filePath string) ([]byte, error)
 | 
				
			||||||
 | 
						GetAllFiles() ([]FileSnapshot, error)
 | 
				
			||||||
 | 
						RemoveAllFiles() error
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type FileSnapshot struct {
 | 
				
			||||||
 | 
						Date     time.Time `gorm:"primaryKey"`
 | 
				
			||||||
 | 
						FilePath string    `gorm:"primaryKey"`
 | 
				
			||||||
 | 
						FileData []byte    `gorm:"type:blob"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type DBWrapper struct {
 | 
				
			||||||
 | 
						db *gorm.DB
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var globalDB *DBWrapper
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetDB() (DB, error) {
 | 
				
			||||||
 | 
						getDBLogger := dbLogger.WithPrefix("GetDB")
 | 
				
			||||||
 | 
						getDBLogger.Debug("Attempting to get database connection")
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dbFile := filepath.Join("data.sqlite")
 | 
				
			||||||
 | 
						getDBLogger.Debug("Opening database file: %q", dbFile)
 | 
				
			||||||
 | 
						db, err := gorm.Open(sqlite.Open(dbFile), &gorm.Config{
 | 
				
			||||||
 | 
							// SkipDefaultTransaction: true,
 | 
				
			||||||
 | 
							PrepareStmt: true,
 | 
				
			||||||
 | 
							Logger:      gormlogger.Default.LogMode(gormlogger.Silent),
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							getDBLogger.Error("Failed to open database: %v", err)
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						getDBLogger.Debug("Database opened successfully, running auto migration")
 | 
				
			||||||
 | 
						if err := db.AutoMigrate(&FileSnapshot{}); err != nil {
 | 
				
			||||||
 | 
							getDBLogger.Error("Auto migration failed: %v", err)
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						getDBLogger.Debug("Auto migration completed")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						globalDB = &DBWrapper{db: db}
 | 
				
			||||||
 | 
						getDBLogger.Debug("Database wrapper initialized")
 | 
				
			||||||
 | 
						return globalDB, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Just a wrapper
 | 
				
			||||||
 | 
					func (db *DBWrapper) Raw(sql string, args ...any) *gorm.DB {
 | 
				
			||||||
 | 
						rawLogger := dbLogger.WithPrefix("Raw").WithField("sql", sql)
 | 
				
			||||||
 | 
						rawLogger.Debug("Executing raw SQL query with args: %v", args)
 | 
				
			||||||
 | 
						return db.db.Raw(sql, args...)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (db *DBWrapper) DB() *gorm.DB {
 | 
				
			||||||
 | 
						dbLogger.WithPrefix("DB").Debug("Returning GORM DB instance")
 | 
				
			||||||
 | 
						return db.db
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (db *DBWrapper) FileExists(filePath string) (bool, error) {
 | 
				
			||||||
 | 
						fileExistsLogger := dbLogger.WithPrefix("FileExists").WithField("filePath", filePath)
 | 
				
			||||||
 | 
						fileExistsLogger.Debug("Checking if file exists in database")
 | 
				
			||||||
 | 
						var count int64
 | 
				
			||||||
 | 
						err := db.db.Model(&FileSnapshot{}).Where("file_path = ?", filePath).Count(&count).Error
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fileExistsLogger.Error("Error checking if file exists: %v", err)
 | 
				
			||||||
 | 
							return false, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fileExistsLogger.Debug("File exists: %t", count > 0)
 | 
				
			||||||
 | 
						return count > 0, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (db *DBWrapper) SaveFile(filePath string, fileData []byte) error {
 | 
				
			||||||
 | 
						saveFileLogger := dbLogger.WithPrefix("SaveFile").WithField("filePath", filePath)
 | 
				
			||||||
 | 
						saveFileLogger.Debug("Attempting to save file to database")
 | 
				
			||||||
 | 
						saveFileLogger.Trace("File data length: %d", len(fileData))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						exists, err := db.FileExists(filePath)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							saveFileLogger.Error("Error checking if file exists: %v", err)
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if exists {
 | 
				
			||||||
 | 
							saveFileLogger.Debug("File already exists, skipping save")
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						saveFileLogger.Debug("Creating new file snapshot in database")
 | 
				
			||||||
 | 
						err = db.db.Create(&FileSnapshot{
 | 
				
			||||||
 | 
							Date:     time.Now(),
 | 
				
			||||||
 | 
							FilePath: filePath,
 | 
				
			||||||
 | 
							FileData: fileData,
 | 
				
			||||||
 | 
						}).Error
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							saveFileLogger.Error("Failed to create file snapshot: %v", err)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							saveFileLogger.Debug("File saved successfully to database")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (db *DBWrapper) GetFile(filePath string) ([]byte, error) {
 | 
				
			||||||
 | 
						getFileLogger := dbLogger.WithPrefix("GetFile").WithField("filePath", filePath)
 | 
				
			||||||
 | 
						getFileLogger.Debug("Getting file from database")
 | 
				
			||||||
 | 
						var fileSnapshot FileSnapshot
 | 
				
			||||||
 | 
						err := db.db.Model(&FileSnapshot{}).Where("file_path = ?", filePath).First(&fileSnapshot).Error
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							// Downgrade not-found to warning to avoid noisy errors during first run
 | 
				
			||||||
 | 
							getFileLogger.Warning("Failed to get file from database: %v", err)
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						getFileLogger.Debug("File found in database")
 | 
				
			||||||
 | 
						getFileLogger.Trace("Retrieved file data length: %d", len(fileSnapshot.FileData))
 | 
				
			||||||
 | 
						return fileSnapshot.FileData, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (db *DBWrapper) GetAllFiles() ([]FileSnapshot, error) {
 | 
				
			||||||
 | 
						getAllFilesLogger := dbLogger.WithPrefix("GetAllFiles")
 | 
				
			||||||
 | 
						getAllFilesLogger.Debug("Getting all files from database")
 | 
				
			||||||
 | 
						var fileSnapshots []FileSnapshot
 | 
				
			||||||
 | 
						err := db.db.Model(&FileSnapshot{}).Find(&fileSnapshots).Error
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							getAllFilesLogger.Error("Failed to get all files from database: %v", err)
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						getAllFilesLogger.Debug("Found %d files in database", len(fileSnapshots))
 | 
				
			||||||
 | 
						getAllFilesLogger.Trace("File snapshots retrieved: %v", fileSnapshots)
 | 
				
			||||||
 | 
						return fileSnapshots, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (db *DBWrapper) RemoveAllFiles() error {
 | 
				
			||||||
 | 
						removeAllFilesLogger := dbLogger.WithPrefix("RemoveAllFiles")
 | 
				
			||||||
 | 
						removeAllFilesLogger.Debug("Removing all files from database")
 | 
				
			||||||
 | 
						err := db.db.Exec("DELETE FROM file_snapshots").Error
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							removeAllFilesLogger.Error("Failed to remove all files from database: %v", err)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							removeAllFilesLogger.Debug("All files removed from database")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										152
									
								
								utils/file.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										152
									
								
								utils/file.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,152 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// fileLogger is a scoped logger for the utils/file package.
 | 
				
			||||||
 | 
					var fileLogger = logger.Default.WithPrefix("utils/file")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CleanPath(path string) string {
 | 
				
			||||||
 | 
						cleanPathLogger := fileLogger.WithPrefix("CleanPath")
 | 
				
			||||||
 | 
						cleanPathLogger.Debug("Cleaning path: %q", path)
 | 
				
			||||||
 | 
						cleanPathLogger.Trace("Original path: %q", path)
 | 
				
			||||||
 | 
						path = filepath.Clean(path)
 | 
				
			||||||
 | 
						path = strings.ReplaceAll(path, "\\", "/")
 | 
				
			||||||
 | 
						cleanPathLogger.Trace("Cleaned path result: %q", path)
 | 
				
			||||||
 | 
						return path
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ToAbs(path string) string {
 | 
				
			||||||
 | 
						toAbsLogger := fileLogger.WithPrefix("ToAbs")
 | 
				
			||||||
 | 
						toAbsLogger.Debug("Converting path to absolute: %q", path)
 | 
				
			||||||
 | 
						toAbsLogger.Trace("Input path: %q", path)
 | 
				
			||||||
 | 
						if filepath.IsAbs(path) {
 | 
				
			||||||
 | 
							toAbsLogger.Debug("Path is already absolute, cleaning it.")
 | 
				
			||||||
 | 
							cleanedPath := CleanPath(path)
 | 
				
			||||||
 | 
							toAbsLogger.Trace("Already absolute path after cleaning: %q", cleanedPath)
 | 
				
			||||||
 | 
							return cleanedPath
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						cwd, err := os.Getwd()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							toAbsLogger.Error("Error getting current working directory: %v", err)
 | 
				
			||||||
 | 
							return CleanPath(path)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						toAbsLogger.Trace("Current working directory: %q", cwd)
 | 
				
			||||||
 | 
						cleanedPath := CleanPath(filepath.Join(cwd, path))
 | 
				
			||||||
 | 
						toAbsLogger.Trace("Converted absolute path result: %q", cleanedPath)
 | 
				
			||||||
 | 
						return cleanedPath
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// LimitString truncates a string to maxLen and adds "..." if truncated
 | 
				
			||||||
 | 
					func LimitString(s string, maxLen int) string {
 | 
				
			||||||
 | 
						limitStringLogger := fileLogger.WithPrefix("LimitString").WithField("originalLength", len(s)).WithField("maxLength", maxLen)
 | 
				
			||||||
 | 
						limitStringLogger.Debug("Limiting string length")
 | 
				
			||||||
 | 
						s = strings.ReplaceAll(s, "\n", "\\n")
 | 
				
			||||||
 | 
						if len(s) <= maxLen {
 | 
				
			||||||
 | 
							limitStringLogger.Trace("String length (%d) is within max length (%d), no truncation", len(s), maxLen)
 | 
				
			||||||
 | 
							return s
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						limited := s[:maxLen-3] + "..."
 | 
				
			||||||
 | 
						limitStringLogger.Trace("String truncated from %d to %d characters: %q", len(s), len(limited), limited)
 | 
				
			||||||
 | 
						return limited
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// StrToFloat converts a string to a float64, returning 0 on error.
 | 
				
			||||||
 | 
					func StrToFloat(s string) float64 {
 | 
				
			||||||
 | 
						strToFloatLogger := fileLogger.WithPrefix("StrToFloat").WithField("inputString", s)
 | 
				
			||||||
 | 
						strToFloatLogger.Debug("Attempting to convert string to float")
 | 
				
			||||||
 | 
						f, err := strconv.ParseFloat(s, 64)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							strToFloatLogger.Warning("Failed to convert string %q to float, returning 0: %v", s, err)
 | 
				
			||||||
 | 
							return 0
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						strToFloatLogger.Trace("Successfully converted %q to float: %f", s, f)
 | 
				
			||||||
 | 
						return f
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ResetWhereNecessary(associations map[string]FileCommandAssociation, db DB) error {
 | 
				
			||||||
 | 
						resetWhereNecessaryLogger := fileLogger.WithPrefix("ResetWhereNecessary")
 | 
				
			||||||
 | 
						resetWhereNecessaryLogger.Debug("Starting reset where necessary operation")
 | 
				
			||||||
 | 
						resetWhereNecessaryLogger.Trace("File-command associations input: %v", associations)
 | 
				
			||||||
 | 
						dirtyFiles := make(map[string]struct{})
 | 
				
			||||||
 | 
						for _, association := range associations {
 | 
				
			||||||
 | 
							resetWhereNecessaryLogger.Debug("Processing association for file: %q", association.File)
 | 
				
			||||||
 | 
							for _, command := range association.Commands {
 | 
				
			||||||
 | 
								resetWhereNecessaryLogger.Debug("Checking command %q for reset requirement", command.Name)
 | 
				
			||||||
 | 
								resetWhereNecessaryLogger.Trace("Command details: %v", command)
 | 
				
			||||||
 | 
								if command.Reset {
 | 
				
			||||||
 | 
									resetWhereNecessaryLogger.Debug("Command %q requires reset for file %q, marking as dirty", command.Name, association.File)
 | 
				
			||||||
 | 
									dirtyFiles[association.File] = struct{}{}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for _, command := range association.IsolateCommands {
 | 
				
			||||||
 | 
								resetWhereNecessaryLogger.Debug("Checking isolate command %q for reset requirement", command.Name)
 | 
				
			||||||
 | 
								resetWhereNecessaryLogger.Trace("Isolate command details: %v", command)
 | 
				
			||||||
 | 
								if command.Reset {
 | 
				
			||||||
 | 
									resetWhereNecessaryLogger.Debug("Isolate command %q requires reset for file %q, marking as dirty", command.Name, association.File)
 | 
				
			||||||
 | 
									dirtyFiles[association.File] = struct{}{}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						resetWhereNecessaryLogger.Debug("Identified %d files that need to be reset", len(dirtyFiles))
 | 
				
			||||||
 | 
						resetWhereNecessaryLogger.Trace("Dirty files identified: %v", dirtyFiles)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for file := range dirtyFiles {
 | 
				
			||||||
 | 
							resetWhereNecessaryLogger.Debug("Resetting file %q", file)
 | 
				
			||||||
 | 
							fileData, err := db.GetFile(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								resetWhereNecessaryLogger.Warning("Failed to get original content for file %q from database: %v", file, err)
 | 
				
			||||||
 | 
								// Seed the snapshot from current disk content if missing, then use it as fallback
 | 
				
			||||||
 | 
								currentData, readErr := os.ReadFile(file)
 | 
				
			||||||
 | 
								if readErr != nil {
 | 
				
			||||||
 | 
									resetWhereNecessaryLogger.Warning("Additionally failed to read current file content for %q: %v", file, readErr)
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								// Best-effort attempt to save baseline; ignore errors to avoid blocking reset
 | 
				
			||||||
 | 
								if saveErr := db.SaveFile(file, currentData); saveErr != nil {
 | 
				
			||||||
 | 
									resetWhereNecessaryLogger.Warning("Failed to seed baseline snapshot for %q: %v", file, saveErr)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								fileData = currentData
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							resetWhereNecessaryLogger.Trace("Retrieved original file data length for %q: %d", file, len(fileData))
 | 
				
			||||||
 | 
							resetWhereNecessaryLogger.Debug("Writing original content back to file %q", file)
 | 
				
			||||||
 | 
							err = os.WriteFile(file, fileData, 0644)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								resetWhereNecessaryLogger.Warning("Failed to write original content back to file %q: %v", file, err)
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							resetWhereNecessaryLogger.Debug("Successfully reset file %q", file)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						resetWhereNecessaryLogger.Debug("Finished reset where necessary operation")
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ResetAllFiles(db DB) error {
 | 
				
			||||||
 | 
						resetAllFilesLogger := fileLogger.WithPrefix("ResetAllFiles")
 | 
				
			||||||
 | 
						resetAllFilesLogger.Debug("Starting reset all files operation")
 | 
				
			||||||
 | 
						fileSnapshots, err := db.GetAllFiles()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							resetAllFilesLogger.Error("Failed to get all file snapshots from database: %v", err)
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						resetAllFilesLogger.Debug("Found %d files in database to reset", len(fileSnapshots))
 | 
				
			||||||
 | 
						resetAllFilesLogger.Trace("File snapshots retrieved: %v", fileSnapshots)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, fileSnapshot := range fileSnapshots {
 | 
				
			||||||
 | 
							resetAllFilesLogger.Debug("Resetting file %q", fileSnapshot.FilePath)
 | 
				
			||||||
 | 
							err = os.WriteFile(fileSnapshot.FilePath, fileSnapshot.FileData, 0644)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								resetAllFilesLogger.Warning("Failed to write file %q to disk: %v", fileSnapshot.FilePath, err)
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							resetAllFilesLogger.Debug("File %q written to disk successfully", fileSnapshot.FilePath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						resetAllFilesLogger.Debug("Finished reset all files operation")
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -2,15 +2,20 @@ package utils
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"flag"
 | 
						"flag"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// flagsLogger is a scoped logger for the utils/flags package.
 | 
				
			||||||
 | 
					var flagsLogger = logger.Default.WithPrefix("utils/flags")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	// Deprecated
 | 
					 | 
				
			||||||
	GitFlag       = flag.Bool("git", false, "Use git to manage files")
 | 
					 | 
				
			||||||
	// Deprecated
 | 
					 | 
				
			||||||
	ResetFlag     = flag.Bool("reset", false, "Reset files to their original state")
 | 
					 | 
				
			||||||
	LogLevel      = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
 | 
					 | 
				
			||||||
	Cookfile      = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed")
 | 
					 | 
				
			||||||
	ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
 | 
						ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
 | 
				
			||||||
	Filter        = flag.String("filter", "", "Filter commands before running them")
 | 
						Filter        = flag.String("f", "", "Filter commands before running them")
 | 
				
			||||||
 | 
						JSON          = flag.Bool("json", false, "Enable JSON mode for processing JSON files")
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						flagsLogger.Debug("Initializing flags")
 | 
				
			||||||
 | 
						flagsLogger.Trace("ParallelFiles initial value: %d, Filter initial value: %q, JSON initial value: %t", *ParallelFiles, *Filter, *JSON)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										97
									
								
								utils/git.go
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								utils/git.go
									
									
									
									
									
								
							@@ -1,97 +0,0 @@
 | 
				
			|||||||
package utils
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"modify/logger"
 | 
					 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"path/filepath"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/go-git/go-git/v5/plumbing/object"
 | 
					 | 
				
			||||||
	"github.com/go-git/go-git/v5"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var (
 | 
					 | 
				
			||||||
	Repo     *git.Repository
 | 
					 | 
				
			||||||
	Worktree *git.Worktree
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func SetupGit() error {
 | 
					 | 
				
			||||||
	cwd, err := os.Getwd()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("failed to get current working directory: %w", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	logger.Debug("Current working directory obtained: %s", cwd)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logger.Debug("Attempting to open git repository at %s", cwd)
 | 
					 | 
				
			||||||
	Repo, err = git.PlainOpen(cwd)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Debug("No existing git repository found at %s, attempting to initialize a new git repository.", cwd)
 | 
					 | 
				
			||||||
		Repo, err = git.PlainInit(cwd, false)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return fmt.Errorf("failed to initialize a new git repository at %s: %w", cwd, err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		logger.Info("Successfully initialized a new git repository at %s", cwd)
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		logger.Info("Successfully opened existing git repository at %s", cwd)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	logger.Debug("Attempting to obtain worktree for repository at %s", cwd)
 | 
					 | 
				
			||||||
	Worktree, err = Repo.Worktree()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("failed to obtain worktree for repository at %s: %w", cwd, err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	logger.Debug("Successfully obtained worktree for repository at %s", cwd)
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func CleanupGitFiles(files []string) error {
 | 
					 | 
				
			||||||
	for _, file := range files {
 | 
					 | 
				
			||||||
		logger.Debug("Checking git status for file: %s", file)
 | 
					 | 
				
			||||||
		status, err := Worktree.Status()
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			logger.Error("Error getting worktree status: %v", err)
 | 
					 | 
				
			||||||
			fmt.Fprintf(os.Stderr, "Error getting worktree status: %v\n", err)
 | 
					 | 
				
			||||||
			return fmt.Errorf("error getting worktree status: %w", err)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if status.IsUntracked(file) {
 | 
					 | 
				
			||||||
			logger.Info("Detected untracked file: %s. Adding to git index.", file)
 | 
					 | 
				
			||||||
			_, err = Worktree.Add(file)
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				logger.Error("Error adding file to git: %v", err)
 | 
					 | 
				
			||||||
				fmt.Fprintf(os.Stderr, "Error adding file to git: %v\n", err)
 | 
					 | 
				
			||||||
				return fmt.Errorf("error adding file to git: %w", err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			filename := filepath.Base(file)
 | 
					 | 
				
			||||||
			logger.Info("File %s added successfully. Committing with message: 'Track %s'", filename, filename)
 | 
					 | 
				
			||||||
			_, err = Worktree.Commit("Track "+filename, &git.CommitOptions{
 | 
					 | 
				
			||||||
				Author: &object.Signature{
 | 
					 | 
				
			||||||
					Name:  "Big Chef",
 | 
					 | 
				
			||||||
					Email: "bigchef@bigchef.com",
 | 
					 | 
				
			||||||
					When:  time.Now(),
 | 
					 | 
				
			||||||
				},
 | 
					 | 
				
			||||||
			})
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				logger.Error("Error committing file: %v", err)
 | 
					 | 
				
			||||||
				fmt.Fprintf(os.Stderr, "Error committing file: %v\n", err)
 | 
					 | 
				
			||||||
				return fmt.Errorf("error committing file: %w", err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			logger.Info("Successfully committed file: %s", filename)
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			logger.Info("File %s is already tracked. Restoring it to the working tree.", file)
 | 
					 | 
				
			||||||
			err := Worktree.Restore(&git.RestoreOptions{
 | 
					 | 
				
			||||||
				Files:    []string{file},
 | 
					 | 
				
			||||||
				Staged:   true,
 | 
					 | 
				
			||||||
				Worktree: true,
 | 
					 | 
				
			||||||
			})
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				logger.Error("Error restoring file: %v", err)
 | 
					 | 
				
			||||||
				fmt.Fprintf(os.Stderr, "Error restoring file: %v\n", err)
 | 
					 | 
				
			||||||
				return fmt.Errorf("error restoring file: %w", err)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			logger.Info("File %s restored successfully", file)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -2,236 +2,374 @@ package utils
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"modify/logger"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
	"github.com/bmatcuk/doublestar/v4"
 | 
						"github.com/bmatcuk/doublestar/v4"
 | 
				
			||||||
	"gopkg.in/yaml.v3"
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// modifyCommandLogger is a scoped logger for the utils/modifycommand package.
 | 
				
			||||||
 | 
					var modifyCommandLogger = logger.Default.WithPrefix("utils/modifycommand")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ModifyCommand struct {
 | 
					type ModifyCommand struct {
 | 
				
			||||||
	Name     string   `yaml:"name"`
 | 
						Name      string                 `yaml:"name,omitempty"`
 | 
				
			||||||
	Regex    string   `yaml:"regex"`
 | 
						Regex     string                 `yaml:"regex,omitempty"`
 | 
				
			||||||
	Lua      string   `yaml:"lua"`
 | 
						Regexes   []string               `yaml:"regexes,omitempty"`
 | 
				
			||||||
	Files    []string `yaml:"files"`
 | 
						Lua       string                 `yaml:"lua,omitempty"`
 | 
				
			||||||
	Git      bool     `yaml:"git"`
 | 
						Files     []string               `yaml:"files,omitempty"`
 | 
				
			||||||
	Reset    bool     `yaml:"reset"`
 | 
						Reset     bool                   `yaml:"reset,omitempty"`
 | 
				
			||||||
	LogLevel string   `yaml:"loglevel"`
 | 
						LogLevel  string                 `yaml:"loglevel,omitempty"`
 | 
				
			||||||
 | 
						Isolate   bool                   `yaml:"isolate,omitempty"`
 | 
				
			||||||
 | 
						NoDedup   bool                   `yaml:"nodedup,omitempty"`
 | 
				
			||||||
 | 
						Disabled  bool                   `yaml:"disable,omitempty"`
 | 
				
			||||||
 | 
						JSON      bool                   `yaml:"json,omitempty"`
 | 
				
			||||||
 | 
						Modifiers map[string]interface{} `yaml:"modifiers,omitempty"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type CookFile []ModifyCommand
 | 
					type CookFile []ModifyCommand
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *ModifyCommand) Validate() error {
 | 
					func (c *ModifyCommand) Validate() error {
 | 
				
			||||||
	if c.Regex == "" {
 | 
						validateLogger := modifyCommandLogger.WithPrefix("Validate").WithField("commandName", c.Name)
 | 
				
			||||||
		return fmt.Errorf("pattern is required")
 | 
						validateLogger.Debug("Validating command")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// For JSON mode, regex patterns are not required
 | 
				
			||||||
 | 
						if !c.JSON {
 | 
				
			||||||
 | 
							if c.Regex == "" && len(c.Regexes) == 0 {
 | 
				
			||||||
 | 
								validateLogger.Error("Validation failed: Regex pattern is required for non-JSON mode")
 | 
				
			||||||
 | 
								return fmt.Errorf("pattern is required for non-JSON mode")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if c.Lua == "" {
 | 
						if c.Lua == "" {
 | 
				
			||||||
 | 
							validateLogger.Error("Validation failed: Lua expression is required")
 | 
				
			||||||
		return fmt.Errorf("lua expression is required")
 | 
							return fmt.Errorf("lua expression is required")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if len(c.Files) == 0 {
 | 
						if len(c.Files) == 0 {
 | 
				
			||||||
 | 
							validateLogger.Error("Validation failed: At least one file is required")
 | 
				
			||||||
		return fmt.Errorf("at least one file is required")
 | 
							return fmt.Errorf("at least one file is required")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if c.LogLevel == "" {
 | 
						if c.LogLevel == "" {
 | 
				
			||||||
 | 
							validateLogger.Debug("LogLevel not specified, defaulting to INFO")
 | 
				
			||||||
		c.LogLevel = "INFO"
 | 
							c.LogLevel = "INFO"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						validateLogger.Debug("Command validated successfully")
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Ehh.. Not much better... Guess this wasn't the big deal
 | 
					// Ehh.. Not much better... Guess this wasn't the big deal
 | 
				
			||||||
var matchesMemoTable map[string]bool = make(map[string]bool)
 | 
					var matchesMemoTable map[string]bool = make(map[string]bool)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Matches(path string, glob string) (bool, error) {
 | 
					func Matches(path string, glob string) (bool, error) {
 | 
				
			||||||
 | 
						matchesLogger := modifyCommandLogger.WithPrefix("Matches").WithField("path", path).WithField("glob", glob)
 | 
				
			||||||
 | 
						matchesLogger.Debug("Checking if path matches glob")
 | 
				
			||||||
	key := fmt.Sprintf("%s:%s", path, glob)
 | 
						key := fmt.Sprintf("%s:%s", path, glob)
 | 
				
			||||||
	if matches, ok := matchesMemoTable[key]; ok {
 | 
						if matches, ok := matchesMemoTable[key]; ok {
 | 
				
			||||||
		logger.Debug("Found match for file %q and glob %q in memo table", path, glob)
 | 
							matchesLogger.Debug("Found match in memo table: %t", matches)
 | 
				
			||||||
		return matches, nil
 | 
							return matches, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	matches, err := doublestar.Match(glob, path)
 | 
						matches, err := doublestar.Match(glob, path)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							matchesLogger.Error("Failed to match glob: %v", err)
 | 
				
			||||||
		return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err)
 | 
							return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	matchesMemoTable[key] = matches
 | 
						matchesMemoTable[key] = matches
 | 
				
			||||||
 | 
						matchesLogger.Debug("Match result: %t, storing in memo table", matches)
 | 
				
			||||||
	return matches, nil
 | 
						return matches, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
 | 
					func SplitPattern(pattern string) (string, string) {
 | 
				
			||||||
	associationCount := 0
 | 
						splitPatternLogger := modifyCommandLogger.WithPrefix("SplitPattern").WithField("pattern", pattern)
 | 
				
			||||||
	fileCommands := make(map[string][]ModifyCommand)
 | 
						splitPatternLogger.Debug("Splitting pattern")
 | 
				
			||||||
	for _, file := range files {
 | 
						splitPatternLogger.Trace("Original pattern: %q", pattern)
 | 
				
			||||||
		for _, command := range commands {
 | 
						static, pattern := doublestar.SplitPattern(pattern)
 | 
				
			||||||
			for _, glob := range command.Files {
 | 
					
 | 
				
			||||||
				matches, err := Matches(file, glob)
 | 
						cwd, err := os.Getwd()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
					logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
 | 
							splitPatternLogger.Error("Error getting current working directory: %v", err)
 | 
				
			||||||
 | 
							return "", ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						splitPatternLogger.Trace("Current working directory: %q", cwd)
 | 
				
			||||||
 | 
						if static == "" {
 | 
				
			||||||
 | 
							splitPatternLogger.Debug("Static part is empty, defaulting to current working directory")
 | 
				
			||||||
 | 
							static = cwd
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if !filepath.IsAbs(static) {
 | 
				
			||||||
 | 
							splitPatternLogger.Debug("Static part is not absolute, joining with current working directory")
 | 
				
			||||||
 | 
							static = filepath.Join(cwd, static)
 | 
				
			||||||
 | 
							static = filepath.Clean(static)
 | 
				
			||||||
 | 
							splitPatternLogger.Trace("Static path after joining and cleaning: %q", static)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						static = strings.ReplaceAll(static, "\\", "/")
 | 
				
			||||||
 | 
						splitPatternLogger.Trace("Final static path: %q, Remaining pattern: %q", static, pattern)
 | 
				
			||||||
 | 
						return static, pattern
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type FileCommandAssociation struct {
 | 
				
			||||||
 | 
						File            string
 | 
				
			||||||
 | 
						IsolateCommands []ModifyCommand
 | 
				
			||||||
 | 
						Commands        []ModifyCommand
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string]FileCommandAssociation, error) {
 | 
				
			||||||
 | 
						associateFilesLogger := modifyCommandLogger.WithPrefix("AssociateFilesWithCommands")
 | 
				
			||||||
 | 
						associateFilesLogger.Debug("Associating files with commands")
 | 
				
			||||||
 | 
						associateFilesLogger.Trace("Input files: %v", files)
 | 
				
			||||||
 | 
						associateFilesLogger.Trace("Input commands: %v", commands)
 | 
				
			||||||
 | 
						associationCount := 0
 | 
				
			||||||
 | 
						fileCommands := make(map[string]FileCommandAssociation)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, file := range files {
 | 
				
			||||||
 | 
							file = strings.ReplaceAll(file, "\\", "/")
 | 
				
			||||||
 | 
							associateFilesLogger.Debug("Processing file: %q", file)
 | 
				
			||||||
 | 
							fileCommands[file] = FileCommandAssociation{
 | 
				
			||||||
 | 
								File:            file,
 | 
				
			||||||
 | 
								IsolateCommands: []ModifyCommand{},
 | 
				
			||||||
 | 
								Commands:        []ModifyCommand{},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for _, command := range commands {
 | 
				
			||||||
 | 
								associateFilesLogger.Debug("Checking command %q for file %q", command.Name, file)
 | 
				
			||||||
 | 
								for _, glob := range command.Files {
 | 
				
			||||||
 | 
									glob = strings.ReplaceAll(glob, "\\", "/")
 | 
				
			||||||
 | 
									static, pattern := SplitPattern(glob)
 | 
				
			||||||
 | 
									associateFilesLogger.Trace("Glob parts for %q → static=%q pattern=%q", glob, static, pattern)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// Build absolute path for the current file to compare with static
 | 
				
			||||||
 | 
									cwd, err := os.Getwd()
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										associateFilesLogger.Warning("Failed to get CWD when matching %q for file %q: %v", glob, file, err)
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									var absFile string
 | 
				
			||||||
 | 
									if filepath.IsAbs(file) {
 | 
				
			||||||
 | 
										absFile = filepath.Clean(file)
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										absFile = filepath.Clean(filepath.Join(cwd, file))
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									absFile = strings.ReplaceAll(absFile, "\\", "/")
 | 
				
			||||||
 | 
									associateFilesLogger.Trace("Absolute file path resolved for matching: %q", absFile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// Only match if the file is under the static root
 | 
				
			||||||
 | 
									if !(strings.HasPrefix(absFile, static+"/") || absFile == static) {
 | 
				
			||||||
 | 
										associateFilesLogger.Trace("Skipping glob %q for file %q because file is outside static root %q", glob, file, static)
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									patternFile := strings.TrimPrefix(absFile, static+`/`)
 | 
				
			||||||
 | 
									associateFilesLogger.Trace("Pattern-relative path used for match: %q", patternFile)
 | 
				
			||||||
 | 
									matches, err := Matches(patternFile, pattern)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										associateFilesLogger.Warning("Failed to match glob %q with file %q: %v", glob, file, err)
 | 
				
			||||||
					continue
 | 
										continue
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				if matches {
 | 
									if matches {
 | 
				
			||||||
					logger.Debug("Found match for file %q and command %q", file, command.Regex)
 | 
										associateFilesLogger.Debug("File %q matches glob %q. Associating with command %q", file, glob, command.Name)
 | 
				
			||||||
					fileCommands[file] = append(fileCommands[file], command)
 | 
										association := fileCommands[file]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
										if command.Isolate {
 | 
				
			||||||
 | 
											associateFilesLogger.Debug("Command %q is an isolate command, adding to isolate list", command.Name)
 | 
				
			||||||
 | 
											association.IsolateCommands = append(association.IsolateCommands, command)
 | 
				
			||||||
 | 
										} else {
 | 
				
			||||||
 | 
											associateFilesLogger.Debug("Command %q is a regular command, adding to regular list", command.Name)
 | 
				
			||||||
 | 
											association.Commands = append(association.Commands, command)
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										fileCommands[file] = association
 | 
				
			||||||
					associationCount++
 | 
										associationCount++
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										associateFilesLogger.Trace("File %q did not match glob %q (pattern=%q, rel=%q)", file, glob, pattern, patternFile)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Debug("Found %d commands for file %q", len(fileCommands[file]), file)
 | 
							currentFileCommands := fileCommands[file]
 | 
				
			||||||
		if len(fileCommands[file]) == 0 {
 | 
							associateFilesLogger.Debug("Finished processing file %q. Found %d regular commands and %d isolate commands", file, len(currentFileCommands.Commands), len(currentFileCommands.IsolateCommands))
 | 
				
			||||||
			logger.Info("No commands found for file %q", file)
 | 
							associateFilesLogger.Trace("Commands for file %q: %v", file, currentFileCommands.Commands)
 | 
				
			||||||
 | 
							associateFilesLogger.Trace("Isolate commands for file %q: %v", file, currentFileCommands.IsolateCommands)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	}
 | 
						associateFilesLogger.Info("Completed association. Found %d total associations for %d files and %d commands", associationCount, len(files), len(commands))
 | 
				
			||||||
	logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands))
 | 
					 | 
				
			||||||
	return fileCommands, nil
 | 
						return fileCommands, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
 | 
					func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
 | 
				
			||||||
	logger.Info("Aggregating globs for %d commands", len(commands))
 | 
						aggregateGlobsLogger := modifyCommandLogger.WithPrefix("AggregateGlobs")
 | 
				
			||||||
 | 
						aggregateGlobsLogger.Debug("Aggregating glob patterns from commands")
 | 
				
			||||||
 | 
						aggregateGlobsLogger.Trace("Input commands for aggregation: %v", commands)
 | 
				
			||||||
	globs := make(map[string]struct{})
 | 
						globs := make(map[string]struct{})
 | 
				
			||||||
	for _, command := range commands {
 | 
						for _, command := range commands {
 | 
				
			||||||
 | 
							aggregateGlobsLogger.Debug("Processing command %q for glob patterns", command.Name)
 | 
				
			||||||
		for _, glob := range command.Files {
 | 
							for _, glob := range command.Files {
 | 
				
			||||||
			globs[glob] = struct{}{}
 | 
								resolvedGlob := strings.Replace(glob, "~", os.Getenv("HOME"), 1)
 | 
				
			||||||
 | 
								resolvedGlob = strings.ReplaceAll(resolvedGlob, "\\", "/")
 | 
				
			||||||
 | 
								aggregateGlobsLogger.Trace("Adding glob: %q (resolved to %q)", glob, resolvedGlob)
 | 
				
			||||||
 | 
								globs[resolvedGlob] = struct{}{}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	logger.Info("Found %d unique globs", len(globs))
 | 
						aggregateGlobsLogger.Debug("Finished aggregating globs. Found %d unique glob patterns", len(globs))
 | 
				
			||||||
 | 
						aggregateGlobsLogger.Trace("Aggregated unique globs: %v", globs)
 | 
				
			||||||
	return globs
 | 
						return globs
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
 | 
					func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
 | 
				
			||||||
 | 
						expandGlobsLogger := modifyCommandLogger.WithPrefix("ExpandGLobs")
 | 
				
			||||||
 | 
						expandGlobsLogger.Debug("Expanding glob patterns to actual files")
 | 
				
			||||||
 | 
						expandGlobsLogger.Trace("Input patterns for expansion: %v", patterns)
 | 
				
			||||||
	var files []string
 | 
						var files []string
 | 
				
			||||||
	filesMap := make(map[string]bool)
 | 
						filesMap := make(map[string]bool)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cwd, err := os.Getwd()
 | 
						cwd, err := os.Getwd()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							expandGlobsLogger.Error("Failed to get current working directory: %v", err)
 | 
				
			||||||
		return nil, fmt.Errorf("failed to get current working directory: %w", err)
 | 
							return nil, fmt.Errorf("failed to get current working directory: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						expandGlobsLogger.Debug("Current working directory: %q", cwd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Debug("Expanding patterns from directory: %s", cwd)
 | 
					 | 
				
			||||||
	for pattern := range patterns {
 | 
						for pattern := range patterns {
 | 
				
			||||||
		logger.Trace("Processing pattern: %s", pattern)
 | 
							expandGlobsLogger.Debug("Processing glob pattern: %q", pattern)
 | 
				
			||||||
		matches, _ := doublestar.Glob(os.DirFS(cwd), pattern)
 | 
							static, pattern := SplitPattern(pattern)
 | 
				
			||||||
		logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
 | 
							matches, err := doublestar.Glob(os.DirFS(static), pattern)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								expandGlobsLogger.Warning("Error expanding glob %q in %q: %v", pattern, static, err)
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							expandGlobsLogger.Debug("Found %d matches for pattern %q", len(matches), pattern)
 | 
				
			||||||
 | 
							expandGlobsLogger.Trace("Raw matches for pattern %q: %v", pattern, matches)
 | 
				
			||||||
		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)
 | 
									expandGlobsLogger.Warning("Error getting file info for %q: %v", m, err)
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if !info.IsDir() && !filesMap[m] {
 | 
								if !info.IsDir() && !filesMap[m] {
 | 
				
			||||||
				logger.Trace("Adding file to process list: %s", m)
 | 
									expandGlobsLogger.Trace("Adding unique file to list: %q", m)
 | 
				
			||||||
				filesMap[m], files = true, append(files, m)
 | 
									filesMap[m], files = true, append(files, m)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(files) > 0 {
 | 
						if len(files) > 0 {
 | 
				
			||||||
		logger.Debug("Found %d files to process: %v", len(files), files)
 | 
							expandGlobsLogger.Debug("Finished expanding globs. Found %d unique files to process", len(files))
 | 
				
			||||||
 | 
							expandGlobsLogger.Trace("Unique files to process: %v", files)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							expandGlobsLogger.Warning("No files found after expanding glob patterns.")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return files, nil
 | 
						return files, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func LoadCommands(args []string) ([]ModifyCommand, error) {
 | 
					func LoadCommands(args []string) ([]ModifyCommand, error) {
 | 
				
			||||||
 | 
						loadCommandsLogger := modifyCommandLogger.WithPrefix("LoadCommands")
 | 
				
			||||||
 | 
						loadCommandsLogger.Debug("Loading commands from arguments (cook files or direct patterns)")
 | 
				
			||||||
 | 
						loadCommandsLogger.Trace("Input arguments: %v", args)
 | 
				
			||||||
	commands := []ModifyCommand{}
 | 
						commands := []ModifyCommand{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Info("Loading commands from cook files: %s", *Cookfile)
 | 
						for _, arg := range args {
 | 
				
			||||||
	newcommands, err := LoadCommandsFromCookFiles(*Cookfile)
 | 
							loadCommandsLogger.Debug("Processing argument for commands: %q", arg)
 | 
				
			||||||
 | 
							newCommands, err := LoadCommandsFromCookFiles(arg)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 | 
								loadCommandsLogger.Error("Failed to load commands from argument %q: %v", arg, err)
 | 
				
			||||||
			return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
 | 
								return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	logger.Info("Successfully loaded %d commands from cook files", len(newcommands))
 | 
							loadCommandsLogger.Debug("Successfully loaded %d commands from %q", len(newCommands), arg)
 | 
				
			||||||
	commands = append(commands, newcommands...)
 | 
							for _, cmd := range newCommands {
 | 
				
			||||||
	logger.Info("Now total commands: %d", len(commands))
 | 
								if cmd.Disabled {
 | 
				
			||||||
 | 
									loadCommandsLogger.Debug("Skipping disabled command: %q", cmd.Name)
 | 
				
			||||||
	logger.Info("Loading commands from arguments: %v", args)
 | 
									continue
 | 
				
			||||||
	newcommands, err = LoadCommandFromArgs(args)
 | 
								}
 | 
				
			||||||
	if err != nil {
 | 
								commands = append(commands, cmd)
 | 
				
			||||||
		if len(commands) == 0 {
 | 
								loadCommandsLogger.Trace("Added command %q. Current total commands: %d", cmd.Name, len(commands))
 | 
				
			||||||
			return nil, fmt.Errorf("failed to load commands from args: %w", err)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Warning("Failed to load commands from args: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	logger.Info("Successfully loaded %d commands from args", len(newcommands))
 | 
					 | 
				
			||||||
	commands = append(commands, newcommands...)
 | 
					 | 
				
			||||||
	logger.Info("Now total commands: %d", len(commands))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						loadCommandsLogger.Info("Finished loading commands. Total %d commands loaded", len(commands))
 | 
				
			||||||
	return commands, nil
 | 
						return commands, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
 | 
					func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
 | 
				
			||||||
	// Cannot reset without git, right?
 | 
						loadCookFilesLogger := modifyCommandLogger.WithPrefix("LoadCommandsFromCookFiles").WithField("pattern", pattern)
 | 
				
			||||||
	if *ResetFlag {
 | 
						loadCookFilesLogger.Debug("Loading commands from cook files based on pattern")
 | 
				
			||||||
		*GitFlag = true
 | 
						loadCookFilesLogger.Trace("Input pattern: %q", pattern)
 | 
				
			||||||
	}
 | 
						static, pattern := SplitPattern(pattern)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(args) < 3 {
 | 
					 | 
				
			||||||
		return nil, fmt.Errorf("at least %d arguments are required", 3)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	command := ModifyCommand{
 | 
					 | 
				
			||||||
		Regex:    args[0],
 | 
					 | 
				
			||||||
		Lua:      args[1],
 | 
					 | 
				
			||||||
		Files:    args[2:],
 | 
					 | 
				
			||||||
		Git:      *GitFlag,
 | 
					 | 
				
			||||||
		Reset:    *ResetFlag,
 | 
					 | 
				
			||||||
		LogLevel: *LogLevel,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err := command.Validate(); err != nil {
 | 
					 | 
				
			||||||
		return nil, fmt.Errorf("invalid command: %w", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return []ModifyCommand{command}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
 | 
					 | 
				
			||||||
	cwd, err := os.Getwd()
 | 
					 | 
				
			||||||
	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), *Cookfile)
 | 
						cookFiles, err := doublestar.Glob(os.DirFS(static), pattern)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							loadCookFilesLogger.Error("Failed to glob cook files for pattern %q: %v", pattern, err)
 | 
				
			||||||
		return nil, fmt.Errorf("failed to glob cook files: %w", err)
 | 
							return nil, fmt.Errorf("failed to glob cook files: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						loadCookFilesLogger.Debug("Found %d cook files for pattern %q", len(cookFiles), pattern)
 | 
				
			||||||
 | 
						loadCookFilesLogger.Trace("Cook files found: %v", cookFiles)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, cookFile := range cookFiles {
 | 
						for _, cookFile := range cookFiles {
 | 
				
			||||||
 | 
							cookFile = filepath.Join(static, cookFile)
 | 
				
			||||||
 | 
							cookFile = filepath.Clean(cookFile)
 | 
				
			||||||
 | 
							cookFile = strings.ReplaceAll(cookFile, "\\", "/")
 | 
				
			||||||
 | 
							loadCookFilesLogger.Debug("Loading commands from individual cook file: %q", cookFile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		cookFileData, err := os.ReadFile(cookFile)
 | 
							cookFileData, err := os.ReadFile(cookFile)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 | 
								loadCookFilesLogger.Error("Failed to read cook file %q: %v", cookFile, err)
 | 
				
			||||||
			return nil, fmt.Errorf("failed to read cook file: %w", err)
 | 
								return nil, fmt.Errorf("failed to read cook file: %w", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		newcommands, err := LoadCommandsFromCookFile(cookFileData)
 | 
							loadCookFilesLogger.Trace("Read %d bytes from cook file %q", len(cookFileData), cookFile)
 | 
				
			||||||
 | 
							newCommands, err := LoadCommandsFromCookFile(cookFileData)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 | 
								loadCookFilesLogger.Error("Failed to load commands from cook file data for %q: %v", cookFile, err)
 | 
				
			||||||
			return nil, fmt.Errorf("failed to load commands from cook file: %w", err)
 | 
								return nil, fmt.Errorf("failed to load commands from cook file: %w", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		commands = append(commands, newcommands...)
 | 
							commands = append(commands, newCommands...)
 | 
				
			||||||
 | 
							loadCookFilesLogger.Debug("Added %d commands from cook file %q. Total commands now: %d", len(newCommands), cookFile, len(commands))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						loadCookFilesLogger.Debug("Finished loading commands from cook files. Total %d commands", len(commands))
 | 
				
			||||||
	return commands, nil
 | 
						return commands, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func LoadCommandsFromCookFile(cookFileData []byte) ([]ModifyCommand, error) {
 | 
					func LoadCommandsFromCookFile(cookFileData []byte) ([]ModifyCommand, error) {
 | 
				
			||||||
 | 
						loadCommandLogger := modifyCommandLogger.WithPrefix("LoadCommandsFromCookFile")
 | 
				
			||||||
 | 
						loadCommandLogger.Debug("Unmarshaling commands from cook file data")
 | 
				
			||||||
 | 
						loadCommandLogger.Trace("Cook file data length: %d", len(cookFileData))
 | 
				
			||||||
	commands := []ModifyCommand{}
 | 
						commands := []ModifyCommand{}
 | 
				
			||||||
	err := yaml.Unmarshal(cookFileData, &commands)
 | 
						err := yaml.Unmarshal(cookFileData, &commands)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							loadCommandLogger.Error("Failed to unmarshal cook file data: %v", err)
 | 
				
			||||||
		return nil, fmt.Errorf("failed to unmarshal cook file: %w", err)
 | 
							return nil, fmt.Errorf("failed to unmarshal cook file: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						loadCommandLogger.Debug("Successfully unmarshaled %d commands", len(commands))
 | 
				
			||||||
 | 
						loadCommandLogger.Trace("Unmarshaled commands: %v", commands)
 | 
				
			||||||
	return commands, nil
 | 
						return commands, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication
 | 
					// CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication
 | 
				
			||||||
func CountGlobsBeforeDedup(commands []ModifyCommand) int {
 | 
					func CountGlobsBeforeDedup(commands []ModifyCommand) int {
 | 
				
			||||||
 | 
						countGlobsLogger := modifyCommandLogger.WithPrefix("CountGlobsBeforeDedup")
 | 
				
			||||||
 | 
						countGlobsLogger.Debug("Counting glob patterns before deduplication")
 | 
				
			||||||
	count := 0
 | 
						count := 0
 | 
				
			||||||
	for _, cmd := range commands {
 | 
						for _, cmd := range commands {
 | 
				
			||||||
 | 
							countGlobsLogger.Trace("Processing command %q, adding %d globs", cmd.Name, len(cmd.Files))
 | 
				
			||||||
		count += len(cmd.Files)
 | 
							count += len(cmd.Files)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						countGlobsLogger.Debug("Total glob patterns before deduplication: %d", count)
 | 
				
			||||||
	return count
 | 
						return count
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand {
 | 
					func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand {
 | 
				
			||||||
 | 
						filterCommandsLogger := modifyCommandLogger.WithPrefix("FilterCommands").WithField("filter", filter)
 | 
				
			||||||
 | 
						filterCommandsLogger.Debug("Filtering commands")
 | 
				
			||||||
 | 
						filterCommandsLogger.Trace("Input commands: %v", commands)
 | 
				
			||||||
	filteredCommands := []ModifyCommand{}
 | 
						filteredCommands := []ModifyCommand{}
 | 
				
			||||||
	filters := strings.Split(filter, ",")
 | 
						filters := strings.Split(filter, ",")
 | 
				
			||||||
 | 
						filterCommandsLogger.Trace("Split filters: %v", filters)
 | 
				
			||||||
	for _, cmd := range commands {
 | 
						for _, cmd := range commands {
 | 
				
			||||||
		for _, filter := range filters {
 | 
							filterCommandsLogger.Debug("Checking command %q against filters", cmd.Name)
 | 
				
			||||||
			if strings.Contains(cmd.Name, filter) {
 | 
							for _, f := range filters {
 | 
				
			||||||
 | 
								if strings.Contains(cmd.Name, f) {
 | 
				
			||||||
 | 
									filterCommandsLogger.Debug("Command %q matches filter %q, adding to filtered list", cmd.Name, f)
 | 
				
			||||||
				filteredCommands = append(filteredCommands, cmd)
 | 
									filteredCommands = append(filteredCommands, cmd)
 | 
				
			||||||
 | 
									break // Command matches, no need to check other filters
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						filterCommandsLogger.Debug("Finished filtering commands. Found %d filtered commands", len(filteredCommands))
 | 
				
			||||||
 | 
						filterCommandsLogger.Trace("Filtered commands: %v", filteredCommands)
 | 
				
			||||||
	return filteredCommands
 | 
						return filteredCommands
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -158,21 +158,24 @@ func TestAssociateFilesWithCommands(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// The associations expected depends on the implementation
 | 
						// The associations expected depends on the implementation
 | 
				
			||||||
	// Let's check the actual associations and verify they make sense
 | 
						// Let's check the actual associations and verify they make sense
 | 
				
			||||||
	for file, cmds := range associations {
 | 
						for file, assoc := range associations {
 | 
				
			||||||
		t.Logf("File %s is associated with %d commands", file, len(cmds))
 | 
							t.Logf("File %s is associated with %d commands and %d isolate commands", file, len(assoc.Commands), len(assoc.IsolateCommands))
 | 
				
			||||||
		for i, cmd := range cmds {
 | 
							for i, cmd := range assoc.Commands {
 | 
				
			||||||
			t.Logf("  Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files)
 | 
								t.Logf("  Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							for i, cmd := range assoc.IsolateCommands {
 | 
				
			||||||
 | 
								t.Logf("  Isolate Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Specific validation based on our file types
 | 
							// Specific validation based on our file types
 | 
				
			||||||
		switch file {
 | 
							switch file {
 | 
				
			||||||
		case "file1.xml":
 | 
							case "file1.xml":
 | 
				
			||||||
			if len(cmds) < 1 {
 | 
								if len(assoc.Commands) < 1 {
 | 
				
			||||||
				t.Errorf("Expected at least 1 command for file1.xml, got %d", len(cmds))
 | 
									t.Errorf("Expected at least 1 command for file1.xml, got %d", len(assoc.Commands))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// Verify at least one command with *.xml pattern
 | 
								// Verify at least one command with *.xml pattern
 | 
				
			||||||
			hasXmlGlob := false
 | 
								hasXmlGlob := false
 | 
				
			||||||
			for _, cmd := range cmds {
 | 
								for _, cmd := range assoc.Commands {
 | 
				
			||||||
				for _, glob := range cmd.Files {
 | 
									for _, glob := range cmd.Files {
 | 
				
			||||||
					if glob == "*.xml" {
 | 
										if glob == "*.xml" {
 | 
				
			||||||
						hasXmlGlob = true
 | 
											hasXmlGlob = true
 | 
				
			||||||
@@ -187,12 +190,12 @@ func TestAssociateFilesWithCommands(t *testing.T) {
 | 
				
			|||||||
				t.Errorf("Expected command with *.xml glob for file1.xml")
 | 
									t.Errorf("Expected command with *.xml glob for file1.xml")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		case "file2.txt":
 | 
							case "file2.txt":
 | 
				
			||||||
			if len(cmds) < 1 {
 | 
								if len(assoc.Commands) < 1 {
 | 
				
			||||||
				t.Errorf("Expected at least 1 command for file2.txt, got %d", len(cmds))
 | 
									t.Errorf("Expected at least 1 command for file2.txt, got %d", len(assoc.Commands))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// Verify at least one command with *.txt pattern
 | 
								// Verify at least one command with *.txt pattern
 | 
				
			||||||
			hasTxtGlob := false
 | 
								hasTxtGlob := false
 | 
				
			||||||
			for _, cmd := range cmds {
 | 
								for _, cmd := range assoc.Commands {
 | 
				
			||||||
				for _, glob := range cmd.Files {
 | 
									for _, glob := range cmd.Files {
 | 
				
			||||||
					if glob == "*.txt" {
 | 
										if glob == "*.txt" {
 | 
				
			||||||
						hasTxtGlob = true
 | 
											hasTxtGlob = true
 | 
				
			||||||
@@ -207,12 +210,12 @@ func TestAssociateFilesWithCommands(t *testing.T) {
 | 
				
			|||||||
				t.Errorf("Expected command with *.txt glob for file2.txt")
 | 
									t.Errorf("Expected command with *.txt glob for file2.txt")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		case "subdir/file3.xml":
 | 
							case "subdir/file3.xml":
 | 
				
			||||||
			if len(cmds) < 1 {
 | 
								if len(assoc.Commands) < 1 {
 | 
				
			||||||
				t.Errorf("Expected at least 1 command for subdir/file3.xml, got %d", len(cmds))
 | 
									t.Errorf("Expected at least 1 command for subdir/file3.xml, got %d", len(assoc.Commands))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// Should match both *.xml and subdir/* patterns
 | 
								// Should match both *.xml and subdir/* patterns
 | 
				
			||||||
			matches := 0
 | 
								matches := 0
 | 
				
			||||||
			for _, cmd := range cmds {
 | 
								for _, cmd := range assoc.Commands {
 | 
				
			||||||
				for _, glob := range cmd.Files {
 | 
									for _, glob := range cmd.Files {
 | 
				
			||||||
					if glob == "*.xml" || glob == "subdir/*" {
 | 
										if glob == "*.xml" || glob == "subdir/*" {
 | 
				
			||||||
						matches++
 | 
											matches++
 | 
				
			||||||
@@ -266,137 +269,15 @@ func TestAggregateGlobs(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestLoadCommandFromArgs(t *testing.T) {
 | 
					 | 
				
			||||||
	// Save original flags
 | 
					 | 
				
			||||||
	origGitFlag := *GitFlag
 | 
					 | 
				
			||||||
	origResetFlag := *ResetFlag
 | 
					 | 
				
			||||||
	origLogLevel := *LogLevel
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Restore original flags after test
 | 
					 | 
				
			||||||
	defer func() {
 | 
					 | 
				
			||||||
		*GitFlag = origGitFlag
 | 
					 | 
				
			||||||
		*ResetFlag = origResetFlag
 | 
					 | 
				
			||||||
		*LogLevel = origLogLevel
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Test cases
 | 
					 | 
				
			||||||
	tests := []struct {
 | 
					 | 
				
			||||||
		name        string
 | 
					 | 
				
			||||||
		args        []string
 | 
					 | 
				
			||||||
		gitFlag     bool
 | 
					 | 
				
			||||||
		resetFlag   bool
 | 
					 | 
				
			||||||
		logLevel    string
 | 
					 | 
				
			||||||
		shouldError bool
 | 
					 | 
				
			||||||
	}{
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			name:        "Valid command",
 | 
					 | 
				
			||||||
			args:        []string{"pattern", "expr", "file1", "file2"},
 | 
					 | 
				
			||||||
			gitFlag:     false,
 | 
					 | 
				
			||||||
			resetFlag:   false,
 | 
					 | 
				
			||||||
			logLevel:    "INFO",
 | 
					 | 
				
			||||||
			shouldError: false,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			name:        "Not enough args",
 | 
					 | 
				
			||||||
			args:        []string{"pattern", "expr"},
 | 
					 | 
				
			||||||
			gitFlag:     false,
 | 
					 | 
				
			||||||
			resetFlag:   false,
 | 
					 | 
				
			||||||
			logLevel:    "INFO",
 | 
					 | 
				
			||||||
			shouldError: true,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			name:        "With git flag",
 | 
					 | 
				
			||||||
			args:        []string{"pattern", "expr", "file1"},
 | 
					 | 
				
			||||||
			gitFlag:     true,
 | 
					 | 
				
			||||||
			resetFlag:   false,
 | 
					 | 
				
			||||||
			logLevel:    "INFO",
 | 
					 | 
				
			||||||
			shouldError: false,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			name:        "With reset flag (forces git flag)",
 | 
					 | 
				
			||||||
			args:        []string{"pattern", "expr", "file1"},
 | 
					 | 
				
			||||||
			gitFlag:     false,
 | 
					 | 
				
			||||||
			resetFlag:   true,
 | 
					 | 
				
			||||||
			logLevel:    "INFO",
 | 
					 | 
				
			||||||
			shouldError: false,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			name:        "With custom log level",
 | 
					 | 
				
			||||||
			args:        []string{"pattern", "expr", "file1"},
 | 
					 | 
				
			||||||
			gitFlag:     false,
 | 
					 | 
				
			||||||
			resetFlag:   false,
 | 
					 | 
				
			||||||
			logLevel:    "DEBUG",
 | 
					 | 
				
			||||||
			shouldError: false,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for _, tc := range tests {
 | 
					 | 
				
			||||||
		t.Run(tc.name, func(t *testing.T) {
 | 
					 | 
				
			||||||
			// Set flags for this test case
 | 
					 | 
				
			||||||
			*GitFlag = tc.gitFlag
 | 
					 | 
				
			||||||
			*ResetFlag = tc.resetFlag
 | 
					 | 
				
			||||||
			*LogLevel = tc.logLevel
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			commands, err := LoadCommandFromArgs(tc.args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if tc.shouldError {
 | 
					 | 
				
			||||||
				if err == nil {
 | 
					 | 
				
			||||||
					t.Errorf("Expected an error but got none")
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				t.Errorf("Unexpected error: %v", err)
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if len(commands) != 1 {
 | 
					 | 
				
			||||||
				t.Errorf("Expected 1 command, got %d", len(commands))
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			cmd := commands[0]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			// Check command properties
 | 
					 | 
				
			||||||
			if cmd.Regex != tc.args[0] {
 | 
					 | 
				
			||||||
				t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Regex)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if cmd.Lua != tc.args[1] {
 | 
					 | 
				
			||||||
				t.Errorf("Expected LuaExpr %q, got %q", tc.args[1], cmd.Lua)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if len(cmd.Files) != len(tc.args)-2 {
 | 
					 | 
				
			||||||
				t.Errorf("Expected %d files, got %d", len(tc.args)-2, len(cmd.Files))
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			// When reset is true, git should be true regardless of what was set
 | 
					 | 
				
			||||||
			expectedGit := tc.gitFlag || tc.resetFlag
 | 
					 | 
				
			||||||
			if cmd.Git != expectedGit {
 | 
					 | 
				
			||||||
				t.Errorf("Expected Git flag %v, got %v", expectedGit, cmd.Git)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if cmd.Reset != tc.resetFlag {
 | 
					 | 
				
			||||||
				t.Errorf("Expected Reset flag %v, got %v", tc.resetFlag, cmd.Reset)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if cmd.LogLevel != tc.logLevel {
 | 
					 | 
				
			||||||
				t.Errorf("Expected LogLevel %q, got %q", tc.logLevel, cmd.LogLevel)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Successfully unmarshal valid YAML data into ModifyCommand slice
 | 
					// Successfully unmarshal valid YAML data into ModifyCommand slice
 | 
				
			||||||
func TestLoadCommandsFromCookFileSuccess(t *testing.T) {
 | 
					func TestLoadCommandsFromCookFileSuccess(t *testing.T) {
 | 
				
			||||||
	// Arrange
 | 
						// Arrange
 | 
				
			||||||
	yamlData := []byte(`
 | 
						yamlData := []byte(`
 | 
				
			||||||
- name: command1
 | 
					- name: command1
 | 
				
			||||||
  pattern: "*.txt"
 | 
					  regex: "*.txt"
 | 
				
			||||||
  lua: replace
 | 
					  lua: replace
 | 
				
			||||||
- name: command2
 | 
					- name: command2
 | 
				
			||||||
  pattern: "*.go"
 | 
					  regex: "*.go"
 | 
				
			||||||
  lua: delete
 | 
					  lua: delete
 | 
				
			||||||
`)
 | 
					`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -420,11 +301,11 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
 | 
				
			|||||||
	yamlData := []byte(`
 | 
						yamlData := []byte(`
 | 
				
			||||||
    # This is a comment
 | 
					    # This is a comment
 | 
				
			||||||
    - name: command1
 | 
					    - name: command1
 | 
				
			||||||
      pattern: "*.txt"
 | 
					      regex: "*.txt"
 | 
				
			||||||
      lua: replace
 | 
					      lua: replace
 | 
				
			||||||
    # Another comment
 | 
					    # Another comment
 | 
				
			||||||
    - name: command2
 | 
					    - name: command2
 | 
				
			||||||
      pattern: "*.go"
 | 
					      regex: "*.go"
 | 
				
			||||||
      lua: delete
 | 
					      lua: delete
 | 
				
			||||||
    `)
 | 
					    `)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -445,7 +326,7 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
 | 
				
			|||||||
// Handle different YAML formatting styles (flow vs block)
 | 
					// Handle different YAML formatting styles (flow vs block)
 | 
				
			||||||
func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
 | 
					func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
 | 
				
			||||||
	// Arrange
 | 
						// Arrange
 | 
				
			||||||
	yamlData := []byte(`[ { name: command1, pattern: "*.txt", lua: replace }, { name: command2, pattern: "*.go", lua: delete } ]`)
 | 
						yamlData := []byte(`[ { name: command1, regex: "*.txt", lua: replace }, { name: command2, regex: "*.go", lua: delete } ]`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Act
 | 
						// Act
 | 
				
			||||||
	commands, err := LoadCommandsFromCookFile(yamlData)
 | 
						commands, err := LoadCommandsFromCookFile(yamlData)
 | 
				
			||||||
@@ -496,13 +377,13 @@ func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) {
 | 
				
			|||||||
	// Arrange
 | 
						// Arrange
 | 
				
			||||||
	yamlData := []byte(`
 | 
						yamlData := []byte(`
 | 
				
			||||||
- name: command1
 | 
					- name: command1
 | 
				
			||||||
  pattern: "*.txt"
 | 
					  regex: "*.txt"
 | 
				
			||||||
  lua: replace
 | 
					  lua: replace
 | 
				
			||||||
- name: command2
 | 
					- name: command2
 | 
				
			||||||
  pattern: "*.go"
 | 
					  regex: "*.go"
 | 
				
			||||||
  lua: delete
 | 
					  lua: delete
 | 
				
			||||||
- name: command3
 | 
					- name: command3
 | 
				
			||||||
  pattern: "*.md"
 | 
					  regex: "*.md"
 | 
				
			||||||
  lua: append
 | 
					  lua: append
 | 
				
			||||||
`)
 | 
					`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -553,155 +434,6 @@ func TestLoadCommandsFromCookFileLegitExample(t *testing.T) {
 | 
				
			|||||||
	assert.Equal(t, "crewlayabout", commands[0].Name)
 | 
						assert.Equal(t, "crewlayabout", commands[0].Name)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Valid command with minimum 3 arguments returns a ModifyCommand slice with correct values
 | 
					 | 
				
			||||||
func TestLoadCommandFromArgsWithValidArguments(t *testing.T) {
 | 
					 | 
				
			||||||
	// Setup
 | 
					 | 
				
			||||||
	oldGitFlag := GitFlag
 | 
					 | 
				
			||||||
	oldResetFlag := ResetFlag
 | 
					 | 
				
			||||||
	oldLogLevel := LogLevel
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	gitValue := true
 | 
					 | 
				
			||||||
	resetValue := false
 | 
					 | 
				
			||||||
	logLevelValue := "info"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	GitFlag = &gitValue
 | 
					 | 
				
			||||||
	ResetFlag = &resetValue
 | 
					 | 
				
			||||||
	LogLevel = &logLevelValue
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	defer func() {
 | 
					 | 
				
			||||||
		GitFlag = oldGitFlag
 | 
					 | 
				
			||||||
		ResetFlag = oldResetFlag
 | 
					 | 
				
			||||||
		LogLevel = oldLogLevel
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	args := []string{"*.go", "return x", "file1.go", "file2.go"}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Execute
 | 
					 | 
				
			||||||
	commands, err := LoadCommandFromArgs(args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Assert
 | 
					 | 
				
			||||||
	assert.NoError(t, err)
 | 
					 | 
				
			||||||
	assert.Len(t, commands, 1)
 | 
					 | 
				
			||||||
	assert.Equal(t, "*.go", commands[0].Regex)
 | 
					 | 
				
			||||||
	assert.Equal(t, "return x", commands[0].Lua)
 | 
					 | 
				
			||||||
	assert.Equal(t, []string{"file1.go", "file2.go"}, commands[0].Files)
 | 
					 | 
				
			||||||
	assert.Equal(t, true, commands[0].Git)
 | 
					 | 
				
			||||||
	assert.Equal(t, false, commands[0].Reset)
 | 
					 | 
				
			||||||
	assert.Equal(t, "info", commands[0].LogLevel)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Less than 3 arguments returns an error with appropriate message
 | 
					 | 
				
			||||||
func TestLoadCommandFromArgsWithInsufficientArguments(t *testing.T) {
 | 
					 | 
				
			||||||
	// Setup
 | 
					 | 
				
			||||||
	oldGitFlag := GitFlag
 | 
					 | 
				
			||||||
	oldResetFlag := ResetFlag
 | 
					 | 
				
			||||||
	oldLogLevel := LogLevel
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	gitValue := false
 | 
					 | 
				
			||||||
	resetValue := false
 | 
					 | 
				
			||||||
	logLevelValue := "info"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	GitFlag = &gitValue
 | 
					 | 
				
			||||||
	ResetFlag = &resetValue
 | 
					 | 
				
			||||||
	LogLevel = &logLevelValue
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	defer func() {
 | 
					 | 
				
			||||||
		GitFlag = oldGitFlag
 | 
					 | 
				
			||||||
		ResetFlag = oldResetFlag
 | 
					 | 
				
			||||||
		LogLevel = oldLogLevel
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	testCases := []struct {
 | 
					 | 
				
			||||||
		name string
 | 
					 | 
				
			||||||
		args []string
 | 
					 | 
				
			||||||
	}{
 | 
					 | 
				
			||||||
		{"empty args", []string{}},
 | 
					 | 
				
			||||||
		{"one arg", []string{"*.go"}},
 | 
					 | 
				
			||||||
		{"two args", []string{"*.go", "return x"}},
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for _, tc := range testCases {
 | 
					 | 
				
			||||||
		t.Run(tc.name, func(t *testing.T) {
 | 
					 | 
				
			||||||
			// Execute
 | 
					 | 
				
			||||||
			commands, err := LoadCommandFromArgs(tc.args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			// Assert
 | 
					 | 
				
			||||||
			assert.Error(t, err)
 | 
					 | 
				
			||||||
			assert.Nil(t, commands)
 | 
					 | 
				
			||||||
			assert.Contains(t, err.Error(), "at least 3 arguments are required")
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Pattern, Lua, and Files fields are correctly populated from args
 | 
					 | 
				
			||||||
func TestLoadCommandFromArgsPopulatesFieldsCorrectly(t *testing.T) {
 | 
					 | 
				
			||||||
	// Setup
 | 
					 | 
				
			||||||
	oldGitFlag := GitFlag
 | 
					 | 
				
			||||||
	oldResetFlag := ResetFlag
 | 
					 | 
				
			||||||
	oldLogLevel := LogLevel
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	gitValue := false
 | 
					 | 
				
			||||||
	resetValue := false
 | 
					 | 
				
			||||||
	logLevelValue := "debug"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	GitFlag = &gitValue
 | 
					 | 
				
			||||||
	ResetFlag = &resetValue
 | 
					 | 
				
			||||||
	LogLevel = &logLevelValue
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	defer func() {
 | 
					 | 
				
			||||||
		GitFlag = oldGitFlag
 | 
					 | 
				
			||||||
		ResetFlag = oldResetFlag
 | 
					 | 
				
			||||||
		LogLevel = oldLogLevel
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	args := []string{"*.txt", "print('Hello')", "file1.txt", "file2.txt"}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Execute
 | 
					 | 
				
			||||||
	commands, err := LoadCommandFromArgs(args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Assert
 | 
					 | 
				
			||||||
	assert.NoError(t, err)
 | 
					 | 
				
			||||||
	assert.Len(t, commands, 1)
 | 
					 | 
				
			||||||
	assert.Equal(t, "*.txt", commands[0].Regex)
 | 
					 | 
				
			||||||
	assert.Equal(t, "print('Hello')", commands[0].Lua)
 | 
					 | 
				
			||||||
	assert.Equal(t, []string{"file1.txt", "file2.txt"}, commands[0].Files)
 | 
					 | 
				
			||||||
	assert.Equal(t, false, commands[0].Git)
 | 
					 | 
				
			||||||
	assert.Equal(t, false, commands[0].Reset)
 | 
					 | 
				
			||||||
	assert.Equal(t, "debug", commands[0].LogLevel)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Git flag is set to true when ResetFlag is true
 | 
					 | 
				
			||||||
func TestLoadCommandFromArgsSetsGitFlagWhenResetFlagIsTrue(t *testing.T) {
 | 
					 | 
				
			||||||
	// Setup
 | 
					 | 
				
			||||||
	oldGitFlag := GitFlag
 | 
					 | 
				
			||||||
	oldResetFlag := ResetFlag
 | 
					 | 
				
			||||||
	oldLogLevel := LogLevel
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	gitValue := false
 | 
					 | 
				
			||||||
	resetValue := true
 | 
					 | 
				
			||||||
	logLevelValue := "info"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	GitFlag = &gitValue
 | 
					 | 
				
			||||||
	ResetFlag = &resetValue
 | 
					 | 
				
			||||||
	LogLevel = &logLevelValue
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	defer func() {
 | 
					 | 
				
			||||||
		GitFlag = oldGitFlag
 | 
					 | 
				
			||||||
		ResetFlag = oldResetFlag
 | 
					 | 
				
			||||||
		LogLevel = oldLogLevel
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	args := []string{"*.go", "return x", "file1.go", "file2.go"}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Execute
 | 
					 | 
				
			||||||
	commands, err := LoadCommandFromArgs(args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Assert
 | 
					 | 
				
			||||||
	assert.NoError(t, err)
 | 
					 | 
				
			||||||
	assert.Len(t, commands, 1)
 | 
					 | 
				
			||||||
	assert.Equal(t, true, commands[0].Git)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// TODO: Figure out how to mock shit
 | 
					// TODO: Figure out how to mock shit
 | 
				
			||||||
// Can't be asked doing that right now...
 | 
					// Can't be asked doing that right now...
 | 
				
			||||||
// Successfully loads commands from multiple YAML files in the current directory
 | 
					// Successfully loads commands from multiple YAML files in the current directory
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,14 @@ package utils
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"modify/logger"
 | 
					 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logger "git.site.quack-lab.dev/dave/cylogger"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// replaceCommandLogger is a scoped logger for the utils/replacecommand package.
 | 
				
			||||||
 | 
					var replaceCommandLogger = logger.Default.WithPrefix("utils/replacecommand")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ReplaceCommand struct {
 | 
					type ReplaceCommand struct {
 | 
				
			||||||
	From int
 | 
						From int
 | 
				
			||||||
	To   int
 | 
						To   int
 | 
				
			||||||
@@ -13,45 +17,63 @@ type ReplaceCommand struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ExecuteModifications(modifications []ReplaceCommand, fileData string) (string, int) {
 | 
					func ExecuteModifications(modifications []ReplaceCommand, fileData string) (string, int) {
 | 
				
			||||||
 | 
						executeModificationsLogger := replaceCommandLogger.WithPrefix("ExecuteModifications")
 | 
				
			||||||
 | 
						executeModificationsLogger.Debug("Executing a batch of text modifications")
 | 
				
			||||||
 | 
						executeModificationsLogger.Trace("Number of modifications: %d, Original file data length: %d", len(modifications), len(fileData))
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sort.Slice(modifications, func(i, j int) bool {
 | 
						sort.Slice(modifications, func(i, j int) bool {
 | 
				
			||||||
		return modifications[i].From > modifications[j].From
 | 
							return modifications[i].From > modifications[j].From
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	logger.Trace("Preparing to apply %d replacement commands in reverse order", len(modifications))
 | 
						executeModificationsLogger.Debug("Modifications sorted in reverse order for safe replacement")
 | 
				
			||||||
 | 
						executeModificationsLogger.Trace("Sorted modifications: %v", modifications)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	executed := 0
 | 
						executed := 0
 | 
				
			||||||
	for _, modification := range modifications {
 | 
						for idx, modification := range modifications {
 | 
				
			||||||
 | 
							executeModificationsLogger.Debug("Applying modification %d/%d", idx+1, len(modifications))
 | 
				
			||||||
 | 
							executeModificationsLogger.Trace("Current modification details: From=%d, To=%d, With=%q", modification.From, modification.To, modification.With)
 | 
				
			||||||
		fileData, err = modification.Execute(fileData)
 | 
							fileData, err = modification.Execute(fileData)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			logger.Error("Failed to execute replacement: %v", err)
 | 
								executeModificationsLogger.Error("Failed to execute replacement for modification %+v: %v", modification, err)
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		executed++
 | 
							executed++
 | 
				
			||||||
 | 
							executeModificationsLogger.Trace("File data length after modification: %d", len(fileData))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	logger.Info("Successfully applied %d text replacements", executed)
 | 
						executeModificationsLogger.Info("Successfully applied %d text replacements", executed)
 | 
				
			||||||
	return fileData, executed
 | 
						return fileData, executed
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *ReplaceCommand) Execute(fileDataStr string) (string, error) {
 | 
					func (m *ReplaceCommand) Execute(fileDataStr string) (string, error) {
 | 
				
			||||||
 | 
						executeLogger := replaceCommandLogger.WithPrefix("Execute").WithField("modification", fmt.Sprintf("From:%d,To:%d,With:%q", m.From, m.To, m.With))
 | 
				
			||||||
 | 
						executeLogger.Debug("Attempting to execute single replacement")
 | 
				
			||||||
	err := m.Validate(len(fileDataStr))
 | 
						err := m.Validate(len(fileDataStr))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							executeLogger.Error("Failed to validate modification: %v", err)
 | 
				
			||||||
		return fileDataStr, fmt.Errorf("failed to validate modification: %v", err)
 | 
							return fileDataStr, fmt.Errorf("failed to validate modification: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logger.Trace("Replace pos %d-%d with %q", m.From, m.To, m.With)
 | 
						executeLogger.Trace("Applying replacement: fileDataStr[:%d] + %q + fileDataStr[%d:]", m.From, m.With, m.To)
 | 
				
			||||||
	return fileDataStr[:m.From] + m.With + fileDataStr[m.To:], nil
 | 
						result := fileDataStr[:m.From] + m.With + fileDataStr[m.To:]
 | 
				
			||||||
 | 
						executeLogger.Trace("Replacement executed. Result length: %d", len(result))
 | 
				
			||||||
 | 
						return result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *ReplaceCommand) Validate(maxsize int) error {
 | 
					func (m *ReplaceCommand) Validate(maxsize int) error {
 | 
				
			||||||
 | 
						validateLogger := replaceCommandLogger.WithPrefix("Validate").WithField("modification", fmt.Sprintf("From:%d,To:%d,With:%q", m.From, m.To, m.With)).WithField("maxSize", maxsize)
 | 
				
			||||||
 | 
						validateLogger.Debug("Validating replacement command against max size")
 | 
				
			||||||
	if m.To < m.From {
 | 
						if m.To < m.From {
 | 
				
			||||||
 | 
							validateLogger.Error("Validation failed: 'To' (%d) is less than 'From' (%d)", m.To, m.From)
 | 
				
			||||||
		return fmt.Errorf("command to is less than from: %v", m)
 | 
							return fmt.Errorf("command to is less than from: %v", m)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if m.From > maxsize || m.To > maxsize {
 | 
						if m.From > maxsize || m.To > maxsize {
 | 
				
			||||||
 | 
							validateLogger.Error("Validation failed: 'From' (%d) or 'To' (%d) is greater than max size (%d)", m.From, m.To, maxsize)
 | 
				
			||||||
		return fmt.Errorf("command from or to is greater than replacement length: %v", m)
 | 
							return fmt.Errorf("command from or to is greater than replacement length: %v", m)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if m.From < 0 || m.To < 0 {
 | 
						if m.From < 0 || m.To < 0 {
 | 
				
			||||||
 | 
							validateLogger.Error("Validation failed: 'From' (%d) or 'To' (%d) is less than 0", m.From, m.To)
 | 
				
			||||||
		return fmt.Errorf("command from or to is less than 0: %v", m)
 | 
							return fmt.Errorf("command from or to is less than 0: %v", m)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						validateLogger.Debug("Modification command validated successfully")
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user