options.go

 1package auth
 2
 3import (
 4	"github.com/MichaelMure/git-bug/entity"
 5	"github.com/MichaelMure/git-bug/identity"
 6)
 7
 8type options struct {
 9	target string
10	userId entity.Id
11	kind   CredentialKind
12}
13
14type Option func(opts *options)
15
16func matcher(opts []Option) *options {
17	result := &options{}
18	for _, opt := range opts {
19		opt(result)
20	}
21	return result
22}
23
24func (opts *options) Match(cred Credential) bool {
25	if opts.target != "" && cred.Target() != opts.target {
26		return false
27	}
28
29	if opts.userId != "" && cred.UserId() != opts.userId {
30		return false
31	}
32
33	if opts.kind != "" && cred.Kind() != opts.kind {
34		return false
35	}
36
37	return true
38}
39
40func WithTarget(target string) Option {
41	return func(opts *options) {
42		opts.target = target
43	}
44}
45
46func WithUser(user identity.Interface) Option {
47	return func(opts *options) {
48		opts.userId = user.Id()
49	}
50}
51
52func WithUserId(userId entity.Id) Option {
53	return func(opts *options) {
54		opts.userId = userId
55	}
56}
57
58func WithKind(kind CredentialKind) Option {
59	return func(opts *options) {
60		opts.kind = kind
61	}
62}