Support ssh clone
Some checks failed
ci/woodpecker/push/test Pipeline failed
ci/woodpecker/push/lint Pipeline failed

This commit is contained in:
Ada 2024-05-22 02:59:08 +02:00
parent 5280ae3533
commit 94ee5804fb
Signed by: ada
GPG key ID: 6A7F898157C6DE6E
3 changed files with 51 additions and 2 deletions

View file

@ -11,6 +11,7 @@ File format used is https://toml.io/en/[toml]. You can specify config file in fi
** `RepoList`: List of mirrored git repository ** `RepoList`: List of mirrored git repository
*** `URL`: Source URL. You can use `https://<user>:<password>@git.example/repo.git` if you want http basic auth. *** `URL`: Source URL. You can use `https://<user>:<password>@git.example/repo.git` if you want http basic auth.
*** `name`: directory name of clone *** `name`: directory name of clone
*** `SSHKey`: SSH key for clone via ssh
== Example: == Example:
[source,toml] [source,toml]

View file

@ -2,8 +2,11 @@ package git
import ( import (
"errors" "errors"
"git.gnous.eu/ada/spiegel/internal/utils"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"io" "io"
"os" "os"
"strings"
goGit "github.com/go-git/go-git/v5" goGit "github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -27,6 +30,8 @@ func (c RepoConfig) fullClone() {
err := w.Close() err := w.Close()
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
return
} }
}(w) }(w)
@ -36,6 +41,27 @@ func (c RepoConfig) fullClone() {
Mirror: true, Mirror: true,
} }
if !utils.IsHttpRepo(c.URL) {
key, err := os.ReadFile(c.SSHKey)
if err != nil {
logrus.Error(err)
return
}
user := strings.Split(c.URL, "@")[0]
url := strings.Split(c.URL, "@")[1]
sshAuth, err := ssh.NewPublicKeys(user, key, "")
if err != nil {
logrus.Error(err)
return
}
repoConfig.Auth = sshAuth
repoConfig.URL = url
}
_, err := goGit.PlainClone(c.FullPath, true, repoConfig) _, err := goGit.PlainClone(c.FullPath, true, repoConfig)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
@ -60,9 +86,30 @@ func (c RepoConfig) Update() {
} }
}(w) }(w)
err = repo.Fetch(&goGit.FetchOptions{ fetchConfig := &goGit.FetchOptions{
Progress: w, Progress: w,
}) }
if !utils.IsHttpRepo(c.URL) {
key, err := os.ReadFile(c.SSHKey)
if err != nil {
logrus.Error(err)
return
}
user := strings.Split(c.URL, "@")[0]
sshAuth, err := ssh.NewPublicKeys(user, key, "")
if err != nil {
logrus.Error(err)
return
}
fetchConfig.Auth = sshAuth
}
err = repo.Fetch(fetchConfig)
if err != nil { if err != nil {
if errors.Is(err, goGit.NoErrAlreadyUpToDate) { if errors.Is(err, goGit.NoErrAlreadyUpToDate) {
logrus.Info(c.Name, " is already up-to-date") logrus.Info(c.Name, " is already up-to-date")

View file

@ -4,4 +4,5 @@ type RepoConfig struct {
URL string // Source url URL string // Source url
FullPath string // Full clone directory FullPath string // Full clone directory
Name string // Name of clone (directory name) Name string // Name of clone (directory name)
SSHKey string // SSH key for auth
} }