options.go

 1package auth
 2
 3type options struct {
 4	target string
 5	kind   CredentialKind
 6	meta   map[string]string
 7}
 8
 9type Option func(opts *options)
10
11func matcher(opts []Option) *options {
12	result := &options{}
13	for _, opt := range opts {
14		opt(result)
15	}
16	return result
17}
18
19func (opts *options) Match(cred Credential) bool {
20	if opts.target != "" && cred.Target() != opts.target {
21		return false
22	}
23
24	if opts.kind != "" && cred.Kind() != opts.kind {
25		return false
26	}
27
28	for key, val := range opts.meta {
29		if v, ok := cred.GetMetadata(key); !ok || v != val {
30			return false
31		}
32	}
33
34	return true
35}
36
37func WithTarget(target string) Option {
38	return func(opts *options) {
39		opts.target = target
40	}
41}
42
43func WithKind(kind CredentialKind) Option {
44	return func(opts *options) {
45		opts.kind = kind
46	}
47}
48
49func WithMeta(key string, val string) Option {
50	return func(opts *options) {
51		if opts.meta == nil {
52			opts.meta = make(map[string]string)
53		}
54		opts.meta[key] = val
55	}
56}