Skip to content

Instantly share code, notes, and snippets.

@crazywolf132
Created May 23, 2024 02:33
Show Gist options
  • Save crazywolf132/94a423bdf85752b090ae1e02d6bdde71 to your computer and use it in GitHub Desktop.
Save crazywolf132/94a423bdf85752b090ae1e02d6bdde71 to your computer and use it in GitHub Desktop.
This is a script to combine all the files in a repo into a single `.txt` file for LLM's
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func main() {
var includeFolders string
var excludeFolders string
var testSuffix string
var output string
flag.StringVar(&includeFolders, "include", "", "Comma-separated list of folders to include")
flag.StringVar(&excludeFolders, "exclude", "snapshots", "Comma-separated list of folders to exclude")
flag.StringVar(&testSuffix, "testSuffix", "_test.go", "Suffix for identifying test files")
flag.StringVar(&output, "output", "combined", "Name of the output file without extension")
flag.Parse()
// Split the include and exclude folder lists into slices
foldersToInclude := strings.Split(includeFolders, ",")
foldersToExclude := strings.Split(excludeFolders, ",")
// Get the current working directory as the starting point
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current directory:", err)
return
}
// Create the output file
outputFile, err := os.Create(fmt.Sprintf("%s.txt", output))
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outputFile.Close()
// Walk through the specified folders recursively
for _, folder := range foldersToInclude {
// Normalize the folder path
fullFolderPath, err := filepath.Abs(filepath.Join(cwd, folder))
folderPath, err := filepath.Rel(cwd, fullFolderPath)
if err != nil {
fmt.Println("Error resolving folder path:", folder, err)
continue
}
err = filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories
if info.IsDir() {
// Check if the directory is in the exclusion list
for _, excludedFolder := range foldersToExclude {
if strings.Contains(path, excludedFolder) {
return filepath.SkipDir
}
}
return nil
}
// Skip test files
if strings.HasSuffix(info.Name(), testSuffix) {
return nil
}
// Read the file contents
content, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("Error reading file:", path, err)
return nil
}
ext := filepath.Ext(path)
language := ext[1:]
// Write the file header to the output file
fmt.Fprintf(outputFile, "--%s\n```%s\n", path, language)
outputFile.Write(content)
fmt.Fprintf(outputFile, "```\n\n")
return nil
})
if err != nil {
fmt.Println("Error walking directory:", folder, err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment