Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
rostrovsky committed Jul 30, 2024
1 parent 2322ddd commit a4d971f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
27 changes: 14 additions & 13 deletions cmd/main.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{}
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -123,31 +124,31 @@ func run(cmd *cobra.Command, args []string) {
if iFlag != "" {
re, err := regexp.Compile(iFlag)
if err != nil {
logErrAndExit(err)
u.LogErrAndExit(err)
}
includeRe = re
}

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 {
Expand Down
24 changes: 12 additions & 12 deletions cmd/utils.go → pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package utils

import (
"bufio"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -98,7 +98,7 @@ func processPath(path string, prefixToRemove string, include *regexp.Regexp, exc

fences := "```"

if isMarkdown(filePath) {
if IsMarkdown(filePath) {
fences = "````"
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Binary file added sourceprompt
Binary file not shown.

0 comments on commit a4d971f

Please sign in to comment.