Skip to content

Instantly share code, notes, and snippets.

@s1moe2
Last active February 14, 2024 06:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1moe2/c08797f7e877040f0f693ff404d60d20 to your computer and use it in GitHub Desktop.
Save s1moe2/c08797f7e877040f0f693ff404d60d20 to your computer and use it in GitHub Desktop.
Clone private GitHub repo with go-git
// SSHClone clones a private GitHub repository using SSH, in the directory passed as parameter
func SSHClone(dest string) error {
var publicKey *ssh.PublicKeys
sshKey, err := ioutil.ReadFile("/Users/youruser/.ssh/id_rsa")
if err != nil {
return err
}
publicKey, err = ssh.NewPublicKeys("git", sshKey, "")
if err != nil {
return err
}
if _, err = git.PlainClone("/clone/destination/", false, &git.CloneOptions{
URL: "git@github.com:theuser/therepo.git",
Auth: publicKey,
}); err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment