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 66f5101..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,20 +74,18 @@ func CloneAllReposByOrg() error { } for _, target := range cloneTargets { - go func(repoUrl string) (string, error) { - 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) + 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 }