Add conversion functionality from yml to toml
This commit is contained in:
26
main.go
26
main.go
@@ -46,11 +46,16 @@ func main() {
|
|||||||
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, " -json\n")
|
||||||
fmt.Fprintf(os.Stderr, " Enable JSON mode for processing JSON files\n")
|
fmt.Fprintf(os.Stderr, " Enable JSON mode for processing JSON files\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " -conv\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " Convert YAML files to TOML format\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, " JSON mode:\n")
|
||||||
fmt.Fprintf(os.Stderr, " %s -json data.json\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, " %s -json data.json\n", os.Args[0])
|
||||||
|
fmt.Fprintf(os.Stderr, " YAML to TOML conversion:\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " %s -conv *.yml\n", os.Args[0])
|
||||||
|
fmt.Fprintf(os.Stderr, " %s -conv **/*.yaml\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")
|
||||||
@@ -74,6 +79,27 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle YAML to TOML conversion if -conv flag is set
|
||||||
|
if *utils.Convert {
|
||||||
|
mainLogger.Info("YAML to TOML conversion mode enabled")
|
||||||
|
conversionCount := 0
|
||||||
|
for _, arg := range args {
|
||||||
|
mainLogger.Debug("Converting YAML files matching pattern: %s", arg)
|
||||||
|
err := utils.ConvertYAMLToTOML(arg)
|
||||||
|
if err != nil {
|
||||||
|
mainLogger.Error("Failed to convert YAML files for pattern %s: %v", arg, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
conversionCount++
|
||||||
|
}
|
||||||
|
if conversionCount == 0 {
|
||||||
|
mainLogger.Warning("No files were converted. Please check your patterns.")
|
||||||
|
} else {
|
||||||
|
mainLogger.Info("Conversion completed for %d pattern(s)", conversionCount)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
mainLogger.Debug("Getting database connection")
|
mainLogger.Debug("Getting database connection")
|
||||||
db, err := utils.GetDB()
|
db, err := utils.GetDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -13,10 +13,11 @@ var (
|
|||||||
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("f", "", "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")
|
JSON = flag.Bool("json", false, "Enable JSON mode for processing JSON files")
|
||||||
|
Convert = flag.Bool("conv", false, "Convert YAML files to TOML format")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flagsLogger.Debug("Initializing command-line flags")
|
flagsLogger.Debug("Initializing command-line flags")
|
||||||
flagsLogger.Trace("Initial flag values - ParallelFiles: %d, Filter: %q, JSON: %t", *ParallelFiles, *Filter, *JSON)
|
flagsLogger.Trace("Initial flag values - ParallelFiles: %d, Filter: %q, JSON: %t, Convert: %t", *ParallelFiles, *Filter, *JSON, *Convert)
|
||||||
flagsLogger.Debug("Flag definitions: -P (parallel files), -f (filter), -json (JSON mode)")
|
flagsLogger.Debug("Flag definitions: -P (parallel files), -f (filter), -json (JSON mode), -conv (YAML to TOML conversion)")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -470,3 +470,111 @@ func LoadCommandsFromTomlFile(tomlFileData []byte) ([]ModifyCommand, error) {
|
|||||||
loadTomlCommandLogger.Trace("Unmarshaled commands: %v", commands)
|
loadTomlCommandLogger.Trace("Unmarshaled commands: %v", commands)
|
||||||
return commands, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user