diff --git a/share/tools.go b/share/tools.go index 26a5567..db1c78f 100644 --- a/share/tools.go +++ b/share/tools.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "strings" + "sync" "time" "github.com/gookit/color" @@ -20,6 +21,13 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/transport/http" ) +type RepoLocal struct { + path string + sizeM float32 + alert bool + error string +} + func InvalidAlert(platform string) { fmt.Printf("Tell me which repos source your want to sync, Usage: ") color.Yellow.Printf("up2 %s /Users/Zoker/repos/ or up2 %s /Users/Zoker/repo.txt\n", platform, platform) @@ -101,20 +109,23 @@ func isGitRepo(repoPath string) (isGit bool) { func printRepos(repos []string) { color.Yellow.Println(len(repos), "repositories detected, please check below: ", "\n") + alertFlag := false - for i, repo := range repos { // todo goroutine + reposLocal := getRepoLocal(repos) + + for i, repo := range reposLocal { i = i + 1 - p := fmt.Sprintf("%d. %s", i, repo) + p := fmt.Sprintf("%d. %s", i, repo.path) fmt.Printf(p) - size, outAlert, _ := repoSize(repo) - alertFlag = alertFlag || outAlert - if outAlert { - color.Red.Printf(" %.2f", size) - color.Red.Println("M") + alertFlag = alertFlag || repo.alert + if repo.alert { + color.Red.Printf(" %.2f", repo.sizeM) + color.Red.Println("M") } else { - color.Green.Printf(" %.2f", size) - color.Green.Println("M") + color.Green.Printf(" %.2f", repo.sizeM) + color.Green.Println("M") } + } if alertFlag { @@ -122,6 +133,22 @@ func printRepos(repos []string) { } } +func getRepoLocal(repos []string) (reposLocal []RepoLocal) { + var wp sync.WaitGroup + for _, path := range repos { + wp.Add(1) + go getRepoItemWorker(path, &wp, &reposLocal) + } + wp.Wait() + return reposLocal +} + +func getRepoItemWorker(path string, wp *sync.WaitGroup, reposLocal *[]RepoLocal) { + defer wp.Done() + size, outAlert, _ := repoSize(path) + *reposLocal = append(*reposLocal, RepoLocal{path: path, sizeM: size, alert: outAlert}) +} + func repoSize(path string) (float32, bool, error) { var size int64 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {