From 0b0736952cc7321879cb7f37ed87dbb3c90a281a Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Thu, 29 Mar 2018 21:42:46 -0700 Subject: [PATCH 1/2] update return value --- cmd/clone.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/clone.go b/cmd/clone.go index 66f5101..5eb1c5c 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -55,16 +55,13 @@ func CloneAllReposByOrg() error { } for _, target := range cloneTargets { - go func(repoUrl string) (string, error) { + go func(repoUrl string) { fmt.Println("Cloning!!!!!!", repoUrl) cmd := exec.Command("git", "clone", repoUrl) err := cmd.Run() if err != nil { fmt.Print("ERROR DETECTEDs") - return repoUrl, err } - - return "Done", nil }(target) } From fdf3cdcfe978dada9d2084c3be2f621fd27238fc Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Fri, 30 Mar 2018 05:50:33 -0700 Subject: [PATCH 2/2] clone working synchronously --- README.md | 2 +- cmd/clone.go | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 79f64e4..37fd20d 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,5 @@ Github search is terrible. The idea here is quickly clone all org repos into a s - `$ ghorg nameoforg` ### Auth -- Create a .env add your personal access token +- `$cp .env-sample .env` add your personal access token - If org is behind SSO add it to the token https://help.github.com/articles/authorizing-a-personal-access-token-for-use-with-a-saml-single-sign-on-organization/ diff --git a/cmd/clone.go b/cmd/clone.go index 5eb1c5c..06024d2 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -6,6 +6,8 @@ import ( "fmt" "os" "os/exec" + "strings" + "time" "github.com/google/go-github/github" "golang.org/x/oauth2" @@ -46,8 +48,25 @@ func getAllOrgCloneUrls() ([]string, error) { return cloneUrls, nil } +func CreateDirIfNotExist() { + clonePath := os.Getenv("ABSOLUTE_PATH_TO_CLONE_TO") + if _, err := os.Stat(clonePath); os.IsNotExist(err) { + err = os.MkdirAll(clonePath, 0755) + if err != nil { + panic(err) + } + } +} + +func getAppNameFromURL(url string) string { + withGit := strings.Split(url, "/") + appName := withGit[len(withGit)-1] + return strings.Split(appName, ".")[0] +} + // CloneAllReposByOrg clones all repos for a given org func CloneAllReposByOrg() error { + CreateDirIfNotExist() cloneTargets, err := getAllOrgCloneUrls() if err != nil { @@ -55,17 +74,18 @@ func CloneAllReposByOrg() error { } for _, target := range cloneTargets { - go func(repoUrl string) { - fmt.Println("Cloning!!!!!!", repoUrl) - cmd := exec.Command("git", "clone", repoUrl) - err := cmd.Run() - if err != nil { - fmt.Print("ERROR DETECTEDs") - } - }(target) + appName := getAppNameFromURL(target) + // go func(repoUrl string) { + fmt.Println("Cloning!!!!!!", target) + cmd := exec.Command("git", "clone", target, os.Getenv("ABSOLUTE_PATH_TO_CLONE_TO")+"/"+appName) + err := cmd.Run() + if err != nil { + fmt.Println("ERROR DETECTED while cloning...", err) + } + // }(target) } - fmt.Scanln("Press any key when things look done") + time.Sleep(30) return nil }