-
Notifications
You must be signed in to change notification settings - Fork 7
/
gateway.go
41 lines (35 loc) · 1012 Bytes
/
gateway.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
package resolver
import (
"context"
internal "github.com/UpCloudLtd/upcloud-cli/v3/internal/service"
)
// CachingGatewayimplements resolver for gateways, caching the results
type CachingGateway struct{}
// make sure we implement the ResolutionProvider interface
var _ ResolutionProvider = CachingGateway{}
// Get implements ResolutionProvider.Get
func (s CachingGateway) Get(ctx context.Context, svc internal.AllServices) (Resolver, error) {
gateways, err := svc.GetGateways(ctx)
if err != nil {
return nil, err
}
return func(arg string) (uuid string, err error) {
rv := ""
for _, gtw := range gateways {
if MatchArgWithWhitespace(arg, gtw.Name) || gtw.UUID == arg {
if rv != "" {
return "", AmbiguousResolutionError(arg)
}
rv = gtw.UUID
}
}
if rv != "" {
return rv, nil
}
return "", NotFoundError(arg)
}, nil
}
// PositionalArgumentHelp implements resolver.ResolutionProvider
func (s CachingGateway) PositionalArgumentHelp() string {
return helpUUIDTitle
}