2019-11-27 12:09:15 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
|
|
)
|
2019-11-28 14:36:49 +01:00
|
|
|
|
2019-11-27 12:09:15 +01:00
|
|
|
// Config is the config
|
|
|
|
type Config struct {
|
2019-11-28 14:36:49 +01:00
|
|
|
HTTP HTTPConfig
|
|
|
|
LDAP LDAPConfig
|
2019-11-27 12:09:15 +01:00
|
|
|
}
|
2019-11-28 14:36:49 +01:00
|
|
|
|
2019-11-27 12:09:15 +01:00
|
|
|
// HTTPConfig is the http config
|
|
|
|
type HTTPConfig struct {
|
2019-11-28 14:36:49 +01:00
|
|
|
Address string
|
|
|
|
Secret string
|
2019-11-27 12:09:15 +01:00
|
|
|
}
|
2019-11-28 14:36:49 +01:00
|
|
|
|
2019-11-27 12:09:15 +01:00
|
|
|
// LDAPConfig is the ldap config
|
|
|
|
type LDAPConfig struct {
|
2019-11-28 14:36:49 +01:00
|
|
|
Base string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
BindDN string
|
|
|
|
BindPassword string
|
|
|
|
UserFilter string
|
|
|
|
Attributes []string
|
2019-11-27 12:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromFile retrieves the configuration from the given file
|
|
|
|
func NewFromFile(filepath string) (*Config, error) {
|
|
|
|
config := NewDefault()
|
|
|
|
cfg, err := ini.Load(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := cfg.MapTo(config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
2019-11-28 14:36:49 +01:00
|
|
|
|
2019-11-27 12:09:15 +01:00
|
|
|
// NewDefault set a default config
|
|
|
|
func NewDefault() *Config {
|
|
|
|
return &Config{
|
|
|
|
HTTP: HTTPConfig{
|
2019-11-28 14:36:49 +01:00
|
|
|
Address: ":3001",
|
2019-11-27 12:09:15 +01:00
|
|
|
},
|
|
|
|
LDAP: LDAPConfig{
|
|
|
|
Base: "dc=example,dc=com",
|
|
|
|
Host: "ldap.example.com",
|
|
|
|
Port: 389,
|
|
|
|
BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com",
|
|
|
|
BindPassword: "readonlypassword",
|
|
|
|
UserFilter: "(uid=%s)",
|
|
|
|
Attributes: []string{"givenName", "sn", "mail", "uid"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 14:36:49 +01:00
|
|
|
|
2019-11-27 12:09:15 +01:00
|
|
|
// Dump return the config dump
|
|
|
|
func Dump(config *Config, w io.Writer) error {
|
|
|
|
cfg := ini.Empty()
|
|
|
|
if err := cfg.ReflectFrom(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := cfg.WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|