From 6ad207be9ca563b0791a2ad35d35a987db7aeb94 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Sat, 13 Jan 2024 09:58:28 +0000 Subject: _examples: Add git clone using ssh-agent Signed-off-by: Paulo Gomes --- _examples/clone/auth/ssh/main.go | 49 ---------------------------- _examples/clone/auth/ssh/private_key/main.go | 49 ++++++++++++++++++++++++++++ _examples/clone/auth/ssh/ssh_agent/main.go | 37 +++++++++++++++++++++ 3 files changed, 86 insertions(+), 49 deletions(-) delete mode 100644 _examples/clone/auth/ssh/main.go create mode 100644 _examples/clone/auth/ssh/private_key/main.go create mode 100644 _examples/clone/auth/ssh/ssh_agent/main.go (limited to '_examples') diff --git a/_examples/clone/auth/ssh/main.go b/_examples/clone/auth/ssh/main.go deleted file mode 100644 index 5f21d90..0000000 --- a/_examples/clone/auth/ssh/main.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "fmt" - "os" - - git "github.com/go-git/go-git/v5" - . "github.com/go-git/go-git/v5/_examples" - "github.com/go-git/go-git/v5/plumbing/transport/ssh" -) - -func main() { - CheckArgs("", "", "") - url, directory, privateKeyFile := os.Args[1], os.Args[2], os.Args[3] - var password string - if len(os.Args) == 5 { - password = os.Args[4] - } - - _, err := os.Stat(privateKeyFile) - if err != nil { - Warning("read file %s failed %s\n", privateKeyFile, err.Error()) - return - } - - // Clone the given repository to the given directory - Info("git clone %s ", url) - publicKeys, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, password) - if err != nil { - Warning("generate publickeys failed: %s\n", err.Error()) - return - } - - r, err := git.PlainClone(directory, false, &git.CloneOptions{ - Auth: publicKeys, - 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/ssh/private_key/main.go b/_examples/clone/auth/ssh/private_key/main.go new file mode 100644 index 0000000..5f21d90 --- /dev/null +++ b/_examples/clone/auth/ssh/private_key/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "os" + + git "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + "github.com/go-git/go-git/v5/plumbing/transport/ssh" +) + +func main() { + CheckArgs("", "", "") + url, directory, privateKeyFile := os.Args[1], os.Args[2], os.Args[3] + var password string + if len(os.Args) == 5 { + password = os.Args[4] + } + + _, err := os.Stat(privateKeyFile) + if err != nil { + Warning("read file %s failed %s\n", privateKeyFile, err.Error()) + return + } + + // Clone the given repository to the given directory + Info("git clone %s ", url) + publicKeys, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, password) + if err != nil { + Warning("generate publickeys failed: %s\n", err.Error()) + return + } + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + Auth: publicKeys, + 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/ssh/ssh_agent/main.go b/_examples/clone/auth/ssh/ssh_agent/main.go new file mode 100644 index 0000000..7a2ebd3 --- /dev/null +++ b/_examples/clone/auth/ssh/ssh_agent/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "os" + + git "github.com/go-git/go-git/v5" + . "github.com/go-git/go-git/v5/_examples" + "github.com/go-git/go-git/v5/plumbing/transport/ssh" +) + +func main() { + CheckArgs("", "") + url, directory := os.Args[1], os.Args[2] + + authMethod, err := ssh.NewSSHAgentAuth("git") + CheckIfError(err) + + // Clone the given repository to the given directory + Info("git clone %s ", url) + + r, err := git.PlainClone(directory, false, &git.CloneOptions{ + Auth: authMethod, + 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) +} -- cgit