Skip to content

Commit

Permalink
git: Consolidate the use of ClientOption
Browse files Browse the repository at this point in the history
The use of options were a mix of value and funcs, which in some cases
was confusing:

NewClient(os.TempDir(), nil,
	WithDiskStorage,
	WithForcePush(),
	WithInsecureCredentialsOverHTTP)

With the changes, all options are exposed as funcs instead:

NewClient(os.TempDir(), nil,
	WithDiskStorage(),
	WithForcePush(),
	WithInsecureCredentialsOverHTTP())

The above changes aligns with the standards used in source controller
internal/reconcile/summarize.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
  • Loading branch information
Paulo Gomes committed Nov 15, 2022
1 parent 4d71f21 commit dccd499
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
34 changes: 20 additions & 14 deletions git/gogit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewClient(path string, authOpts *git.AuthOptions, clientOpts ...ClientOptio
}

if len(clientOpts) == 0 {
clientOpts = append(clientOpts, WithDiskStorage)
clientOpts = append(clientOpts, WithDiskStorage())
}

for _, clientOpt := range clientOpts {
Expand Down Expand Up @@ -106,19 +106,23 @@ func WithWorkTreeFS(wt billy.Filesystem) ClientOption {
}
}

func WithDiskStorage(g *Client) error {
wt := fs.New(g.path)
dot := fs.New(filepath.Join(g.path, extgogit.GitDirName))
func WithDiskStorage() ClientOption {
return func(c *Client) error {
wt := fs.New(c.path)
dot := fs.New(filepath.Join(c.path, extgogit.GitDirName))

g.storer = filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
g.worktreeFS = wt
return nil
c.storer = filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
c.worktreeFS = wt
return nil
}
}

func WithMemoryStorage(g *Client) error {
g.storer = memory.NewStorage()
g.worktreeFS = memfs.New()
return nil
func WithMemoryStorage() ClientOption {
return func(c *Client) error {
c.storer = memory.NewStorage()
c.worktreeFS = memfs.New()
return nil
}
}

// WithForcePush enables the use of force push for all push operations
Expand All @@ -133,9 +137,11 @@ func WithForcePush() ClientOption {

// WithInsecureCredentialsOverHTTP enables credentials being used over
// HTTP. This is not recommended for production environments.
func WithInsecureCredentialsOverHTTP(g *Client) error {
g.credentialsOverHTTP = true
return nil
func WithInsecureCredentialsOverHTTP() ClientOption {
return func(c *Client) error {
c.credentialsOverHTTP = true
return nil
}
}

func (g *Client) Init(ctx context.Context, url, branch string) error {
Expand Down
2 changes: 1 addition & 1 deletion git/gogit/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func TestForcePush(t *testing.T) {
cc2, err := commitFile(repo2, "test", "first push from second clone", time.Now())
g.Expect(err).ToNot(HaveOccurred())

ggc2, err := NewClient(tmp2, nil, WithDiskStorage, WithForcePush())
ggc2, err := NewClient(tmp2, nil, WithDiskStorage(), WithForcePush())
g.Expect(err).ToNot(HaveOccurred())
ggc2.repository = repo2

Expand Down
4 changes: 2 additions & 2 deletions git/gogit/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,9 @@ func TestClone_CredentialsOverHttp(t *testing.T) {
previousRequestCount = totalRequests

tmpDir := t.TempDir()
opts := []ClientOption{WithDiskStorage}
opts := []ClientOption{WithDiskStorage()}
if tt.allowCredentialsOverHttp {
opts = append(opts, WithInsecureCredentialsOverHTTP)
opts = append(opts, WithInsecureCredentialsOverHTTP())
}

ggc, err := NewClient(tmpDir, &git.AuthOptions{
Expand Down
26 changes: 16 additions & 10 deletions git/libgit2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewClient(path string, authOpts *git.AuthOptions, clientOpts ...ClientOptio
}

if len(clientOpts) == 0 {
clientOpts = append(clientOpts, WithDiskStorage)
clientOpts = append(clientOpts, WithDiskStorage())
}

for _, clientOpt := range clientOpts {
Expand All @@ -95,21 +95,27 @@ func NewClient(path string, authOpts *git.AuthOptions, clientOpts ...ClientOptio
return l, nil
}

func WithDiskStorage(l *Client) error {
l.repoFS = osfs.New(l.path)
return nil
func WithDiskStorage() ClientOption {
return func(c *Client) error {
c.repoFS = osfs.New(c.path)
return nil
}
}

func WithMemoryStorage(l *Client) error {
l.repoFS = memfs.New()
return nil
func WithMemoryStorage() ClientOption {
return func(c *Client) error {
c.repoFS = memfs.New()
return nil
}
}

// WithInsecureCredentialsOverHTTP enables credentials being used over
// HTTP. This is not recommended for production environments.
func WithInsecureCredentialsOverHTTP(l *Client) error {
l.credentialsOverHTTP = true
return nil
func WithInsecureCredentialsOverHTTP() ClientOption {
return func(c *Client) error {
c.credentialsOverHTTP = true
return nil
}
}

func (l *Client) Init(ctx context.Context, url, branch string) error {
Expand Down
4 changes: 2 additions & 2 deletions git/libgit2/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,9 @@ func TestClone_CredentialsOverHttp(t *testing.T) {
previousRequestCount = totalRequests

tmpDir := t.TempDir()
opts := []ClientOption{WithDiskStorage}
opts := []ClientOption{WithDiskStorage()}
if tt.allowCredentialsOverHttp {
opts = append(opts, WithInsecureCredentialsOverHTTP)
opts = append(opts, WithInsecureCredentialsOverHTTP())
}

lgc, err := NewClient(tmpDir, &git.AuthOptions{
Expand Down

0 comments on commit dccd499

Please sign in to comment.