forked from gomods/athens
/
lister.go
39 lines (35 loc) · 1021 Bytes
/
lister.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package gcp
import (
"context"
"strings"
"github.com/gomods/athens/pkg/storage"
opentracing "github.com/opentracing/opentracing-go"
)
// List implements the (./pkg/storage).Lister interface
// It returns a list of versions, if any, for a given module
func (s *Storage) List(ctx context.Context, module string) ([]string, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "storage.gcp.List")
defer sp.Finish()
paths, err := s.bucket.List(ctx, module)
if err != nil {
return nil, err
}
versions := extractVersions(paths)
if len(versions) < 1 {
return nil, storage.ErrNotFound{Module: module}
}
return versions, nil
}
func extractVersions(paths []string) []string {
versions := []string{}
for _, p := range paths {
if strings.HasSuffix(p, ".info") {
segments := strings.Split(p, "/")
// version should be last segment w/ .info suffix
last := segments[len(segments)-1]
version := strings.TrimSuffix(last, ".info")
versions = append(versions, version)
}
}
return versions
}