✨ Support clone of archived repo via /git/<name>
This commit is contained in:
parent
099419bbbd
commit
5b67a66d74
7 changed files with 72 additions and 6 deletions
|
@ -9,5 +9,5 @@ type Config struct {
|
||||||
CloneDirectory string // Repository where gir-mirror keep repository
|
CloneDirectory string // Repository where gir-mirror keep repository
|
||||||
Log log.Config
|
Log log.Config
|
||||||
Interval int // Update interval in minute
|
Interval int // Update interval in minute
|
||||||
RepoList []git.RepoConfig
|
RepoList []git.Config
|
||||||
}
|
}
|
||||||
|
|
7
internal/constant/constants.go
Normal file
7
internal/constant/constants.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package constant
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTPTimeout = 3 * time.Second
|
||||||
|
)
|
|
@ -18,7 +18,7 @@ func start(duration time.Duration, fn func(), name string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Launch all repo update background tasks.
|
// Launch all repo update background tasks.
|
||||||
func Launch(duration time.Duration, config []git.RepoConfig) {
|
func Launch(duration time.Duration, config []git.Config) {
|
||||||
var counter int
|
var counter int
|
||||||
for _, content := range config {
|
for _, content := range config {
|
||||||
counter++
|
counter++
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package git
|
package git
|
||||||
|
|
||||||
type RepoConfig struct {
|
type Config 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)
|
||||||
|
|
|
@ -8,11 +8,13 @@ import (
|
||||||
|
|
||||||
"git.gnous.eu/ada/spiegel/internal/utils"
|
"git.gnous.eu/ada/spiegel/internal/utils"
|
||||||
goGit "github.com/go-git/go-git/v5"
|
goGit "github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/serverinfo"
|
||||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||||
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartClone(repoList []RepoConfig) {
|
func StartClone(repoList []Config) {
|
||||||
logrus.Debug("Start first repository clone")
|
logrus.Debug("Start first repository clone")
|
||||||
for _, content := range repoList {
|
for _, content := range repoList {
|
||||||
_, err := os.Stat(content.FullPath)
|
_, err := os.Stat(content.FullPath)
|
||||||
|
@ -22,7 +24,7 @@ func StartClone(repoList []RepoConfig) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c RepoConfig) fullClone() {
|
func (c Config) fullClone() {
|
||||||
logrus.Debug("Clone ", c.Name, "...")
|
logrus.Debug("Clone ", c.Name, "...")
|
||||||
logger := logrus.New()
|
logger := logrus.New()
|
||||||
w := logger.Writer()
|
w := logger.Writer()
|
||||||
|
@ -66,9 +68,14 @@ func (c RepoConfig) fullClone() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = c.UpdateInfo()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c RepoConfig) Update() {
|
func (c Config) Update() {
|
||||||
repo, err := goGit.PlainOpen(c.FullPath)
|
repo, err := goGit.PlainOpen(c.FullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
|
@ -117,4 +124,23 @@ func (c RepoConfig) Update() {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = c.UpdateInfo()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) UpdateInfo() error {
|
||||||
|
r, err := goGit.PlainOpen(c.FullPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = serverinfo.UpdateServerInfo(r.Storer, r.Storer.(*filesystem.Storage).Filesystem()) //nolint: forcetypeassert
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
29
internal/router/init.go
Normal file
29
internal/router/init.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.gnous.eu/ada/spiegel/internal/constant"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Listen string
|
||||||
|
Archive string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) Router() {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
fs := http.FileServer(http.Dir(c.Archive))
|
||||||
|
mux.Handle("/git/", http.StripPrefix("/git/", fs))
|
||||||
|
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: c.Listen,
|
||||||
|
Handler: mux,
|
||||||
|
ReadHeaderTimeout: constant.HTTPTimeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Info("HTTP listen on :5000")
|
||||||
|
|
||||||
|
logrus.Fatal(server.ListenAndServe())
|
||||||
|
}
|
4
main.go
4
main.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"git.gnous.eu/ada/spiegel/internal/config"
|
"git.gnous.eu/ada/spiegel/internal/config"
|
||||||
"git.gnous.eu/ada/spiegel/internal/cron"
|
"git.gnous.eu/ada/spiegel/internal/cron"
|
||||||
"git.gnous.eu/ada/spiegel/internal/git"
|
"git.gnous.eu/ada/spiegel/internal/git"
|
||||||
|
"git.gnous.eu/ada/spiegel/internal/router"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,6 +39,9 @@ func main() {
|
||||||
logrus.Info("Config loaded")
|
logrus.Info("Config loaded")
|
||||||
logrus.Debug("Config: ", initConfig)
|
logrus.Debug("Config: ", initConfig)
|
||||||
|
|
||||||
|
routerConfig := router.Config{Listen: ":3000", Archive: initConfig.CloneDirectory}
|
||||||
|
go routerConfig.Router()
|
||||||
|
|
||||||
git.StartClone(initConfig.RepoList)
|
git.StartClone(initConfig.RepoList)
|
||||||
|
|
||||||
duration := time.Duration(initConfig.Interval) * time.Minute
|
duration := time.Duration(initConfig.Interval) * time.Minute
|
||||||
|
|
Loading…
Reference in a new issue