-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
54 lines (46 loc) · 1.66 KB
/
local.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
package credentials
import "fmt"
// LocalProviderType is the type of Local.
const LocalProviderType ProviderType = "local"
// Local is a collection of locally stored credentials.
type Local struct {
credentials map[string]*Credentials
}
// NewLocal returns a new instance of Local initialized with @credentials.
func NewLocal(credentials map[string]*Credentials) *Local {
localCred := new(Local)
localCred.credentials = make(map[string]*Credentials)
for k, v := range credentials {
localCred.credentials[k] = v
}
return localCred
}
// LocalFactory is the ProviderFactory for Local. It accepts the same arguments as NewLocal.
func LocalFactory(args ...interface{}) (Provider, error) {
if args == nil {
return NewLocal(nil), nil
}
credentials, ok := args[0].(map[string]*Credentials)
if !ok {
return nil, fmt.Errorf("wrong input type for local factory: expected: %T, actual: %T", map[string]Credentials{}, args[0])
}
return NewLocal(credentials), nil
}
// Type returns the ProviderType of Local.
func (local Local) Type() ProviderType {
return LocalProviderType
}
// GetCredentials returns the credentials, stored in @local, identified by @credID.
// If no credentials are stored under the provided @credID, it returns an error.
func (local Local) GetCredentials(credID string) (cred *Credentials, err error) {
cred, ok := local.credentials[credID]
if !ok {
err = fmt.Errorf("no credentials found for ID: %s", credID)
}
return
}
// SetCredentials stores @cred in @local under @credID.
// It silently overwrites any credentials previously stored under @credID.
func (local *Local) SetCredentials(credID string, cred *Credentials) {
local.credentials[credID] = cred
}