diff options
author | Colton J. McCurdy <colton@stockx.com> | 2018-10-25 14:49:54 -0400 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2018-10-25 20:49:54 +0200 |
commit | cde0367919945862cab219228305b59b4b97cd13 (patch) | |
tree | 057ea7eff36b153b9f741ce24220e96a68f7c29a /_examples/clone/auth | |
parent | f22a5cb22a50e889b493b97ed98c7062d00e4504 (diff) | |
download | go-git-cde0367919945862cab219228305b59b4b97cd13.tar.gz |
examples & documentation: PlainClone with Basic Authentication (Password & Access Token) (#990)
examples: PlainClone with Basic Authentication (Password & Access Token)
Diffstat (limited to '_examples/clone/auth')
-rw-r--r-- | _examples/clone/auth/basic/access_token/main.go | 40 | ||||
-rw-r--r-- | _examples/clone/auth/basic/username_password/main.go | 37 |
2 files changed, 77 insertions, 0 deletions
diff --git a/_examples/clone/auth/basic/access_token/main.go b/_examples/clone/auth/basic/access_token/main.go new file mode 100644 index 0000000..7f6d121 --- /dev/null +++ b/_examples/clone/auth/basic/access_token/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "os" + + git "gopkg.in/src-d/go-git.v4" + . "gopkg.in/src-d/go-git.v4/_examples" + "gopkg.in/src-d/go-git.v4/plumbing/transport/http" +) + +func main() { + CheckArgs("<url>", "<directory>", "<github_access_token>") + url, directory, token := os.Args[1], os.Args[2], os.Args[3] + + // Clone the given repository to the given directory + Info("git clone %s %s", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + // The intended use of a GitHub personal access token is in replace of your password + // because access tokens can easily be revoked. + // https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ + Auth: &http.BasicAuth{ + Username: "abc123", // yes, this can be anything except an empty string + Password: token, + }, + URL: url, + Progress: os.Stdout, + }) + CheckIfError(err) + + // ... retrieving the branch being pointed by HEAD + ref, err := r.Head() + CheckIfError(err) + // ... retrieving the commit object + commit, err := r.CommitObject(ref.Hash()) + CheckIfError(err) + + fmt.Println(commit) +} diff --git a/_examples/clone/auth/basic/username_password/main.go b/_examples/clone/auth/basic/username_password/main.go new file mode 100644 index 0000000..754558c --- /dev/null +++ b/_examples/clone/auth/basic/username_password/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "os" + + git "gopkg.in/src-d/go-git.v4" + . "gopkg.in/src-d/go-git.v4/_examples" + "gopkg.in/src-d/go-git.v4/plumbing/transport/http" +) + +func main() { + CheckArgs("<url>", "<directory>", "<github_username>", "<github_password>") + url, directory, username, password := os.Args[1], os.Args[2], os.Args[3], os.Args[4] + + // Clone the given repository to the given directory + Info("git clone %s %s", url, directory) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + Auth: &http.BasicAuth{ + Username: username, + Password: password, + }, + URL: url, + Progress: os.Stdout, + }) + CheckIfError(err) + + // ... retrieving the branch being pointed by HEAD + ref, err := r.Head() + CheckIfError(err) + // ... retrieving the commit object + commit, err := r.CommitObject(ref.Hash()) + CheckIfError(err) + + fmt.Println(commit) +} |