forked from dexidp/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_repo.go
61 lines (51 loc) · 1.26 KB
/
config_repo.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
package connector
import (
"encoding/json"
"io"
"os"
"github.com/coreos/dex/repo"
)
func newConnectorConfigsFromReader(r io.Reader) ([]ConnectorConfig, error) {
var ms []map[string]interface{}
if err := json.NewDecoder(r).Decode(&ms); err != nil {
return nil, err
}
cfgs := make([]ConnectorConfig, len(ms))
for i, m := range ms {
cfg, err := newConnectorConfigFromMap(m)
if err != nil {
return nil, err
}
cfgs[i] = cfg
}
return cfgs, nil
}
func NewConnectorConfigRepoFromFile(loc string) (ConnectorConfigRepo, error) {
cf, err := os.Open(loc)
if err != nil {
return nil, err
}
defer cf.Close()
cfgs, err := newConnectorConfigsFromReader(cf)
if err != nil {
return nil, err
}
return &memConnectorConfigRepo{configs: cfgs}, nil
}
type memConnectorConfigRepo struct {
configs []ConnectorConfig
}
func NewConnectorConfigRepoFromConfigs(cfgs []ConnectorConfig) ConnectorConfigRepo {
return &memConnectorConfigRepo{configs: cfgs}
}
func (r *memConnectorConfigRepo) All() ([]ConnectorConfig, error) {
return r.configs, nil
}
func (r *memConnectorConfigRepo) GetConnectorByID(_ repo.Transaction, id string) (ConnectorConfig, error) {
for _, cfg := range r.configs {
if cfg.ConnectorID() == id {
return cfg, nil
}
}
return nil, ErrorNotFound
}