From 66b5da34aba44fc6969f436e592e5b3971076ddc Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Sat, 29 Feb 2020 11:26:14 -0800 Subject: [PATCH] clone https with token in url --- cmd/clone.go | 22 ++++++++++++++-------- cmd/clone_bitbucket.go | 4 ++++ cmd/clone_github.go | 4 ++++ cmd/clone_gitlab.go | 5 +++++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cmd/clone.go b/cmd/clone.go index ea4c3078..2d2c6a63 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -13,7 +13,6 @@ import ( "github.com/gabrie30/ghorg/configs" "github.com/korovkin/limiter" "github.com/spf13/cobra" - "gopkg.in/src-d/go-git.v4" ) var ( @@ -54,9 +53,10 @@ func init() { // Repo represents an SCM repo type Repo struct { - Name string - Path string - URL string + Name string + Path string + URL string + CloneURL string } var cloneCmd = &cobra.Command{ @@ -388,14 +388,15 @@ func CloneAllRepos() { } } } else { - args := []string{"clone", repo.URL, repoDir} + // if https clone and github/gitlab add personal access token to url + + args := []string{"clone", repo.CloneURL, repoDir} if os.Getenv("GHORG_BACKUP") == "true" { args = append(args, "--mirror") } - _, err := git.PlainClone(repoDir, false, &git.CloneOptions{ - URL: repo.URL, - }) + cmd := exec.Command("git", args...) + err := cmd.Run() if err != nil { colorlog.PrintError(fmt.Sprintf("Problem trying to clone Repo: %s Error: %v", repo.URL, err)) @@ -473,3 +474,8 @@ func ensureTrailingSlash(path string) string { return path + "/" } + +func addTokenToHTTPSCloneURL(url string, token string) string { + splitURL := strings.Split(url, "https://") + return "https://" + token + "@" + splitURL[1] +} diff --git a/cmd/clone_bitbucket.go b/cmd/clone_bitbucket.go index 0dbb14bc..41094de6 100644 --- a/cmd/clone_bitbucket.go +++ b/cmd/clone_bitbucket.go @@ -28,9 +28,11 @@ func getBitBucketOrgCloneUrls() ([]Repo, error) { r := Repo{} if os.Getenv("GHORG_CLONE_PROTOCOL") == "ssh" && linkType == "ssh" { r.URL = link.(string) + r.CloneURL = link.(string) cloneData = append(cloneData, r) } else if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" && linkType == "https" { r.URL = link.(string) + r.CloneURL = link.(string) cloneData = append(cloneData, r) } } @@ -62,9 +64,11 @@ func getBitBucketUserCloneUrls() ([]Repo, error) { r := Repo{} if os.Getenv("GHORG_CLONE_PROTOCOL") == "ssh" && linkType == "ssh" { r.URL = link.(string) + r.CloneURL = link.(string) cloneData = append(cloneData, r) } else if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" && linkType == "https" { r.URL = link.(string) + r.CloneURL = link.(string) cloneData = append(cloneData, r) } } diff --git a/cmd/clone_github.go b/cmd/clone_github.go index fce6c0aa..f6c71971 100644 --- a/cmd/clone_github.go +++ b/cmd/clone_github.go @@ -47,9 +47,11 @@ func getGitHubOrgCloneUrls() ([]Repo, error) { } if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" { + r.CloneURL = addTokenToHTTPSCloneURL(*repo.CloneURL, os.Getenv("GHORG_GITHUB_TOKEN")) r.URL = *repo.CloneURL cloneData = append(cloneData, r) } else { + r.CloneURL = *repo.SSHURL r.URL = *repo.SSHURL cloneData = append(cloneData, r) } @@ -98,9 +100,11 @@ func getGitHubUserCloneUrls() ([]Repo, error) { } r := Repo{} if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" { + r.CloneURL = addTokenToHTTPSCloneURL(*repo.CloneURL, os.Getenv("GHORG_GITHUB_TOKEN")) r.URL = *repo.CloneURL repoData = append(repoData, r) } else { + r.CloneURL = *repo.SSHURL r.URL = *repo.SSHURL repoData = append(repoData, r) } diff --git a/cmd/clone_gitlab.go b/cmd/clone_gitlab.go index a49411f2..325fb860 100644 --- a/cmd/clone_gitlab.go +++ b/cmd/clone_gitlab.go @@ -61,11 +61,14 @@ func getGitLabOrgCloneUrls() ([]Repo, error) { } } r := Repo{} + r.Path = p.PathWithNamespace if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" { + r.CloneURL = addTokenToHTTPSCloneURL(p.HTTPURLToRepo, os.Getenv("GHORG_GITLAB_TOKEN")) r.URL = p.HTTPURLToRepo repoData = append(repoData, r) } else { + r.CloneURL = p.SSHURLToRepo r.URL = p.SSHURLToRepo repoData = append(repoData, r) } @@ -123,9 +126,11 @@ func getGitLabUserCloneUrls() ([]Repo, error) { r := Repo{} r.Path = p.PathWithNamespace if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" { + r.CloneURL = addTokenToHTTPSCloneURL(p.HTTPURLToRepo, os.Getenv("GHORG_GITLAB_TOKEN")) r.URL = p.HTTPURLToRepo cloneData = append(cloneData, r) } else { + r.CloneURL = p.SSHURLToRepo r.URL = p.SSHURLToRepo cloneData = append(cloneData, r) }