From 0d66374f4f2da583bbbed1da621b778547175e60 Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Wed, 5 Apr 2017 16:43:00 -0600 Subject: [PATCH] tq,lfsapi/ssh: include `createdAt` field in struct --- lfsapi/ssh.go | 7 ++++++- tools/time_tools.go | 10 +++++----- tq/api.go | 10 ++++++++++ tq/transfer.go | 6 +++++- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lfsapi/ssh.go b/lfsapi/ssh.go index d7960296..975ad7ba 100644 --- a/lfsapi/ssh.go +++ b/lfsapi/ssh.go @@ -59,10 +59,12 @@ type sshAuthResponse struct { Header map[string]string `json:"header"` ExpiresAt time.Time `json:"expires_at"` ExpiresIn int `json:"expires_in"` + + createdAt time.Time } func (r *sshAuthResponse) IsExpiredWithin(d time.Duration) (time.Time, bool) { - return tools.IsExpiredAtOrIn(d, r.ExpiresAt, time.Duration(r.ExpiresIn)*time.Second) + return tools.IsExpiredAtOrIn(r.createdAt, d, r.ExpiresAt, time.Duration(r.ExpiresIn)*time.Second) } type sshAuthClient struct { @@ -83,6 +85,8 @@ func (c *sshAuthClient) Resolve(e Endpoint, method string) (sshAuthResponse, err cmd.Stdout = &outbuf cmd.Stderr = &errbuf + now := time.Now() + // Execute command err := cmd.Start() if err == nil { @@ -94,6 +98,7 @@ func (c *sshAuthClient) Resolve(e Endpoint, method string) (sshAuthResponse, err res.Message = strings.TrimSpace(errbuf.String()) } else { err = json.Unmarshal(outbuf.Bytes(), &res) + res.createdAt = now } return res, err diff --git a/tools/time_tools.go b/tools/time_tools.go index f2e5a037..f8bae467 100644 --- a/tools/time_tools.go +++ b/tools/time_tools.go @@ -4,21 +4,21 @@ import "time" // IsExpiredAtOrIn returns whether or not the result of calling TimeAtOrIn is // "expired" within "until" units of time from now. -func IsExpiredAtOrIn(until time.Duration, at time.Time, in time.Duration) (time.Time, bool) { - expiration := TimeAtOrIn(at, in) +func IsExpiredAtOrIn(now time.Time, until time.Duration, at time.Time, in time.Duration) (time.Time, bool) { + expiration := TimeAtOrIn(now, at, in) if expiration.IsZero() { return expiration, false } - return expiration, expiration.Before(time.Now().Add(until)) + return expiration, expiration.Before(now.Add(until)) } // TimeAtOrIn returns either "at", or the "in" duration added to the current // time. TimeAtOrIn prefers to add a duration rather than return the "at" // parameter. -func TimeAtOrIn(at time.Time, in time.Duration) time.Time { +func TimeAtOrIn(now, at time.Time, in time.Duration) time.Time { if in == 0 { return at } - return time.Now().Add(in) + return now.Add(in) } diff --git a/tq/api.go b/tq/api.go index 0452355e..a7d38eb4 100644 --- a/tq/api.go +++ b/tq/api.go @@ -1,6 +1,8 @@ package tq import ( + "time" + "github.com/git-lfs/git-lfs/errors" "github.com/git-lfs/git-lfs/lfsapi" "github.com/rubyist/tracerx" @@ -45,6 +47,8 @@ func (c *tqClient) Batch(remote string, bReq *batchRequest) (*BatchResponse, err } bRes.endpoint = c.Endpoints.Endpoint(bReq.Operation, remote) + requestedAt := time.Now() + req, err := c.NewRequest("POST", bRes.endpoint, "objects/batch", bReq) if err != nil { return nil, errors.Wrap(err, "batch request") @@ -67,5 +71,11 @@ func (c *tqClient) Batch(remote string, bReq *batchRequest) (*BatchResponse, err return nil, lfsapi.NewStatusCodeError(res) } + for _, obj := range bRes.Objects { + for _, a := range obj.Actions { + a.createdAt = requestedAt + } + } + return bRes, nil } diff --git a/tq/transfer.go b/tq/transfer.go index dca9fdbf..a200a882 100644 --- a/tq/transfer.go +++ b/tq/transfer.go @@ -90,6 +90,7 @@ func newTransfer(tr *Transfer, name string, path string) *Transfer { Header: action.Header, ExpiresAt: action.ExpiresAt, ExpiresIn: action.ExpiresIn, + createdAt: action.createdAt, } } @@ -102,6 +103,7 @@ func newTransfer(tr *Transfer, name string, path string) *Transfer { Header: link.Header, ExpiresAt: link.ExpiresAt, ExpiresIn: link.ExpiresIn, + createdAt: link.createdAt, } } } @@ -114,10 +116,12 @@ type Action struct { Header map[string]string `json:"header,omitempty"` ExpiresAt time.Time `json:"expires_at,omitempty"` ExpiresIn int `json:"expires_in,omitempty"` + + createdAt time.Time `json:"-"` } func (a *Action) IsExpiredWithin(d time.Duration) (time.Time, bool) { - return tools.IsExpiredAtOrIn(d, a.ExpiresAt, time.Duration(a.ExpiresIn)*time.Second) + return tools.IsExpiredAtOrIn(a.createdAt, d, a.ExpiresAt, time.Duration(a.ExpiresIn)*time.Second) } type ActionSet map[string]*Action