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"` Header map[string]string `json:"header"`
ExpiresAt time.Time `json:"expires_at"` ExpiresAt time.Time `json:"expires_at"`
ExpiresIn int `json:"expires_in"` ExpiresIn int `json:"expires_in"`
createdAt time.Time
} }
func (r *sshAuthResponse) IsExpiredWithin(d time.Duration) (time.Time, bool) { 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 { type sshAuthClient struct {
@ -83,6 +85,8 @@ func (c *sshAuthClient) Resolve(e Endpoint, method string) (sshAuthResponse, err
cmd.Stdout = &outbuf cmd.Stdout = &outbuf
cmd.Stderr = &errbuf cmd.Stderr = &errbuf
now := time.Now()
// Execute command // Execute command
err := cmd.Start() err := cmd.Start()
if err == nil { if err == nil {
@ -94,6 +98,7 @@ func (c *sshAuthClient) Resolve(e Endpoint, method string) (sshAuthResponse, err
res.Message = strings.TrimSpace(errbuf.String()) res.Message = strings.TrimSpace(errbuf.String())
} else { } else {
err = json.Unmarshal(outbuf.Bytes(), &res) err = json.Unmarshal(outbuf.Bytes(), &res)
res.createdAt = now
} }
return res, err return res, err

@ -4,21 +4,21 @@ import "time"
// IsExpiredAtOrIn returns whether or not the result of calling TimeAtOrIn is // IsExpiredAtOrIn returns whether or not the result of calling TimeAtOrIn is
// "expired" within "until" units of time from now. // "expired" within "until" units of time from now.
func IsExpiredAtOrIn(until time.Duration, at time.Time, in time.Duration) (time.Time, bool) { func IsExpiredAtOrIn(now time.Time, until time.Duration, at time.Time, in time.Duration) (time.Time, bool) {
expiration := TimeAtOrIn(at, in) expiration := TimeAtOrIn(now, at, in)
if expiration.IsZero() { if expiration.IsZero() {
return expiration, false 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 // TimeAtOrIn returns either "at", or the "in" duration added to the current
// time. TimeAtOrIn prefers to add a duration rather than return the "at" // time. TimeAtOrIn prefers to add a duration rather than return the "at"
// parameter. // 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 { if in == 0 {
return at return at
} }
return time.Now().Add(in) return now.Add(in)
} }

@ -1,6 +1,8 @@
package tq package tq
import ( import (
"time"
"github.com/git-lfs/git-lfs/errors" "github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/lfsapi" "github.com/git-lfs/git-lfs/lfsapi"
"github.com/rubyist/tracerx" "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) bRes.endpoint = c.Endpoints.Endpoint(bReq.Operation, remote)
requestedAt := time.Now()
req, err := c.NewRequest("POST", bRes.endpoint, "objects/batch", bReq) req, err := c.NewRequest("POST", bRes.endpoint, "objects/batch", bReq)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "batch request") 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) return nil, lfsapi.NewStatusCodeError(res)
} }
for _, obj := range bRes.Objects {
for _, a := range obj.Actions {
a.createdAt = requestedAt
}
}
return bRes, nil return bRes, nil
} }

@ -90,6 +90,7 @@ func newTransfer(tr *Transfer, name string, path string) *Transfer {
Header: action.Header, Header: action.Header,
ExpiresAt: action.ExpiresAt, ExpiresAt: action.ExpiresAt,
ExpiresIn: action.ExpiresIn, ExpiresIn: action.ExpiresIn,
createdAt: action.createdAt,
} }
} }
@ -102,6 +103,7 @@ func newTransfer(tr *Transfer, name string, path string) *Transfer {
Header: link.Header, Header: link.Header,
ExpiresAt: link.ExpiresAt, ExpiresAt: link.ExpiresAt,
ExpiresIn: link.ExpiresIn, ExpiresIn: link.ExpiresIn,
createdAt: link.createdAt,
} }
} }
} }
@ -114,10 +116,12 @@ type Action struct {
Header map[string]string `json:"header,omitempty"` Header map[string]string `json:"header,omitempty"`
ExpiresAt time.Time `json:"expires_at,omitempty"` ExpiresAt time.Time `json:"expires_at,omitempty"`
ExpiresIn int `json:"expires_in,omitempty"` ExpiresIn int `json:"expires_in,omitempty"`
createdAt time.Time `json:"-"`
} }
func (a *Action) IsExpiredWithin(d time.Duration) (time.Time, bool) { 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 type ActionSet map[string]*Action