spiegel/internal/git/clone.go
Ada cece0ed3bb
Some checks failed
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/lint Pipeline failed
Clone repository on start + Update repository
2024-04-04 20:53:35 +02:00

73 lines
1.3 KiB
Go

package git
import (
"errors"
"io"
"log"
"os"
goGit "github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
)
func StartClone(repoList []RepoConfig) {
logrus.Debug("Start first repository clone")
for _, content := range repoList {
_, err := os.Stat(content.FullPath)
if os.IsNotExist(err) {
content.fullClone()
}
}
}
func (config RepoConfig) fullClone() {
logrus.Debug("Clone ", config.Name, "...")
logger := logrus.New()
w := logger.Writer()
defer func(w *io.PipeWriter) {
err := w.Close()
if err != nil {
logrus.Error(err)
}
}(w)
_, err := goGit.PlainClone(config.FullPath, true, &goGit.CloneOptions{
URL: config.URL,
Progress: w,
Mirror: true,
})
if err != nil {
log.Panic(err)
}
}
func (config RepoConfig) Update() {
repo, err := goGit.PlainOpen(config.FullPath)
if err != nil {
logrus.Error(err)
return
}
logrus.Debug("Clone ", config.Name, "...")
logger := logrus.New()
w := logger.Writer()
defer func(w *io.PipeWriter) {
err := w.Close()
if err != nil {
logrus.Error(err)
}
}(w)
err = repo.Fetch(&goGit.FetchOptions{
Progress: w,
})
if err != nil {
if errors.Is(err, goGit.NoErrAlreadyUpToDate) {
logrus.Info(config.Name, " is already up-to-date")
} else {
logrus.Error(err)
}
}
}