Find git repositories within each directory
This commit is contained in:
parent
9560cf0f11
commit
d3475bdd8c
2 changed files with 61 additions and 1 deletions
20
internal/utils/path.go
Normal file
20
internal/utils/path.go
Normal file
|
@ -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
|
||||||
|
}
|
42
main.go
42
main.go
|
@ -3,8 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git-repo-updater/internal/config"
|
"git-repo-updater/internal/config"
|
||||||
|
"git-repo-updater/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -14,5 +17,42 @@ func main() {
|
||||||
log.Fatalf("Failed to load config: %v", err)
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue