-
Notifications
You must be signed in to change notification settings - Fork 6
/
objectstorage.go
42 lines (36 loc) · 1.16 KB
/
objectstorage.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
40
41
42
package resolver
import (
"context"
internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)
// CachingObjectStorage implements resolver for ObjectStorages, caching the results
type CachingObjectStorage struct{}
// make sure we implement the ResolutionProvider interface
var _ ResolutionProvider = CachingObjectStorage{}
// Get implements ResolutionProvider.Get
func (s CachingObjectStorage) Get(ctx context.Context, svc internal.AllServices) (Resolver, error) {
objectstorages, err := svc.GetManagedObjectStorages(ctx, &request.GetManagedObjectStoragesRequest{})
if err != nil {
return nil, err
}
return func(arg string) (uuid string, err error) {
rv := ""
for _, objsto := range objectstorages {
if MatchArgWithWhitespace(arg, objsto.Name) || objsto.UUID == arg {
if rv != "" {
return "", AmbiguousResolutionError(arg)
}
rv = objsto.UUID
}
}
if rv != "" {
return rv, nil
}
return "", NotFoundError(arg)
}, nil
}
// PositionalArgumentHelp implements resolver.ResolutionProvider
func (s CachingObjectStorage) PositionalArgumentHelp() string {
return helpUUIDTitle
}