tq,lfsapi/ssh: include createdAt field in struct

This commit is contained in:
Taylor Blau 2017-04-05 16:43:00 -06:00
parent 2a1e63e4e9
commit 0d66374f4f
4 changed files with 26 additions and 7 deletions

@ -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

@ -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)
}

@ -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
}

@ -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