Refactor
This commit is contained in:
parent
74e7ef4451
commit
89955975bf
2 changed files with 36 additions and 28 deletions
33
internal/repositories/find.go
Normal file
33
internal/repositories/find.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
31
main.go
31
main.go
|
|
@ -3,11 +3,10 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git-repo-updater/internal/config"
|
"git-repo-updater/internal/config"
|
||||||
"git-repo-updater/internal/utils"
|
"git-repo-updater/internal/repositories"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -20,7 +19,7 @@ func main() {
|
||||||
dirs := cfg.Directories
|
dirs := cfg.Directories
|
||||||
|
|
||||||
for _, dir := range dirs {
|
for _, dir := range dirs {
|
||||||
repositories, err := findRepositoriesInDirectory(dir)
|
repositories, err := repositories.FindInDirectory(dir)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
|
|
@ -28,7 +27,7 @@ func main() {
|
||||||
lines := strings.SplitSeq(repositories, "\n")
|
lines := strings.SplitSeq(repositories, "\n")
|
||||||
|
|
||||||
for repositoryPath := range lines {
|
for repositoryPath := range lines {
|
||||||
if (repositoryPath == "") {
|
if repositoryPath == "" {
|
||||||
continue
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue