-
Notifications
You must be signed in to change notification settings - Fork 2
/
credentials_set.go
108 lines (95 loc) · 2.59 KB
/
credentials_set.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package goauth
import (
"context"
"fmt"
"io/fs"
"net/http"
"sort"
"strings"
"github.com/grokify/mogo/encoding/jsonutil"
"github.com/grokify/mogo/errors/errorsutil"
)
type CredentialsSet struct {
Credentials map[string]Credentials `json:"credentials,omitempty"`
}
func ReadFileCredentialsSet(credentialsSetFilename string, inflateEndpoints bool) (*CredentialsSet, error) {
var set *CredentialsSet
if err := jsonutil.UnmarshalFile(credentialsSetFilename, &set); err != nil {
return nil, err
} else if inflateEndpoints {
err := set.Inflate()
if err != nil {
return nil, err
}
}
return set, nil
}
func (set *CredentialsSet) Get(key string) (Credentials, error) {
if creds, ok := set.Credentials[key]; ok {
return creds, nil
}
return Credentials{}, fmt.Errorf("credentials key not found (%s)", key)
}
func (set *CredentialsSet) Inflate() error {
for k, v := range set.Credentials {
err := v.Inflate()
if err != nil {
return err
}
set.Credentials[k] = v
}
return nil
}
/*
func (set *CredentialsSet) NewSimpleClient(accountKey string) (*httpsimple.SimpleClient, error) {
creds, ok := set.Credentials[accountKey]
if !ok {
return nil, fmt.Errorf("client_not_found [%s]", accountKey)
}
return creds.NewSimpleClient()
}
*/
func ReadCredentialsFromSetFile(credentialsSetFilename, accountKey string, inclAccountsOnError bool) (Credentials, error) {
set, err := ReadFileCredentialsSet(credentialsSetFilename, true)
if err != nil {
return Credentials{}, err
}
creds, err := set.Get(accountKey)
if err != nil {
if inclAccountsOnError {
return creds, errorsutil.Wrap(err,
fmt.Sprintf("validAccounts [%s]", strings.Join(set.Accounts(), ",")))
}
return creds, err
}
return creds, nil
}
/*
func ReadFileCredentialsFromCredentialsSet(credentialsSetFilename, account string) (Credentials, error) {
set, err := ReadFileCredentialsSet(credentialsSetFilename, true)
if err != nil {
return Credentials{}, err
} else {
return set.Get(account)
}
}
*/
func (set *CredentialsSet) NewClient(ctx context.Context, key string) (*http.Client, error) {
creds, ok := set.Credentials[key]
if !ok {
return nil, fmt.Errorf("E_CREDS_KEY_NOT_FOUND [%v]", key)
}
return creds.NewClient(ctx)
}
func (set *CredentialsSet) Accounts() []string { return set.Keys() }
func (set *CredentialsSet) Keys() []string {
keys := []string{}
for key := range set.Credentials {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func (set *CredentialsSet) WriteFile(filename, prefix, indent string, perm fs.FileMode) error {
return jsonutil.MarshalFile(filename, set, prefix, indent, perm)
}