75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
)
|
|
|
|
func main() {
|
|
// Define the output directory flag.
|
|
outDir := flag.String("out", ".", "Directory to write output files")
|
|
flag.Parse()
|
|
|
|
// Determine source of input - either a file or STDIN.
|
|
var data []byte
|
|
var err error
|
|
|
|
// If no positional arguments or "-" is provided, read from STDIN.
|
|
if flag.NArg() < 1 || flag.Arg(0) == "-" {
|
|
data, err = io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
log.Fatalf("Error reading from STDIN: %v", err)
|
|
}
|
|
} else {
|
|
inputFile := flag.Arg(0)
|
|
data, err = ioutil.ReadFile(inputFile)
|
|
if err != nil {
|
|
log.Fatalf("Error reading file %s: %v", inputFile, err)
|
|
}
|
|
}
|
|
|
|
text := string(data)
|
|
|
|
// Updated regular expression to handle both LF and CRLF line breaks.
|
|
re := regexp.MustCompile(`(?s)File:\s*(.+?)\s*\r?\n-+\r?\n(.*?)(\r?\n-+\r?\n|$)`)
|
|
matches := re.FindAllStringSubmatch(text, -1)
|
|
if matches == nil {
|
|
log.Println("No file entries found in the input.")
|
|
return
|
|
}
|
|
|
|
// Ensure the output directory exists.
|
|
if err := os.MkdirAll(*outDir, os.ModePerm); err != nil {
|
|
log.Fatalf("Error creating output directory %s: %v", *outDir, err)
|
|
}
|
|
|
|
for _, match := range matches {
|
|
// match[1] is the file name, and match[2] is the file content.
|
|
fileName := match[1]
|
|
fileContent := match[2]
|
|
|
|
// Create the full file path using the output directory.
|
|
fullPath := filepath.Join(*outDir, fileName)
|
|
|
|
// Ensure that the directory for the file exists (in case there's nested directories).
|
|
dir := filepath.Dir(fullPath)
|
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
log.Printf("Error creating directory %s: %v", dir, err)
|
|
continue
|
|
}
|
|
|
|
if err := ioutil.WriteFile(fullPath, []byte(fileContent), 0644); err != nil {
|
|
log.Printf("Error writing to file %s: %v", fullPath, err)
|
|
} else {
|
|
fmt.Printf("Created file: %s\n", fullPath)
|
|
}
|
|
}
|
|
|
|
}
|