Skip to content

Instantly share code, notes, and snippets.

@CarlosMecha
Created June 4, 2018 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosMecha/b176a3a17d4d7f06aca7767eafab68ed to your computer and use it in GitHub Desktop.
Save CarlosMecha/b176a3a17d4d7f06aca7767eafab68ed to your computer and use it in GitHub Desktop.
Clone a GitHub repo with SSO Token using git2go
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