commit 664b6e1a77dd4ef859ad15993685ab39f1b7793e Author: Mason Payne Date: Mon Feb 3 22:55:03 2025 -0700 initial commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/llm-file-parser.iml b/.idea/llm-file-parser.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/llm-file-parser.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..39218ef --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1b5550a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module llm-file-parser + +go 1.23 diff --git a/llmparse.exe b/llmparse.exe new file mode 100644 index 0000000..9a49e93 Binary files /dev/null and b/llmparse.exe differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..1039f5d --- /dev/null +++ b/main.go @@ -0,0 +1,76 @@ +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) + + // Regular expression to match each file block. + // It matches a line starting with "File:" followed by the file name, + // then a divider line with dashes, then captures everything until the next divider. + re := regexp.MustCompile(`(?s)File:\s*(.+?)\s*\n-+\n(.*?)(\n-+\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] will be the file name, match[2] 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 nesting). + 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) + } + } + +}