From 73b33753c0dbcf4e824fadc46a8dc8a6aa33af94 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 6 Apr 2021 14:54:12 +0000 Subject: [PATCH] locking: use an interface for lock client Right now, all of our locking happens over HTTP. However, in the future, we'll add support for a pure SSH-based protocol, and we'll want to use a different concrete implementation, so let's make our lockClient an interface and switch the concrete implementation to httpLockClient. --- locking/api.go | 17 ++++++++++++----- locking/api_test.go | 8 ++++---- locking/locks.go | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/locking/api.go b/locking/api.go index 49e78748..524097cb 100644 --- a/locking/api.go +++ b/locking/api.go @@ -10,7 +10,14 @@ import ( "github.com/git-lfs/git-lfs/lfshttp" ) -type lockClient struct { +type lockClient interface { + Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error) + Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error) + Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error) + SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error) +} + +type httpLockClient struct { *lfsapi.Client } @@ -50,7 +57,7 @@ type lockResponse struct { RequestID string `json:"request_id,omitempty"` } -func (c *lockClient) Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error) { +func (c *httpLockClient) Lock(remote string, lockReq *lockRequest) (*lockResponse, int, error) { e := c.Endpoints.Endpoint("upload", remote) req, err := c.NewRequest("POST", e, "locks", lockReq) if err != nil { @@ -101,7 +108,7 @@ type unlockResponse struct { RequestID string `json:"request_id,omitempty"` } -func (c *lockClient) Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error) { +func (c *httpLockClient) Unlock(ref *git.Ref, remote, id string, force bool) (*unlockResponse, int, error) { e := c.Endpoints.Endpoint("upload", remote) suffix := fmt.Sprintf("locks/%s/unlock", id) req, err := c.NewRequest("POST", e, suffix, &unlockRequest{ @@ -198,7 +205,7 @@ type lockList struct { RequestID string `json:"request_id,omitempty"` } -func (c *lockClient) Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error) { +func (c *httpLockClient) Search(remote string, searchReq *lockSearchRequest) (*lockList, int, error) { e := c.Endpoints.Endpoint("download", remote) req, err := c.NewRequest("GET", e, "locks", nil) if err != nil { @@ -267,7 +274,7 @@ type lockVerifiableList struct { RequestID string `json:"request_id,omitempty"` } -func (c *lockClient) SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error) { +func (c *httpLockClient) SearchVerifiable(remote string, vreq *lockVerifiableRequest) (*lockVerifiableList, int, error) { e := c.Endpoints.Endpoint("upload", remote) req, err := c.NewRequest("POST", e, "locks/verify", vreq) if err != nil { diff --git a/locking/api_test.go b/locking/api_test.go index 7c2ad6fe..f63a63ab 100644 --- a/locking/api_test.go +++ b/locking/api_test.go @@ -60,7 +60,7 @@ func TestAPILock(t *testing.T) { })) require.Nil(t, err) - lc := &lockClient{Client: c} + lc := &httpLockClient{Client: c} lockRes, status, err := lc.Lock("", &lockRequest{Path: "request", Ref: &lockRef{Name: "refs/heads/master"}}) require.Nil(t, err) assert.Equal(t, 200, status) @@ -108,7 +108,7 @@ func TestAPIUnlock(t *testing.T) { })) require.Nil(t, err) - lc := &lockClient{Client: c} + lc := &httpLockClient{Client: c} unlockRes, status, err := lc.Unlock(&git.Ref{ Name: "master", Sha: "6161616161616161616161616161616161616161", @@ -156,7 +156,7 @@ func TestAPISearch(t *testing.T) { })) require.Nil(t, err) - lc := &lockClient{Client: c} + lc := &httpLockClient{Client: c} locks, status, err := lc.Search("", &lockSearchRequest{ Filters: []lockFilter{ {Property: "a", Value: "A"}, @@ -211,7 +211,7 @@ func TestAPISearchVerifiable(t *testing.T) { })) require.Nil(t, err) - lc := &lockClient{Client: c} + lc := &httpLockClient{Client: c} locks, status, err := lc.SearchVerifiable("", &lockVerifiableRequest{ Cursor: "cursor", Limit: 5, diff --git a/locking/locks.go b/locking/locks.go index cf334401..91bba099 100644 --- a/locking/locks.go +++ b/locking/locks.go @@ -42,7 +42,7 @@ type LockCacher interface { type Client struct { Remote string RemoteRef *git.Ref - client *lockClient + client lockClient cache LockCacher cacheDir string cfg *config.Configuration @@ -63,7 +63,7 @@ type Client struct { func NewClient(remote string, lfsClient *lfsapi.Client, cfg *config.Configuration) (*Client, error) { return &Client{ Remote: remote, - client: &lockClient{Client: lfsClient}, + client: &httpLockClient{Client: lfsClient}, cache: &nilLockCacher{}, cfg: cfg, ModifyIgnoredFiles: lfsClient.GitEnv().Bool("lfs.lockignoredfiles", false),