This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/connector/config_repo.go

62 lines
1.3 KiB
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package connector
import (
"encoding/json"
"io"
"os"
"github.com/coreos/dex/repo"
2015-08-18 05:57:27 +05:30
)
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}
}
2015-08-18 05:57:27 +05:30
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
}