Advertisement
Guest User

cfg

a guest
Aug 19th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.89 KB | None | 0 0
  1. package cfg
  2.  
  3. import (
  4.     "time"
  5.     "fmt"
  6.     "go.uber.org/zap"
  7.         "github.com/BurntSushi/toml"
  8. )
  9.  
  10. type Esme struct {
  11.         Enabled         bool
  12.         Name            string
  13.         Operator        string
  14.         Host            string
  15.         Port            int
  16.         EnquireInterval int `toml:"enquire_interval"`
  17.         SystemId        string `toml:"system_id"`
  18.         Password        string
  19.         DstNumberPrefix int    `toml:"dst_number_prefix"`
  20.         SrcAddr         string `toml:"src_addr"`
  21.         AddrTon         int    `toml:"addr_ton"`
  22.         AddrNpi         int    `toml:"addr_npi"`
  23.         SrcTon          int    `toml:"src_ton"`
  24.         SrcNpi          int    `toml:"src_npi"`
  25.         TzShift         int    `toml:"tz_shift"`
  26. }
  27.  
  28. type Database struct {
  29.         User       string
  30.         Password   string
  31.         Host       string
  32.         Name       string
  33.         StorableDb string `toml:"storable_db"`
  34. }
  35.  
  36. type Cfg struct {
  37.     Host  string
  38.         Port  string
  39.         Db    Database `toml:"database"`
  40.         Esmes map[string]Esme
  41. }
  42.  
  43. func New(path *string, logger *zap.Logger) Cfg {
  44.         var c Cfg
  45.         if _, err := toml.DecodeFile(*path, &c); err != nil {
  46.                 panic(err.Error())
  47.         }
  48.         if c.Db.User == "" || c.Db.Password == "" || c.Db.Host == "" || c.Db.StorableDb == "" || c.Port == "" || c.Host == ""  {
  49.           panic(fmt.Errorf("missing mandatory config parameters"))
  50.         }
  51.         for name, options := range c.Esmes {
  52.             if options.Host == "" || options.Port == 0 || options.SystemId == "" || options.Password == "" || options.SrcAddr == "" {
  53.             logger.Warn("Missing one or many parameters for oper", zap.String("name",name), zap.Any("time", time.Now().Format(time.RFC3339)))
  54.                 delete(c.Esmes, name)
  55.                 continue
  56.             }
  57.         }
  58.         return c
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement