From 89955975bf2c896f5a1b90ea73c7c318f7963e6d Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 31 Jul 2025 21:23:06 +0100 Subject: [PATCH] Refactor --- internal/repositories/find.go | 33 +++++++++++++++++++++++++++++++++ main.go | 31 +++---------------------------- 2 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 internal/repositories/find.go diff --git a/internal/repositories/find.go b/internal/repositories/find.go new file mode 100644 index 0000000..0e0f764 --- /dev/null +++ b/internal/repositories/find.go @@ -0,0 +1,33 @@ +package repositories + +import ( + "fmt" + "log" + "os/exec" + + "git-repo-updater/internal/utils" +) + +func FindInDirectory(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() + + return "", fmt.Errorf("Command failed with exit code %d\n", exitCode) + } + + return "", fmt.Errorf("find failed on %s: %w\nOutput: %s", dir, err, string(output)) + } + + return string(output), nil +} diff --git a/main.go b/main.go index edc28dc..6b019c3 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,10 @@ package main import ( "fmt" "log" - "os/exec" "strings" "git-repo-updater/internal/config" - "git-repo-updater/internal/utils" + "git-repo-updater/internal/repositories" ) func main() { @@ -20,7 +19,7 @@ func main() { dirs := cfg.Directories for _, dir := range dirs { - repositories, err := findRepositoriesInDirectory(dir) + repositories, err := repositories.FindInDirectory(dir) if err != nil { } @@ -28,7 +27,7 @@ func main() { lines := strings.SplitSeq(repositories, "\n") for repositoryPath := range lines { - if (repositoryPath == "") { + if repositoryPath == "" { continue } @@ -36,27 +35,3 @@ func main() { } } } - -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() - - return "", fmt.Errorf("Command failed with exit code %d\n", exitCode) - } - - return "", fmt.Errorf("find failed on %s: %w\nOutput: %s", dir, err, string(output)) - } - - return string(output), nil -}