config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. toml "github.com/pelletier/go-toml"
  7. )
  8. type Validation struct {
  9. Param string
  10. CmdFlag string
  11. ConfFlag string
  12. }
  13. type ServerConfig struct {
  14. Address string `toml:address,omitempty`
  15. Port int64 `toml:port,omitempty`
  16. Encryption bool `toml:encryption,omitempty`
  17. User string `toml:user,omitempty`
  18. Password string `toml:password,omitempty`
  19. }
  20. func (s ServerConfig) String() string {
  21. return fmt.Sprintf(
  22. "\tAddress: %s\n\tPort: %d\n\tEncryption: %t\n\tUser: %s\n\tPassword: %s\n",
  23. s.Address,
  24. s.Port,
  25. s.Encryption,
  26. s.User,
  27. s.Password,
  28. )
  29. }
  30. type Config struct {
  31. Server *ServerConfig `toml:server,omitempty`
  32. From string `toml:from,omitempty`
  33. To []string `toml:to,omitempty`
  34. Cc []string `toml:cc,omitempty`
  35. Bcc []string `toml:bcc,omitempty`
  36. Subject string `toml:subject,omitempty`
  37. Text string `toml:text,omitempty`
  38. }
  39. func (c Config) String() string {
  40. return fmt.Sprintf(
  41. "From: %s\nTo: %s\nCc: %s\nBcc: %s\nSubject: %s\nText:\n%s\nServer:\n%s",
  42. c.From,
  43. c.To,
  44. c.Cc,
  45. c.Bcc,
  46. c.Subject,
  47. c.Text,
  48. c.Server,
  49. )
  50. }
  51. func NewConfig() *Config {
  52. server := &ServerConfig{}
  53. config := &Config{}
  54. config.Server = server
  55. return config
  56. }
  57. func (s *ServerConfig) Merge(key string, value interface{}) {
  58. Merge(key, value, s)
  59. }
  60. func (c *Config) Merge(key string, value interface{}) {
  61. Merge(key, value, c)
  62. }
  63. func readConfig(configPath, section string) *Config {
  64. config := NewConfig()
  65. tree, err := toml.LoadFile(configPath)
  66. if err != nil {
  67. Error.F("Error in parsing the configuration\n%s", err)
  68. os.Exit(2)
  69. }
  70. keys := tree.Keys()
  71. if !IsPresent(keys, section) {
  72. return config
  73. }
  74. sub := tree.Get(section).(*toml.Tree)
  75. err = sub.Unmarshal(config)
  76. return config
  77. }
  78. func checkValidity(validations *[]Validation, obj interface{}, name, cmd, param string) {
  79. if IsEmpty(obj) {
  80. *validations = append(*validations, Validation{name, cmd, param})
  81. }
  82. }
  83. func (c *Config) Validate() error {
  84. var validations = []Validation{}
  85. var msg string
  86. checkValidity(&validations, c.Server.Address, "server address", "server-address", "server.address")
  87. checkValidity(&validations, c.Server.Port, "server port", "server-port", "server.port")
  88. if !c.Server.Encryption {
  89. Warning.Ln("Warning: not using encryption!")
  90. }
  91. checkValidity(&validations, c.Server.User, "username", "user", "server.user")
  92. checkValidity(&validations, c.Server.Password, "password", "password", "server.password")
  93. checkValidity(&validations, c.From, "from", "from", "from")
  94. checkValidity(&validations, c.To, "to", "to", "to")
  95. checkValidity(&validations, c.Subject, "subject", "sub", "subject")
  96. checkValidity(&validations, c.Text, "body", "", "text")
  97. Debug.F("Lengths:\n\tTo: %d\n\tCc: %d\n\tBcc: %d", len(c.To), len(c.Cc), len(c.Bcc))
  98. if len(validations) > 0 {
  99. for _, v := range validations {
  100. if v.CmdFlag == "" {
  101. msg += fmt.Sprintf("%s: pass a value either via stdin or in configuration file section (%s)\n", v.Param, v.ConfFlag)
  102. } else {
  103. msg += fmt.Sprintf("%s: pass a value either via command line (-%s) or in configuration file section (%s)\n", v.Param, v.CmdFlag, v.ConfFlag)
  104. }
  105. }
  106. return errors.New(msg)
  107. }
  108. return nil
  109. }