Rework variables to not be commands...
This commit is contained in:
21
main.go
21
main.go
@@ -190,29 +190,16 @@ func runModifier(args []string, cmd *cobra.Command) {
|
||||
// Load all commands
|
||||
mainLogger.Debug("Loading commands from arguments")
|
||||
mainLogger.Trace("Arguments: %v", args)
|
||||
commands, err := utils.LoadCommands(args)
|
||||
commands, modifiers, err := utils.LoadCommands(args)
|
||||
if err != nil || len(commands) == 0 {
|
||||
mainLogger.Error("Failed to load commands: %v", err)
|
||||
cmd.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(modifiers) > 0 {
|
||||
mainLogger.Info("Loaded %d global modifiers", len(modifiers))
|
||||
processor.SetVariables(modifiers)
|
||||
}
|
||||
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 filterFlag != "" {
|
||||
|
||||
67
toml_test.go
67
toml_test.go
@@ -44,7 +44,7 @@ files = ["*.txt"]
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Test loading TOML commands
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
commands, _, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
assert.NoError(t, err, "Should load TOML commands without error")
|
||||
assert.Len(t, commands, 2, "Should load 2 commands from TOML")
|
||||
|
||||
@@ -92,23 +92,19 @@ files = ["test.txt"]
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Test loading TOML commands
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
commands, modifiers, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
assert.NoError(t, err, "Should load TOML commands without error")
|
||||
assert.Len(t, commands, 2, "Should load 2 commands from TOML")
|
||||
assert.Len(t, commands, 1, "Should load 1 command from TOML")
|
||||
assert.Len(t, modifiers, 3, "Should load 3 modifiers")
|
||||
|
||||
// Verify global modifiers command (first command should have only modifiers)
|
||||
assert.Empty(t, commands[0].Name, "Global modifiers command should have no name")
|
||||
assert.Empty(t, commands[0].Regex, "Global modifiers command should have no regex")
|
||||
assert.Empty(t, commands[0].Lua, "Global modifiers command should have no lua")
|
||||
assert.Empty(t, commands[0].Files, "Global modifiers command should have no files")
|
||||
assert.Len(t, commands[0].Modifiers, 3, "Global modifiers command should have 3 modifiers")
|
||||
assert.Equal(t, int64(3), commands[0].Modifiers["multiplier"], "Multiplier should be 3")
|
||||
assert.Equal(t, "TEST_", commands[0].Modifiers["prefix"], "Prefix should be TEST_")
|
||||
assert.Equal(t, true, commands[0].Modifiers["enabled"], "Enabled should be true")
|
||||
// Verify modifiers
|
||||
assert.Equal(t, int64(3), modifiers["multiplier"], "Multiplier should be 3")
|
||||
assert.Equal(t, "TEST_", modifiers["prefix"], "Prefix should be TEST_")
|
||||
assert.Equal(t, true, modifiers["enabled"], "Enabled should be true")
|
||||
|
||||
// Verify regular command
|
||||
assert.Equal(t, "UseGlobalModifiers", commands[1].Name, "Regular command name should match")
|
||||
assert.Equal(t, "value = !num", commands[1].Regex, "Regular command regex should match")
|
||||
assert.Equal(t, "UseGlobalModifiers", commands[0].Name, "Regular command name should match")
|
||||
assert.Equal(t, "value = !num", commands[0].Regex, "Regular command regex should match")
|
||||
}
|
||||
|
||||
func TestTOMLMultilineRegex(t *testing.T) {
|
||||
@@ -166,12 +162,13 @@ isolate = true
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Test loading TOML commands
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
commands, modifiers, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
assert.NoError(t, err, "Should load TOML commands without error")
|
||||
assert.Len(t, commands, 2, "Should load 2 commands from TOML")
|
||||
assert.Len(t, commands, 1, "Should load 1 command from TOML")
|
||||
assert.Len(t, modifiers, 1, "Should load 1 modifier")
|
||||
|
||||
// Verify the multiline regex command
|
||||
multilineCmd := commands[1]
|
||||
multilineCmd := commands[0]
|
||||
assert.Equal(t, "MultilineTest", multilineCmd.Name, "Command name should match")
|
||||
assert.Contains(t, multilineCmd.Regex, "\\[config\\.settings\\]", "Regex should contain escaped brackets")
|
||||
assert.Contains(t, multilineCmd.Regex, "depth = !num", "Regex should contain depth pattern")
|
||||
@@ -225,7 +222,7 @@ files = ["*.conf", "*.ini"]
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Test loading TOML commands
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
commands, _, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
assert.NoError(t, err, "Should load TOML commands without error")
|
||||
assert.Len(t, commands, 1, "Should load 1 command from TOML")
|
||||
|
||||
@@ -276,7 +273,7 @@ files = ["config.json"]
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Test loading TOML commands
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
commands, _, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
assert.NoError(t, err, "Should load TOML commands without error")
|
||||
assert.Len(t, commands, 2, "Should load 2 commands from TOML")
|
||||
|
||||
@@ -358,9 +355,10 @@ some_other_setting = enabled = true
|
||||
os.Chdir(tmpDir)
|
||||
|
||||
// Test the complete workflow using the main function
|
||||
commands, err := utils.LoadCommands([]string{"test.toml"})
|
||||
commands, modifiers, err := utils.LoadCommands([]string{"test.toml"})
|
||||
assert.NoError(t, err, "Should load TOML commands without error")
|
||||
assert.Len(t, commands, 3, "Should load 3 commands total (including global modifiers)")
|
||||
assert.Len(t, commands, 2, "Should load 2 commands")
|
||||
assert.Len(t, modifiers, 2, "Should load 2 modifiers")
|
||||
|
||||
// Associate files with commands
|
||||
files := []string{"test.txt"}
|
||||
@@ -406,13 +404,13 @@ files = ["test.txt"
|
||||
err = os.WriteFile(invalidFile, []byte(invalidTOML), 0644)
|
||||
assert.NoError(t, err, "Should write invalid TOML file")
|
||||
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("invalid.toml")
|
||||
commands, _, err := utils.LoadCommandsFromTomlFiles("invalid.toml")
|
||||
assert.Error(t, err, "Should return error for invalid TOML syntax")
|
||||
assert.Nil(t, commands, "Should return nil commands for invalid TOML")
|
||||
assert.Contains(t, err.Error(), "failed to unmarshal TOML file", "Error should mention TOML unmarshaling")
|
||||
|
||||
// Test 2: Non-existent file
|
||||
commands, err = utils.LoadCommandsFromTomlFiles("nonexistent.toml")
|
||||
commands, _, err = utils.LoadCommandsFromTomlFiles("nonexistent.toml")
|
||||
assert.NoError(t, err, "Should handle non-existent file without error")
|
||||
assert.Empty(t, commands, "Should return empty commands for non-existent file")
|
||||
|
||||
@@ -421,7 +419,7 @@ files = ["test.txt"
|
||||
err = os.WriteFile(emptyFile, []byte(""), 0644)
|
||||
assert.NoError(t, err, "Should write empty TOML file")
|
||||
|
||||
commands, err = utils.LoadCommandsFromTomlFiles("empty.toml")
|
||||
commands, _, err = utils.LoadCommandsFromTomlFiles("empty.toml")
|
||||
assert.Error(t, err, "Should return error for empty TOML file")
|
||||
assert.Nil(t, commands, "Should return nil commands for empty TOML")
|
||||
}
|
||||
@@ -482,23 +480,14 @@ func TestYAMLToTOMLConversion(t *testing.T) {
|
||||
assert.Contains(t, tomlContent, `prefix = "CONV_"`, "TOML should contain prefix")
|
||||
|
||||
// Test that converted TOML loads correctly
|
||||
commands, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
commands, modifiers, err := utils.LoadCommandsFromTomlFiles("test.toml")
|
||||
assert.NoError(t, err, "Should load converted TOML without error")
|
||||
assert.Len(t, commands, 3, "Should load 3 commands from converted TOML")
|
||||
assert.Len(t, commands, 2, "Should load 2 commands from converted TOML")
|
||||
assert.Len(t, modifiers, 2, "Should load 2 modifiers from converted TOML")
|
||||
|
||||
// Find global modifiers command (it might not be first)
|
||||
var globalCmd utils.ModifyCommand
|
||||
foundGlobal := false
|
||||
for _, cmd := range commands {
|
||||
if cmd.Name == "GlobalModifiers" {
|
||||
globalCmd = cmd
|
||||
foundGlobal = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, foundGlobal, "Should find global modifiers command")
|
||||
assert.Equal(t, 2.5, globalCmd.Modifiers["multiplier"], "Should preserve multiplier value")
|
||||
assert.Equal(t, "CONV_", globalCmd.Modifiers["prefix"], "Should preserve prefix value")
|
||||
// Verify modifiers
|
||||
assert.Equal(t, 2.5, modifiers["multiplier"], "Should preserve multiplier value")
|
||||
assert.Equal(t, "CONV_", modifiers["prefix"], "Should preserve prefix value")
|
||||
|
||||
// Test skip functionality - run conversion again
|
||||
err = utils.ConvertYAMLToTOML("test.yml")
|
||||
|
||||
@@ -263,24 +263,29 @@ func ExpandGlobs(patterns map[string]struct{}) ([]string, error) {
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func LoadCommands(args []string) ([]ModifyCommand, error) {
|
||||
func LoadCommands(args []string) ([]ModifyCommand, map[string]interface{}, error) {
|
||||
loadCommandsLogger := modifyCommandLogger.WithPrefix("LoadCommands")
|
||||
loadCommandsLogger.Debug("Loading commands from arguments (cook files or direct patterns)")
|
||||
loadCommandsLogger.Trace("Input arguments: %v", args)
|
||||
commands := []ModifyCommand{}
|
||||
modifiers := make(map[string]interface{})
|
||||
|
||||
for _, arg := range args {
|
||||
loadCommandsLogger.Debug("Processing argument for commands: %q", arg)
|
||||
var newCommands []ModifyCommand
|
||||
var newModifiers map[string]interface{}
|
||||
var err error
|
||||
|
||||
// Check file extension to determine format
|
||||
if strings.HasSuffix(arg, ".toml") {
|
||||
loadCommandsLogger.Debug("Loading TOML commands from %q", arg)
|
||||
newCommands, err = LoadCommandsFromTomlFiles(arg)
|
||||
newCommands, newModifiers, err = LoadCommandsFromTomlFiles(arg)
|
||||
if err != nil {
|
||||
loadCommandsLogger.Error("Failed to load TOML commands from argument %q: %v", arg, err)
|
||||
return nil, fmt.Errorf("failed to load commands from TOML files: %w", err)
|
||||
return nil, nil, fmt.Errorf("failed to load commands from TOML files: %w", err)
|
||||
}
|
||||
for k, v := range newModifiers {
|
||||
modifiers[k] = v
|
||||
}
|
||||
} else {
|
||||
// Default to YAML for .yml, .yaml, or any other extension
|
||||
@@ -288,7 +293,7 @@ func LoadCommands(args []string) ([]ModifyCommand, error) {
|
||||
newCommands, err = LoadCommandsFromCookFiles(arg)
|
||||
if err != nil {
|
||||
loadCommandsLogger.Error("Failed to load YAML commands from argument %q: %v", arg, err)
|
||||
return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
|
||||
return nil, nil, fmt.Errorf("failed to load commands from cook files: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,8 +308,8 @@ func LoadCommands(args []string) ([]ModifyCommand, error) {
|
||||
}
|
||||
}
|
||||
|
||||
loadCommandsLogger.Info("Finished loading commands. Total %d commands loaded", len(commands))
|
||||
return commands, nil
|
||||
loadCommandsLogger.Info("Finished loading commands. Total %d commands and %d modifiers loaded", len(commands), len(modifiers))
|
||||
return commands, modifiers, nil
|
||||
}
|
||||
|
||||
func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
|
||||
@@ -395,16 +400,17 @@ func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand {
|
||||
return filteredCommands
|
||||
}
|
||||
|
||||
func LoadCommandsFromTomlFiles(pattern string) ([]ModifyCommand, error) {
|
||||
func LoadCommandsFromTomlFiles(pattern string) ([]ModifyCommand, map[string]interface{}, error) {
|
||||
loadTomlFilesLogger := modifyCommandLogger.WithPrefix("LoadCommandsFromTomlFiles").WithField("pattern", pattern)
|
||||
loadTomlFilesLogger.Debug("Loading commands from TOML files based on pattern")
|
||||
loadTomlFilesLogger.Trace("Input pattern: %q", pattern)
|
||||
static, pattern := SplitPattern(pattern)
|
||||
commands := []ModifyCommand{}
|
||||
modifiers := make(map[string]interface{})
|
||||
tomlFiles, err := doublestar.Glob(os.DirFS(static), pattern)
|
||||
if err != nil {
|
||||
loadTomlFilesLogger.Error("Failed to glob TOML files for pattern %q: %v", pattern, err)
|
||||
return nil, fmt.Errorf("failed to glob TOML files: %w", err)
|
||||
return nil, nil, fmt.Errorf("failed to glob TOML files: %w", err)
|
||||
}
|
||||
loadTomlFilesLogger.Debug("Found %d TOML files for pattern %q", len(tomlFiles), pattern)
|
||||
loadTomlFilesLogger.Trace("TOML files found: %v", tomlFiles)
|
||||
@@ -417,30 +423,34 @@ func LoadCommandsFromTomlFiles(pattern string) ([]ModifyCommand, error) {
|
||||
tomlFileData, err := os.ReadFile(tomlFile)
|
||||
if err != nil {
|
||||
loadTomlFilesLogger.Error("Failed to read TOML file %q: %v", tomlFile, err)
|
||||
return nil, fmt.Errorf("failed to read TOML file: %w", err)
|
||||
return nil, nil, fmt.Errorf("failed to read TOML file: %w", err)
|
||||
}
|
||||
loadTomlFilesLogger.Trace("Read %d bytes from TOML file %q", len(tomlFileData), tomlFile)
|
||||
newCommands, err := LoadCommandsFromTomlFile(tomlFileData)
|
||||
newCommands, newModifiers, err := LoadCommandsFromTomlFile(tomlFileData)
|
||||
if err != nil {
|
||||
loadTomlFilesLogger.Error("Failed to load commands from TOML file data for %q: %v", tomlFile, err)
|
||||
return nil, fmt.Errorf("failed to load commands from TOML file: %w", err)
|
||||
return nil, nil, fmt.Errorf("failed to load commands from TOML file: %w", err)
|
||||
}
|
||||
commands = append(commands, newCommands...)
|
||||
loadTomlFilesLogger.Debug("Added %d commands from TOML file %q. Total commands now: %d", len(newCommands), tomlFile, len(commands))
|
||||
for k, v := range newModifiers {
|
||||
modifiers[k] = v
|
||||
}
|
||||
loadTomlFilesLogger.Debug("Added %d commands and %d modifiers from TOML file %q. Total commands now: %d", len(newCommands), len(newModifiers), tomlFile, len(commands))
|
||||
}
|
||||
|
||||
loadTomlFilesLogger.Debug("Finished loading commands from TOML files. Total %d commands", len(commands))
|
||||
return commands, nil
|
||||
loadTomlFilesLogger.Debug("Finished loading commands from TOML files. Total %d commands and %d modifiers", len(commands), len(modifiers))
|
||||
return commands, modifiers, nil
|
||||
}
|
||||
|
||||
func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, error) {
|
||||
func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, map[string]interface{}, error) {
|
||||
loadTomlCommandLogger := modifyCommandLogger.WithPrefix("LoadCommandsFromTomlFile")
|
||||
loadTomlCommandLogger.Debug("Unmarshaling commands from TOML file data")
|
||||
loadTomlCommandLogger.Trace("TOML file data length: %d", len(tomlFileData))
|
||||
|
||||
// TOML structure for commands array
|
||||
// TOML structure for commands array and top-level modifiers
|
||||
var tomlData struct {
|
||||
Commands []ModifyCommand `toml:"commands"`
|
||||
Modifiers map[string]interface{} `toml:"modifiers,omitempty"`
|
||||
Commands []ModifyCommand `toml:"commands"`
|
||||
// Also support direct array without wrapper
|
||||
DirectCommands []ModifyCommand `toml:"-"`
|
||||
}
|
||||
@@ -449,10 +459,19 @@ func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, error) {
|
||||
err := toml.Unmarshal(tomlFileData, &tomlData)
|
||||
if err != nil {
|
||||
loadTomlCommandLogger.Error("Failed to unmarshal TOML file data: %v", err)
|
||||
return nil, fmt.Errorf("failed to unmarshal TOML file: %w", err)
|
||||
return nil, nil, fmt.Errorf("failed to unmarshal TOML file: %w", err)
|
||||
}
|
||||
|
||||
var commands []ModifyCommand
|
||||
modifiers := make(map[string]interface{})
|
||||
|
||||
// Extract top-level modifiers
|
||||
if len(tomlData.Modifiers) > 0 {
|
||||
loadTomlCommandLogger.Debug("Found %d top-level modifiers", len(tomlData.Modifiers))
|
||||
for k, v := range tomlData.Modifiers {
|
||||
modifiers[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// If we found commands in the wrapped structure, use those
|
||||
if len(tomlData.Commands) > 0 {
|
||||
@@ -460,18 +479,22 @@ func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, error) {
|
||||
loadTomlCommandLogger.Debug("Found %d commands in wrapped TOML structure", len(commands))
|
||||
} else {
|
||||
// Try to parse as direct array (similar to YAML format)
|
||||
commands = []ModifyCommand{}
|
||||
err = toml.Unmarshal(tomlFileData, &commands)
|
||||
directCommands := []ModifyCommand{}
|
||||
err = toml.Unmarshal(tomlFileData, &directCommands)
|
||||
if err != nil {
|
||||
loadTomlCommandLogger.Error("Failed to unmarshal TOML file data as direct array: %v", err)
|
||||
return nil, fmt.Errorf("failed to unmarshal TOML file as direct array: %w", err)
|
||||
return nil, nil, fmt.Errorf("failed to unmarshal TOML file as direct array: %w", err)
|
||||
}
|
||||
if len(directCommands) > 0 {
|
||||
commands = directCommands
|
||||
loadTomlCommandLogger.Debug("Found %d commands in direct TOML array", len(directCommands))
|
||||
}
|
||||
loadTomlCommandLogger.Debug("Found %d commands in direct TOML array", len(commands))
|
||||
}
|
||||
|
||||
loadTomlCommandLogger.Debug("Successfully unmarshaled %d commands", len(commands))
|
||||
loadTomlCommandLogger.Debug("Successfully unmarshaled %d commands and %d modifiers", len(commands), len(modifiers))
|
||||
loadTomlCommandLogger.Trace("Unmarshaled commands: %v", commands)
|
||||
return commands, nil
|
||||
loadTomlCommandLogger.Trace("Unmarshaled modifiers: %v", modifiers)
|
||||
return commands, modifiers, nil
|
||||
}
|
||||
|
||||
// ConvertYAMLToTOML converts YAML files to TOML format
|
||||
|
||||
@@ -260,10 +260,10 @@ func TestAggregateGlobs(t *testing.T) {
|
||||
resolvedCwd := ResolvePath(cwd)
|
||||
|
||||
expected := map[string]struct{}{
|
||||
resolvedCwd + "/*.xml": {},
|
||||
resolvedCwd + "/*.txt": {},
|
||||
resolvedCwd + "/*.json": {},
|
||||
resolvedCwd + "/subdir/*.xml": {},
|
||||
resolvedCwd + "/*.xml": {},
|
||||
resolvedCwd + "/*.txt": {},
|
||||
resolvedCwd + "/*.json": {},
|
||||
resolvedCwd + "/subdir/*.xml": {},
|
||||
}
|
||||
|
||||
if len(globs) != len(expected) {
|
||||
|
||||
Reference in New Issue
Block a user