From d46a9f0883374d897725fbb603751a5f71617d7c Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Sat, 27 Jul 2024 19:16:55 +0200 Subject: [PATCH] refactor(git): add output option to git functions --- internal/cmd/backup.go | 1 + pkg/git/project.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/internal/cmd/backup.go b/internal/cmd/backup.go index 9b18f82..f7afcca 100644 --- a/internal/cmd/backup.go +++ b/internal/cmd/backup.go @@ -42,6 +42,7 @@ func backup(ctx context.Context, c *config.Config) error { c.GitRepo, git.WithBasicAuth(c.GitUser, c.GitPass), git.WithBranch(c.GitBranch), + git.WithOutputWriter(c.Output), ) if err := project.Clone(ctx); err != nil { diff --git a/pkg/git/project.go b/pkg/git/project.go index d10a145..058f3df 100644 --- a/pkg/git/project.go +++ b/pkg/git/project.go @@ -3,6 +3,7 @@ package git import ( "context" "errors" + "io" "github.com/go-git/go-billy/v5" "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 { Branch string Force bool @@ -40,6 +47,7 @@ type Project struct { storer *memory.Storage repository *git.Repository worktree *git.Worktree + writer io.Writer } func NewProject(url string, options ...ProjectOption) *Project { @@ -103,6 +111,10 @@ func (p *Project) Clone(ctx context.Context) error { cloneOpts.Auth = p.auth } + if p.writer != nil { + cloneOpts.Progress = p.writer + } + if err := cloneOpts.Validate(); err != nil { return err } @@ -147,6 +159,10 @@ func (p *Project) Pull(ctx context.Context) error { ReferenceName: plumbing.NewBranchReferenceName(p.Branch), } + if p.writer != nil { + pullOpts.Progress = p.writer + } + if err := pullOpts.Validate(); err != nil { return err } @@ -168,6 +184,10 @@ func (p *Project) Push(ctx context.Context) error { pushOpts.Auth = p.auth } + if p.writer != nil { + pushOpts.Progress = p.writer + } + if err := pushOpts.Validate(); err != nil { return err }