55 lines
1 KiB
Go
55 lines
1 KiB
Go
package repositories
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"git-repo-updater/internal/config"
|
|
"git-repo-updater/internal/utils"
|
|
)
|
|
|
|
func FindInDirectory(dir string) (string, error) {
|
|
repoPath, err := utils.ExpandPath(dir)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
repoPath, depth := splitPath(repoPath)
|
|
|
|
cmd := exec.Command("find", repoPath, "-type", "d", "-name", ".git", "-mindepth", "1", "-maxdepth", depth)
|
|
|
|
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
|
|
}
|
|
|
|
func splitPath(repoPath string) (string, string) {
|
|
cfg, err := config.Load()
|
|
|
|
if err != nil {
|
|
}
|
|
|
|
parts := strings.SplitN(repoPath, ":", 2)
|
|
|
|
repoPath = parts[0]
|
|
depth := cfg.Depth
|
|
|
|
if len(parts) == 2 {
|
|
return parts[0], parts[1]
|
|
}
|
|
|
|
return repoPath, depth
|
|
}
|