✨ Support ssh clone
This commit is contained in:
parent
5280ae3533
commit
f47bbeaaea
4 changed files with 70 additions and 2 deletions
|
@ -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
|
||||
*** `URL`: Source URL. You can use `https://<user>:<password>@git.example/repo.git` if you want http basic auth.
|
||||
*** `name`: directory name of clone
|
||||
*** `SSHKey`: SSH key for clone via ssh
|
||||
|
||||
== Example:
|
||||
[source,toml]
|
||||
|
|
|
@ -2,8 +2,11 @@ package git
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"git.gnous.eu/ada/spiegel/internal/utils"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
goGit "github.com/go-git/go-git/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
@ -27,6 +30,8 @@ func (c RepoConfig) fullClone() {
|
|||
err := w.Close()
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
|
||||
return
|
||||
}
|
||||
}(w)
|
||||
|
||||
|
@ -36,6 +41,27 @@ func (c RepoConfig) fullClone() {
|
|||
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)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
|
@ -60,9 +86,30 @@ func (c RepoConfig) Update() {
|
|||
}
|
||||
}(w)
|
||||
|
||||
err = repo.Fetch(&goGit.FetchOptions{
|
||||
fetchConfig := &goGit.FetchOptions{
|
||||
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 errors.Is(err, goGit.NoErrAlreadyUpToDate) {
|
||||
logrus.Info(c.Name, " is already up-to-date")
|
||||
|
|
|
@ -4,4 +4,5 @@ type RepoConfig struct {
|
|||
URL string // Source url
|
||||
FullPath string // Full clone directory
|
||||
Name string // Name of clone (directory name)
|
||||
SSHKey string // SSH key for auth
|
||||
}
|
||||
|
|
19
internal/utils/utils.go
Normal file
19
internal/utils/utils.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func IsHttpRepo(url string) bool {
|
||||
regex := "^http.?://.*"
|
||||
result, err := regexp.Match(regex, []byte(url))
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
if result {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
Loading…
Reference in a new issue