Created
June 4, 2018 20:17
-
-
Save CarlosMecha/b176a3a17d4d7f06aca7767eafab68ed to your computer and use it in GitHub Desktop.
Clone a GitHub repo with SSO Token using git2go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"gopkg.in/libgit2/git2go.v27" | |
) | |
func credentialsCallback(url, username string, t git.CredType) (git.ErrorCode, *git.Cred) { | |
ret, cred := git.NewCredUserpassPlaintext(os.Getenv("GITHUB_TOKEN"), "x-oauth-basic") | |
return git.ErrOk, &cred | |
} | |
func printMessage(msg string) git.ErrorCode { | |
fmt.Fprintf(os.Stderr, "Message %s\n", msg) | |
return git.ErrOk | |
} | |
func main() { | |
if os.Getenv("GITHUB_TOKEN") == "" { | |
fmt.Fprintf(os.Stderr, "Please export $GITHUB_TOKEN with a personal token and try again.") | |
os.Exit(1) | |
} | |
options := &git.CloneOptions{ | |
FetchOptions: &git.FetchOptions{ | |
RemoteCallbacks: git.RemoteCallbacks{ | |
CredentialsCallback: credentialsCallback, | |
SidebandProgressCallback: printMessage, | |
}, | |
}, | |
} | |
repo, err := git.Clone("https://github.com/my-org/my-repo", "./repo", options) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Error cloning repo %s\n", err.Error()) | |
os.Exit(1) | |
} | |
fmt.Printf("Repo %+v\n", repo) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment