diff --git a/cmd/main.go b/main.go similarity index 88% rename from cmd/main.go rename to main.go index 9646dfa..e46ff59 100644 --- a/cmd/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/spf13/cobra" + u "github.com/rostrovsky/sourceprompt/pkg/utils" ) const DEFAULT_PROMPT = `You will be provided with a markdown text (under the "---" separator) containing the contents of a codebase. Each code snippet will be enclosed in code fences, along with the corresponding file name. Your task is to analyze the codebase and gain a comprehensive understanding of its structure, functionality, and key features. @@ -70,8 +71,8 @@ func run(cmd *cobra.Command, args []string) { path := args[0] - if !isURL(path) && !isFilePath(path) { - logErrAndExit(fmt.Errorf("argument must be a valid git URL or file path")) + if !u.IsURL(path) && !u.IsFilePath(path) { + u.LogErrAndExit(fmt.Errorf("argument must be a valid git URL or file path")) } sb := strings.Builder{} @@ -81,9 +82,9 @@ func run(cmd *cobra.Command, args []string) { } else { if pFlag != "" { slog.Debug("Using custom prompt") - promptContent, err := getCustomPromptContent(pFlag) + promptContent, err := u.GetCustomPromptContent(pFlag) if err != nil { - logErrAndExit(err) + u.LogErrAndExit(err) } sb.Write(promptContent) sb.WriteString("\n\n") @@ -94,12 +95,12 @@ func run(cmd *cobra.Command, args []string) { prefixToRemove := "" - if isURL(path) { + if u.IsURL(path) { slog.Debug("Cloning using git", "url", path) tempDir, err := os.MkdirTemp("", "sourceprompt-git-clone-") if err != nil { - logErrAndExit(fmt.Errorf("failed to create temporary directory: %v", err)) + u.LogErrAndExit(fmt.Errorf("failed to create temporary directory: %v", err)) } defer func() { os.RemoveAll(tempDir) @@ -109,7 +110,7 @@ func run(cmd *cobra.Command, args []string) { cmd := exec.Command("git", "clone", path, tempDir) err = cmd.Run() if err != nil { - logErrAndExit(fmt.Errorf("failed to clone Git repository: %v", err)) + u.LogErrAndExit(fmt.Errorf("failed to clone Git repository: %v", err)) } slog.Debug("Repository cloned succesfully", "tempDir", tempDir) @@ -123,7 +124,7 @@ func run(cmd *cobra.Command, args []string) { if iFlag != "" { re, err := regexp.Compile(iFlag) if err != nil { - logErrAndExit(err) + u.LogErrAndExit(err) } includeRe = re } @@ -131,23 +132,23 @@ func run(cmd *cobra.Command, args []string) { if eFlag != "" { re, err := regexp.Compile(eFlag) if err != nil { - logErrAndExit(err) + u.LogErrAndExit(err) } excludeRe = re } - err := processPath(path, prefixToRemove, includeRe, excludeRe, &sb) + err := u.ProcessPath(path, prefixToRemove, includeRe, excludeRe, &sb) if err != nil { - logErrAndExit(err) + u.LogErrAndExit(err) } slog.Debug("Processing done") if oFlag != "" { slog.Debug("Saving output", "file", oFlag) - err := writeToFile(oFlag, []byte(sb.String())) + err := u.WriteToFile(oFlag, []byte(sb.String())) if err != nil { - logErrAndExit(err) + u.LogErrAndExit(err) } slog.Debug("File saved sucessfully") } else { diff --git a/cmd/utils.go b/pkg/utils/utils.go similarity index 85% rename from cmd/utils.go rename to pkg/utils/utils.go index 3e64718..3defff8 100644 --- a/cmd/utils.go +++ b/pkg/utils/utils.go @@ -1,4 +1,4 @@ -package main +package utils import ( "bufio" @@ -13,23 +13,23 @@ import ( "unicode/utf8" ) -func logErrAndExit(err error) { +func LogErrAndExit(err error) { slog.Error(err.Error()) os.Exit(1) } -func isURL(str string) bool { +func IsURL(str string) bool { u, err := url.Parse(str) return err == nil && u.Scheme != "" && u.Host != "" } -func isFilePath(str string) bool { +func IsFilePath(str string) bool { x, err := filepath.Abs(str) slog.Debug(x) return err == nil } -func isBinary(filename string) (bool, error) { +func IsBinary(filename string) (bool, error) { f, err := os.Open(filename) if err != nil { return false, err @@ -45,11 +45,11 @@ func isBinary(filename string) (bool, error) { return !utf8.ValidString(string(buf)), nil } -func isMarkdown(filename string) bool { +func IsMarkdown(filename string) bool { return strings.HasSuffix(strings.ToLower(filename), ".md") } -func processPath(path string, prefixToRemove string, include *regexp.Regexp, exclude *regexp.Regexp, stringBuilder *strings.Builder) error { +func ProcessPath(path string, prefixToRemove string, include *regexp.Regexp, exclude *regexp.Regexp, stringBuilder *strings.Builder) error { return filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error { if err != nil { return err @@ -76,7 +76,7 @@ func processPath(path string, prefixToRemove string, include *regexp.Regexp, exc } // skip binary files - isBinary, err := isBinary(filePath) + isBinary, err := IsBinary(filePath) if err != nil { return err } @@ -98,7 +98,7 @@ func processPath(path string, prefixToRemove string, include *regexp.Regexp, exc fences := "```" - if isMarkdown(filePath) { + if IsMarkdown(filePath) { fences = "````" } @@ -111,7 +111,7 @@ func processPath(path string, prefixToRemove string, include *regexp.Regexp, exc }) } -func writeToFile(filePath string, content []byte) error { +func WriteToFile(filePath string, content []byte) error { dir := filepath.Dir(filePath) err := os.MkdirAll(dir, os.ModePerm) if err != nil { @@ -138,8 +138,8 @@ func writeToFile(filePath string, content []byte) error { return nil } -func getCustomPromptContent(promptFilepathOrUrl string) ([]byte, error) { - if isURL(promptFilepathOrUrl) { +func GetCustomPromptContent(promptFilepathOrUrl string) ([]byte, error) { + if IsURL(promptFilepathOrUrl) { slog.Debug("Downloading prompt file", "url", promptFilepathOrUrl) resp, err := http.Get(promptFilepathOrUrl) if err != nil { diff --git a/sourceprompt b/sourceprompt new file mode 100755 index 0000000..b3cf98d Binary files /dev/null and b/sourceprompt differ