package main import ( "errors" "io/ioutil" "path" "strings" "time" "dim13.org/acme" "gopkg.in/yaml.v2" ) const defKeySize = 2048 type Config struct { Defaults defaults Provider map[string]*provider Account map[string]*account Hook map[string]*hook Desire map[string]*desire } type defaults struct { Gracetime Listen string ListenTLS string Provider string Account string Basedir string KeySize int } type provider struct { Directory string *acme.Provider } type account struct { Mail string Phone string KeySize int Key string registered bool *acme.Account acme.Contacts } type hook struct { CMD string } type desire struct { Provider string Account string Altnames []string KeySize int Key string Cert string Webroot string Hooks []string provider *provider account *account *acme.Desire } var ( errNoProvider = errors.New("no provider specified") errNoAccount = errors.New("no account specified") errNoKey = errors.New("no key specified") errNoCert = errors.New("no cert specified") errNoAltNames = errors.New("no altnames specified") errNoMail = errors.New("no mail specified") ) func LoadConfig(fname string) (*Config, error) { c := &Config{} conf, err := ioutil.ReadFile(fname) if err != nil { return nil, err } err = yaml.Unmarshal(conf, c) if err != nil { return nil, err } // apply defaults if c.Defaults.KeySize == 0 { c.Defaults.KeySize = defKeySize } for k, v := range c.Account { if v.KeySize == 0 { v.KeySize = c.Defaults.KeySize } if v.Mail == "" { return nil, errNoMail } if v.Key == "" { return nil, errNoKey } if c.Defaults.Basedir != "" { v.Key = path.Join(c.Defaults.Basedir, v.Key) } c.Account[k] = v } for k, v := range c.Desire { if v.Provider == "" { if c.Defaults.Provider != "" { v.Provider = c.Defaults.Provider } else { return nil, errNoProvider } } v.provider = c.Provider[v.Provider] if v.Account == "" { if c.Defaults.Account != "" { v.Account = c.Defaults.Account } else { return nil, errNoAccount } } v.account = c.Account[v.Account] if v.KeySize == 0 { v.KeySize = c.Defaults.KeySize } if v.Key == "" { return nil, errNoKey } if v.Cert == "" { return nil, errNoCert } if c.Defaults.Basedir != "" { v.Key = path.Join(c.Defaults.Basedir, v.Key) v.Cert = path.Join(c.Defaults.Basedir, v.Cert) } switch len(v.Altnames) { case 0: return nil, errNoAltNames case 1: an := v.Altnames[0] if strings.HasPrefix(an, "www.") { v.Altnames = append(v.Altnames, an[4:]) } } c.Desire[k] = v } return c, nil } type Gracetime struct{ time.Duration } func (g *Gracetime) UnmarshalText(s []byte) error { var err error g.Duration, err = time.ParseDuration(string(s)) return err }