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 }