581 lines
23 KiB
Go
581 lines
23 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
logger "git.site.quack-lab.dev/dave/cylogger"
|
|
"github.com/bmatcuk/doublestar/v4"
|
|
"github.com/BurntSushi/toml"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// modifyCommandLogger is a scoped logger for the utils/modifycommand package.
|
|
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"`
|
|
Modifiers map[string]interface{} `yaml:"modifiers,omitempty" toml:"modifiers,omitempty"`
|
|
}
|
|
|
|
type CookFile []ModifyCommand
|
|
|
|
func (c *ModifyCommand) Validate() error {
|
|
validateLogger := modifyCommandLogger.WithPrefix("Validate").WithField("commandName", c.Name)
|
|
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 == "" {
|
|
validateLogger.Error("Validation failed: Lua expression is required")
|
|
return fmt.Errorf("lua expression is required")
|
|
}
|
|
if len(c.Files) == 0 {
|
|
validateLogger.Error("Validation failed: At least one file is required")
|
|
return fmt.Errorf("at least one file is required")
|
|
}
|
|
if c.LogLevel == "" {
|
|
validateLogger.Debug("LogLevel not specified, defaulting to INFO")
|
|
c.LogLevel = "INFO"
|
|
}
|
|
validateLogger.Debug("Command validated successfully")
|
|
return nil
|
|
}
|
|
|
|
// Ehh.. Not much better... Guess this wasn't the big deal
|
|
var matchesMemoTable map[string]bool = make(map[string]bool)
|
|
|
|
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)
|
|
if matches, ok := matchesMemoTable[key]; ok {
|
|
matchesLogger.Debug("Found match in memo table: %t", matches)
|
|
return matches, nil
|
|
}
|
|
matches, err := doublestar.Match(glob, path)
|
|
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)
|
|
}
|
|
matchesMemoTable[key] = matches
|
|
matchesLogger.Debug("Match result: %t, storing in memo table", matches)
|
|
return matches, nil
|
|
}
|
|
|
|
func SplitPattern(pattern string) (string, string) {
|
|
splitPatternLogger := modifyCommandLogger.WithPrefix("SplitPattern").WithField("pattern", pattern)
|
|
splitPatternLogger.Debug("Splitting pattern")
|
|
splitPatternLogger.Trace("Original pattern: %q", pattern)
|
|
static, pattern := doublestar.SplitPattern(pattern)
|
|
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
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
|
|
}
|
|
if matches {
|
|
associateFilesLogger.Debug("File %q matches glob %q. Associating with command %q", file, glob, command.Name)
|
|
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++
|
|
} else {
|
|
associateFilesLogger.Trace("File %q did not match glob %q (pattern=%q, rel=%q)", file, glob, pattern, patternFile)
|
|
}
|
|
}
|
|
}
|
|
currentFileCommands := fileCommands[file]
|
|
associateFilesLogger.Debug("Finished processing file %q. Found %d regular commands and %d isolate commands", file, len(currentFileCommands.Commands), len(currentFileCommands.IsolateCommands))
|
|
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))
|
|
return fileCommands, nil
|
|
}
|
|
|
|
func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
|
|
aggregateGlobsLogger := modifyCommandLogger.WithPrefix("AggregateGlobs")
|
|
aggregateGlobsLogger.Debug("Aggregating glob patterns from commands")
|
|
aggregateGlobsLogger.Trace("Input commands for aggregation: %v", commands)
|
|
globs := make(map[string]struct{})
|
|
for _, command := range commands {
|
|
aggregateGlobsLogger.Debug("Processing command %q for glob patterns", command.Name)
|
|
for _, glob := range command.Files {
|
|
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{}{}
|
|
}
|
|
}
|
|
aggregateGlobsLogger.Debug("Finished aggregating globs. Found %d unique glob patterns", len(globs))
|
|
aggregateGlobsLogger.Trace("Aggregated unique globs: %v", globs)
|
|
return globs
|
|
}
|
|
|
|
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
|
|
filesMap := make(map[string]bool)
|
|
|
|
cwd, err := os.Getwd()
|
|
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)
|
|
}
|
|
expandGlobsLogger.Debug("Current working directory: %q", cwd)
|
|
|
|
for pattern := range patterns {
|
|
expandGlobsLogger.Debug("Processing glob pattern: %q", pattern)
|
|
static, pattern := SplitPattern(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 {
|
|
m = filepath.Join(static, m)
|
|
info, err := os.Stat(m)
|
|
if err != nil {
|
|
expandGlobsLogger.Warning("Error getting file info for %q: %v", m, err)
|
|
continue
|
|
}
|
|
if !info.IsDir() && !filesMap[m] {
|
|
expandGlobsLogger.Trace("Adding unique file to list: %q", m)
|
|
filesMap[m], files = true, append(files, m)
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(files) > 0 {
|
|
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
|
|
}
|
|
|
|
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{}
|
|
|
|
for _, arg := range args {
|
|
loadCommandsLogger.Debug("Processing argument for commands: %q", arg)
|
|
var newCommands []ModifyCommand
|
|
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)
|
|
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)
|
|
}
|
|
} else {
|
|
// Default to YAML for .yml, .yaml, or any other extension
|
|
loadCommandsLogger.Debug("Loading YAML commands from %q", arg)
|
|
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)
|
|
}
|
|
}
|
|
|
|
loadCommandsLogger.Debug("Successfully loaded %d commands from %q", len(newCommands), arg)
|
|
for _, cmd := range newCommands {
|
|
if cmd.Disabled {
|
|
loadCommandsLogger.Debug("Skipping disabled command: %q", cmd.Name)
|
|
continue
|
|
}
|
|
commands = append(commands, cmd)
|
|
loadCommandsLogger.Trace("Added command %q. Current total commands: %d", cmd.Name, len(commands))
|
|
}
|
|
}
|
|
|
|
loadCommandsLogger.Info("Finished loading commands. Total %d commands loaded", len(commands))
|
|
return commands, nil
|
|
}
|
|
|
|
func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
|
|
loadCookFilesLogger := modifyCommandLogger.WithPrefix("LoadCommandsFromCookFiles").WithField("pattern", pattern)
|
|
loadCookFilesLogger.Debug("Loading commands from cook files based on pattern")
|
|
loadCookFilesLogger.Trace("Input pattern: %q", pattern)
|
|
static, pattern := SplitPattern(pattern)
|
|
commands := []ModifyCommand{}
|
|
cookFiles, err := doublestar.Glob(os.DirFS(static), pattern)
|
|
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)
|
|
}
|
|
loadCookFilesLogger.Debug("Found %d cook files for pattern %q", len(cookFiles), pattern)
|
|
loadCookFilesLogger.Trace("Cook files found: %v", 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)
|
|
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)
|
|
}
|
|
loadCookFilesLogger.Trace("Read %d bytes from cook file %q", len(cookFileData), cookFile)
|
|
newCommands, err := LoadCommandsFromCookFile(cookFileData)
|
|
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)
|
|
}
|
|
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
|
|
}
|
|
|
|
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{}
|
|
err := yaml.Unmarshal(cookFileData, &commands)
|
|
if err != nil {
|
|
loadCommandLogger.Error("Failed to unmarshal cook file data: %v", 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
|
|
}
|
|
|
|
// CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication
|
|
func CountGlobsBeforeDedup(commands []ModifyCommand) int {
|
|
countGlobsLogger := modifyCommandLogger.WithPrefix("CountGlobsBeforeDedup")
|
|
countGlobsLogger.Debug("Counting glob patterns before deduplication")
|
|
count := 0
|
|
for _, cmd := range commands {
|
|
countGlobsLogger.Trace("Processing command %q, adding %d globs", cmd.Name, len(cmd.Files))
|
|
count += len(cmd.Files)
|
|
}
|
|
countGlobsLogger.Debug("Total glob patterns before deduplication: %d", count)
|
|
return count
|
|
}
|
|
|
|
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{}
|
|
filters := strings.Split(filter, ",")
|
|
filterCommandsLogger.Trace("Split filters: %v", filters)
|
|
for _, cmd := range commands {
|
|
filterCommandsLogger.Debug("Checking command %q against filters", cmd.Name)
|
|
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)
|
|
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
|
|
}
|
|
|
|
func LoadCommandsFromTomlFiles(pattern string) ([]ModifyCommand, 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{}
|
|
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)
|
|
}
|
|
loadTomlFilesLogger.Debug("Found %d TOML files for pattern %q", len(tomlFiles), pattern)
|
|
loadTomlFilesLogger.Trace("TOML files found: %v", tomlFiles)
|
|
|
|
for _, tomlFile := range tomlFiles {
|
|
tomlFile = filepath.Join(static, tomlFile)
|
|
tomlFile = filepath.Clean(tomlFile)
|
|
tomlFile = strings.ReplaceAll(tomlFile, "\\", "/")
|
|
loadTomlFilesLogger.Debug("Loading commands from individual TOML file: %q", tomlFile)
|
|
|
|
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)
|
|
}
|
|
loadTomlFilesLogger.Trace("Read %d bytes from TOML file %q", len(tomlFileData), tomlFile)
|
|
newCommands, 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)
|
|
}
|
|
commands = append(commands, newCommands...)
|
|
loadTomlFilesLogger.Debug("Added %d commands from TOML file %q. Total commands now: %d", len(newCommands), tomlFile, len(commands))
|
|
}
|
|
|
|
loadTomlFilesLogger.Debug("Finished loading commands from TOML files. Total %d commands", len(commands))
|
|
return commands, nil
|
|
}
|
|
|
|
func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, 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
|
|
var tomlData struct {
|
|
Commands []ModifyCommand `toml:"commands"`
|
|
// Also support direct array without wrapper
|
|
DirectCommands []ModifyCommand `toml:"-"`
|
|
}
|
|
|
|
// First try to parse as wrapped structure
|
|
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)
|
|
}
|
|
|
|
var commands []ModifyCommand
|
|
|
|
// If we found commands in the wrapped structure, use those
|
|
if len(tomlData.Commands) > 0 {
|
|
commands = tomlData.Commands
|
|
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)
|
|
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)
|
|
}
|
|
loadTomlCommandLogger.Debug("Found %d commands in direct TOML array", len(commands))
|
|
}
|
|
|
|
loadTomlCommandLogger.Debug("Successfully unmarshaled %d commands", len(commands))
|
|
loadTomlCommandLogger.Trace("Unmarshaled commands: %v", commands)
|
|
return commands, nil
|
|
}
|
|
|
|
// ConvertYAMLToTOML converts YAML files to TOML format
|
|
func ConvertYAMLToTOML(yamlPattern string) error {
|
|
convertLogger := modifyCommandLogger.WithPrefix("ConvertYAMLToTOML").WithField("pattern", yamlPattern)
|
|
convertLogger.Debug("Starting YAML to TOML conversion")
|
|
|
|
// Load YAML commands
|
|
yamlCommands, err := LoadCommandsFromCookFiles(yamlPattern)
|
|
if err != nil {
|
|
convertLogger.Error("Failed to load YAML commands: %v", err)
|
|
return fmt.Errorf("failed to load YAML commands: %w", err)
|
|
}
|
|
|
|
if len(yamlCommands) == 0 {
|
|
convertLogger.Info("No YAML commands found for pattern: %s", yamlPattern)
|
|
return nil
|
|
}
|
|
|
|
convertLogger.Debug("Loaded %d commands from YAML", len(yamlCommands))
|
|
|
|
// Find all YAML files matching the pattern
|
|
static, pattern := SplitPattern(yamlPattern)
|
|
yamlFiles, err := doublestar.Glob(os.DirFS(static), pattern)
|
|
if err != nil {
|
|
convertLogger.Error("Failed to glob YAML files: %v", err)
|
|
return fmt.Errorf("failed to glob YAML files: %w", err)
|
|
}
|
|
|
|
convertLogger.Debug("Found %d YAML files to convert", len(yamlFiles))
|
|
|
|
conversionCount := 0
|
|
skippedCount := 0
|
|
|
|
for _, yamlFile := range yamlFiles {
|
|
yamlFilePath := filepath.Join(static, yamlFile)
|
|
yamlFilePath = filepath.Clean(yamlFilePath)
|
|
yamlFilePath = strings.ReplaceAll(yamlFilePath, "\\", "/")
|
|
|
|
// Generate corresponding TOML file path
|
|
tomlFilePath := strings.TrimSuffix(yamlFilePath, filepath.Ext(yamlFilePath)) + ".toml"
|
|
|
|
convertLogger.Debug("Processing YAML file: %s -> %s", yamlFilePath, tomlFilePath)
|
|
|
|
// Check if TOML file already exists
|
|
if _, err := os.Stat(tomlFilePath); err == nil {
|
|
convertLogger.Info("Skipping conversion - TOML file already exists: %s", tomlFilePath)
|
|
skippedCount++
|
|
continue
|
|
}
|
|
|
|
// Read YAML file
|
|
yamlData, err := os.ReadFile(yamlFilePath)
|
|
if err != nil {
|
|
convertLogger.Error("Failed to read YAML file %s: %v", yamlFilePath, err)
|
|
continue
|
|
}
|
|
|
|
// Load YAML commands from this specific file
|
|
fileCommands, err := LoadCommandsFromCookFile(yamlData)
|
|
if err != nil {
|
|
convertLogger.Error("Failed to parse YAML file %s: %v", yamlFilePath, err)
|
|
continue
|
|
}
|
|
|
|
// Convert to TOML structure
|
|
tomlData, err := convertCommandsToTOML(fileCommands)
|
|
if err != nil {
|
|
convertLogger.Error("Failed to convert commands to TOML for %s: %v", yamlFilePath, err)
|
|
continue
|
|
}
|
|
|
|
// Write TOML file
|
|
err = os.WriteFile(tomlFilePath, tomlData, 0644)
|
|
if err != nil {
|
|
convertLogger.Error("Failed to write TOML file %s: %v", tomlFilePath, err)
|
|
continue
|
|
}
|
|
|
|
convertLogger.Info("Successfully converted %s to %s", yamlFilePath, tomlFilePath)
|
|
conversionCount++
|
|
}
|
|
|
|
convertLogger.Info("Conversion completed: %d files converted, %d files skipped", conversionCount, skippedCount)
|
|
return nil
|
|
}
|
|
|
|
// convertCommandsToTOML converts a slice of ModifyCommand to TOML format
|
|
func convertCommandsToTOML(commands []ModifyCommand) ([]byte, error) {
|
|
convertLogger := modifyCommandLogger.WithPrefix("convertCommandsToTOML")
|
|
convertLogger.Debug("Converting %d commands to TOML format", len(commands))
|
|
|
|
// Create TOML structure
|
|
tomlData := struct {
|
|
Commands []ModifyCommand `toml:"commands"`
|
|
}{
|
|
Commands: commands,
|
|
}
|
|
|
|
// Marshal to TOML
|
|
tomlBytes, err := toml.Marshal(tomlData)
|
|
if err != nil {
|
|
convertLogger.Error("Failed to marshal commands to TOML: %v", err)
|
|
return nil, fmt.Errorf("failed to marshal commands to TOML: %w", err)
|
|
}
|
|
|
|
convertLogger.Debug("Successfully converted %d commands to TOML (%d bytes)", len(commands), len(tomlBytes))
|
|
return tomlBytes, nil
|
|
}
|