first working version
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
@@ -12,9 +13,10 @@ import (
|
||||
|
||||
type (
|
||||
Repository struct {
|
||||
src string
|
||||
dst string
|
||||
auth Authentication
|
||||
src string
|
||||
dst string
|
||||
srcAuth Authentication
|
||||
dstAuth Authentication
|
||||
}
|
||||
|
||||
Authentication interface {
|
||||
@@ -22,16 +24,42 @@ type (
|
||||
}
|
||||
|
||||
TokenAuthentication struct {
|
||||
username string
|
||||
token string
|
||||
token string
|
||||
}
|
||||
|
||||
BasicAuthentication struct {
|
||||
username, password string
|
||||
}
|
||||
|
||||
NoAuthentication struct{}
|
||||
)
|
||||
|
||||
func NewRepository(src, dst string, srcAuth, dstAuth Authentication) Repository {
|
||||
return Repository{
|
||||
src: src,
|
||||
dst: dst,
|
||||
srcAuth: srcAuth,
|
||||
dstAuth: dstAuth,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTokenAuthentication(token string) TokenAuthentication {
|
||||
return TokenAuthentication{
|
||||
token: token,
|
||||
}
|
||||
}
|
||||
|
||||
func NewBasicAuthentication(username, password string) BasicAuthentication {
|
||||
return BasicAuthentication{
|
||||
username: username,
|
||||
password: password,
|
||||
}
|
||||
}
|
||||
|
||||
func Sync(r Repository) error {
|
||||
repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
URL: r.src,
|
||||
URL: r.src,
|
||||
Auth: r.srcAuth.Value(),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to clone repository from source: %w", err)
|
||||
@@ -50,11 +78,14 @@ func Sync(r Repository) error {
|
||||
|
||||
err = m.Push(&git.PushOptions{
|
||||
RemoteName: "mirror",
|
||||
Auth: r.auth.Value(),
|
||||
Auth: r.dstAuth.Value(),
|
||||
RefSpecs: []config.RefSpec{"+refs/*:refs/*"},
|
||||
Force: true,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to push to mirror server: %w", err)
|
||||
}
|
||||
|
||||
@@ -63,11 +94,18 @@ func Sync(r Repository) error {
|
||||
|
||||
func (a TokenAuthentication) Value() transport.AuthMethod {
|
||||
return &http.BasicAuth{
|
||||
Username: a.username,
|
||||
Username: "git",
|
||||
Password: a.token,
|
||||
}
|
||||
}
|
||||
|
||||
func (a BasicAuthentication) Value() transport.AuthMethod {
|
||||
return &http.BasicAuth{
|
||||
Username: a.username,
|
||||
Password: a.password,
|
||||
}
|
||||
}
|
||||
|
||||
func (NoAuthentication) Value() transport.AuthMethod {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user