refactor(git): rework git package

This commit is contained in:
Tom Neuber 2024-06-23 16:25:55 +02:00
parent 7ce90dacc4
commit 373c961deb
Signed by: tom
GPG key ID: F17EFE4272D89FF6
4 changed files with 233 additions and 269 deletions

79
pkg/git/project.go Normal file
View file

@ -0,0 +1,79 @@
package git
import (
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
)
type ProjectOption func(*Project)
func WithBasicAuth(user, pass string) ProjectOption {
return func(p *Project) {
p.auth = &http.BasicAuth{
Username: user,
Password: pass,
}
}
}
func WithBranch(branch string) ProjectOption {
return func(p *Project) {
p.Branch = branch
}
}
func WithKey(path string) ProjectOption {
return func(p *Project) {
p.KeyFile = path
}
}
type Project struct {
Branch string
RepoURL string
KeyFile string
auth transport.AuthMethod
fs billy.Filesystem
storer *memory.Storage
repository *git.Repository
}
func NewProject(url string, options ...ProjectOption) *Project {
project := &Project{
Branch: "",
RepoURL: url,
KeyFile: "",
fs: memfs.New(),
storer: memory.NewStorage(),
repository: nil,
}
for _, option := range options {
option(project)
}
return project
}
func (p *Project) Clone() error {
cloneOpts := git.CloneOptions{
URL: p.RepoURL,
}
if p.auth != nil {
cloneOpts.Auth = p.auth
}
var err error
p.repository, err = git.Clone(
p.storer,
p.fs,
&cloneOpts,
)
return err
}