Add conversion functionality from yml to toml

This commit is contained in:
2025-10-26 15:05:02 +01:00
parent 9d9820072a
commit 302e874710
3 changed files with 137 additions and 2 deletions

View File

@@ -470,3 +470,111 @@ func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, error) {
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
}