Skip to content

Commit b620733

Browse files
committed
allow returning the latest version from the cached versions as the /@latest version in offline mode.
1 parent 24895f2 commit b620733

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

‎pkg/download/protocol.go‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package download
22

33
import (
44
"context"
5+
"encoding/json"
56
"regexp"
67
"strings"
78
"sync"
@@ -184,9 +185,34 @@ func (p *protocol) Latest(ctx context.Context, mod string) (*storage.RevInfo, er
184185
ctx, span := observ.StartSpan(ctx, op.String())
185186
defer span.End()
186187
if p.networkMode == Offline {
187-
// Go never pings the /@latest endpoint _first_. It always tries /list and if that
188-
// endpoint returns an empty list then it fallsback to calling /@latest.
189-
return nil, errors.E(op, "Athens is in offline mode, use /list endpoint", errors.KindNotFound)
188+
//Go pkgsite will call /@latest endpoint
189+
//when Athens is in offline mode, we return the latest version from the cached versions as the /@latest version in offline mode."
190+
lr, err := p.storage.List(ctx, mod)
191+
if err != nil {
192+
return nil, errors.E(op, err)
193+
}
194+
var latest *storage.RevInfo
195+
for _, v := range lr {
196+
var rev storage.RevInfo
197+
info, err := p.storage.Info(ctx, mod, v)
198+
if err != nil {
199+
continue
200+
}
201+
err = json.Unmarshal(info, &rev)
202+
if err != nil {
203+
continue
204+
}
205+
if latest == nil || rev.Time.After(latest.Time) {
206+
latest = &rev
207+
}
208+
}
209+
if latest == nil {
210+
return nil, errors.E(op, errors.KindNotFound, errors.M(mod))
211+
}
212+
return &storage.RevInfo{
213+
Version: latest.Version,
214+
Time: latest.Time,
215+
}, nil
190216
}
191217
lr, _, err := p.lister.List(ctx, mod)
192218
if err != nil {

0 commit comments

Comments
 (0)