Implement loading lua from separate files via @

This commit is contained in:
2025-12-19 14:07:39 +01:00
parent 2fa99ec3a2
commit 98e05f4998
9 changed files with 514 additions and 22 deletions

View File

@@ -16,17 +16,18 @@ import (
var modifyCommandLogger = logger.Default.WithPrefix("utils/modifycommand")
type ModifyCommand struct {
Name string `yaml:"name,omitempty" toml:"name,omitempty"`
Regex string `yaml:"regex,omitempty" toml:"regex,omitempty"`
Regexes []string `yaml:"regexes,omitempty" toml:"regexes,omitempty"`
Lua string `yaml:"lua,omitempty" toml:"lua,omitempty"`
Files []string `yaml:"files,omitempty" toml:"files,omitempty"`
Reset bool `yaml:"reset,omitempty" toml:"reset,omitempty"`
LogLevel string `yaml:"loglevel,omitempty" toml:"loglevel,omitempty"`
Isolate bool `yaml:"isolate,omitempty" toml:"isolate,omitempty"`
NoDedup bool `yaml:"nodedup,omitempty" toml:"nodedup,omitempty"`
Disabled bool `yaml:"disable,omitempty" toml:"disable,omitempty"`
JSON bool `yaml:"json,omitempty" toml:"json,omitempty"`
Name string `yaml:"name,omitempty" toml:"name,omitempty"`
Regex string `yaml:"regex,omitempty" toml:"regex,omitempty"`
Regexes []string `yaml:"regexes,omitempty" toml:"regexes,omitempty"`
Lua string `yaml:"lua,omitempty" toml:"lua,omitempty"`
Files []string `yaml:"files,omitempty" toml:"files,omitempty"`
Reset bool `yaml:"reset,omitempty" toml:"reset,omitempty"`
LogLevel string `yaml:"loglevel,omitempty" toml:"loglevel,omitempty"`
Isolate bool `yaml:"isolate,omitempty" toml:"isolate,omitempty"`
NoDedup bool `yaml:"nodedup,omitempty" toml:"nodedup,omitempty"`
Disabled bool `yaml:"disable,omitempty" toml:"disable,omitempty"`
JSON bool `yaml:"json,omitempty" toml:"json,omitempty"`
SourceDir string `yaml:"-" toml:"-"` // Directory of the config file that loaded this command
}
type CookFile []ModifyCommand
@@ -331,6 +332,11 @@ func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, map[string]inte
loadCookFilesLogger.Error("Failed to load commands from cook file data for %q: %v", cookFile, err)
return nil, nil, fmt.Errorf("failed to load commands from cook file: %w", err)
}
// Set source directory for each command
sourceDir := filepath.Dir(cookFile)
for i := range newCommands {
newCommands[i].SourceDir = sourceDir
}
commands = append(commands, newCommands...)
for k, v := range newVariables {
variables[k] = v
@@ -429,6 +435,11 @@ func LoadCommandsFromTomlFiles(pattern string) ([]ModifyCommand, map[string]inte
loadTomlFilesLogger.Error("Failed to load commands from TOML file data for %q: %v", tomlFile, err)
return nil, nil, fmt.Errorf("failed to load commands from TOML file: %w", err)
}
// Set source directory for each command
sourceDir := filepath.Dir(tomlFile)
for i := range newCommands {
newCommands[i].SourceDir = sourceDir
}
commands = append(commands, newCommands...)
for k, v := range newVariables {
variables[k] = v