refactor(git): add output option to git functions
This commit is contained in:
parent
62b316b5ef
commit
d46a9f0883
2 changed files with 21 additions and 0 deletions
|
@ -42,6 +42,7 @@ func backup(ctx context.Context, c *config.Config) error {
|
||||||
c.GitRepo,
|
c.GitRepo,
|
||||||
git.WithBasicAuth(c.GitUser, c.GitPass),
|
git.WithBasicAuth(c.GitUser, c.GitPass),
|
||||||
git.WithBranch(c.GitBranch),
|
git.WithBranch(c.GitBranch),
|
||||||
|
git.WithOutputWriter(c.Output),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := project.Clone(ctx); err != nil {
|
if err := project.Clone(ctx); err != nil {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package git
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/go-git/go-billy/v5"
|
"github.com/go-git/go-billy/v5"
|
||||||
"github.com/go-git/go-billy/v5/memfs"
|
"github.com/go-git/go-billy/v5/memfs"
|
||||||
|
@ -30,6 +31,12 @@ func WithBranch(branch string) ProjectOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithOutputWriter(o io.Writer) ProjectOption {
|
||||||
|
return func(p *Project) {
|
||||||
|
p.writer = o
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Project struct {
|
type Project struct {
|
||||||
Branch string
|
Branch string
|
||||||
Force bool
|
Force bool
|
||||||
|
@ -40,6 +47,7 @@ type Project struct {
|
||||||
storer *memory.Storage
|
storer *memory.Storage
|
||||||
repository *git.Repository
|
repository *git.Repository
|
||||||
worktree *git.Worktree
|
worktree *git.Worktree
|
||||||
|
writer io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProject(url string, options ...ProjectOption) *Project {
|
func NewProject(url string, options ...ProjectOption) *Project {
|
||||||
|
@ -103,6 +111,10 @@ func (p *Project) Clone(ctx context.Context) error {
|
||||||
cloneOpts.Auth = p.auth
|
cloneOpts.Auth = p.auth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.writer != nil {
|
||||||
|
cloneOpts.Progress = p.writer
|
||||||
|
}
|
||||||
|
|
||||||
if err := cloneOpts.Validate(); err != nil {
|
if err := cloneOpts.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -147,6 +159,10 @@ func (p *Project) Pull(ctx context.Context) error {
|
||||||
ReferenceName: plumbing.NewBranchReferenceName(p.Branch),
|
ReferenceName: plumbing.NewBranchReferenceName(p.Branch),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.writer != nil {
|
||||||
|
pullOpts.Progress = p.writer
|
||||||
|
}
|
||||||
|
|
||||||
if err := pullOpts.Validate(); err != nil {
|
if err := pullOpts.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -168,6 +184,10 @@ func (p *Project) Push(ctx context.Context) error {
|
||||||
pushOpts.Auth = p.auth
|
pushOpts.Auth = p.auth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.writer != nil {
|
||||||
|
pushOpts.Progress = p.writer
|
||||||
|
}
|
||||||
|
|
||||||
if err := pushOpts.Validate(); err != nil {
|
if err := pushOpts.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue