clone https with token in url

This commit is contained in:
Jay Gabriels 2020-02-29 11:26:14 -08:00
parent 44f00160f5
commit 66b5da34ab
No known key found for this signature in database
GPG Key ID: 47EC2E34963C2D56
4 changed files with 27 additions and 8 deletions

View File

@ -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]
}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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)
}