From d3475bdd8c5c61ca8ec32a1904ff7ff7382553bb Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 31 Jul 2025 20:53:40 +0100 Subject: [PATCH] Find git repositories within each directory --- internal/utils/path.go | 20 ++++++++++++++++++++ main.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 internal/utils/path.go diff --git a/internal/utils/path.go b/internal/utils/path.go new file mode 100644 index 0000000..1200fb0 --- /dev/null +++ b/internal/utils/path.go @@ -0,0 +1,20 @@ +package utils + +import ( + "os" + "strings" +) + +func ExpandPath(path string) (string, error) { + if strings.HasPrefix(path, "~") { + home, err := os.UserHomeDir() + + if err != nil { + return "", err + } + + return strings.Replace(path, "~", home, 1), nil + } + + return path, nil +} diff --git a/main.go b/main.go index 54b6911..0a1d748 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,11 @@ package main import ( "fmt" "log" + "os/exec" + "strings" "git-repo-updater/internal/config" + "git-repo-updater/internal/utils" ) func main() { @@ -14,5 +17,42 @@ func main() { log.Fatalf("Failed to load config: %v", err) } - fmt.Println("Directories:", cfg.Directories) + dirs := cfg.Directories + + for _, dir := range dirs { + repositories, err := findRepositoriesInDirectory(dir) + + if err != nil { + } + + lines := strings.SplitSeq(repositories, "\n") + + for repositoryPath := range lines { + fmt.Println(repositoryPath) + } + } +} + +func findRepositoriesInDirectory(dir string) (string, error) { + expanded, err := utils.ExpandPath(dir) + + if err != nil { + log.Fatal(err) + } + + cmd := exec.Command("find", expanded, "-type", "d", "-name", ".git", "-mindepth", "1", "-maxdepth", "2") + + output, err := cmd.CombinedOutput() + + if err != nil { + if exitErr, ok := err.(*exec.ExitError); ok { + exitCode := exitErr.ExitCode() + + fmt.Printf("Command failed with exit code %d\n", exitCode) + } + + fmt.Printf("find failed on %s: %v\nOutput: %s\n", dir, err, string(output)) + } + + return string(output), nil }