/
resolver.go
65 lines (60 loc) · 1.45 KB
/
resolver.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package common
import (
"github.com/docker/swarmkit/api"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
// Resolver provides ID to Name resolution.
type Resolver struct {
cmd *cobra.Command
c api.ControlClient
ctx context.Context
cache map[string]string
}
// NewResolver creates a new Resolver.
func NewResolver(cmd *cobra.Command, c api.ControlClient) *Resolver {
return &Resolver{
cmd: cmd,
c: c,
ctx: Context(cmd),
cache: make(map[string]string),
}
}
func (r *Resolver) get(t interface{}, id string) string {
switch t.(type) {
case api.Node:
res, err := r.c.GetNode(r.ctx, &api.GetNodeRequest{NodeID: id})
if err != nil {
return id
}
if res.Node.Spec.Annotations.Name != "" {
return res.Node.Spec.Annotations.Name
}
if res.Node.Description == nil {
return id
}
return res.Node.Description.Hostname
case api.Service:
res, err := r.c.GetService(r.ctx, &api.GetServiceRequest{ServiceID: id})
if err != nil {
return id
}
return res.Service.Spec.Annotations.Name
default:
return id
}
}
// Resolve will attempt to resolve an ID to a Name by querying the manager.
// Results are stored into a cache.
// If the `-n` flag is used in the command-line, resolution is disabled.
func (r *Resolver) Resolve(t interface{}, id string) string {
if r.cmd.Flags().Changed("no-resolve") {
return id
}
if name, ok := r.cache[id]; ok {
return name
}
name := r.get(t, id)
r.cache[id] = name
return name
}