config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package file
  2. import (
  3. "errors"
  4. "time"
  5. xtime "go-common/library/time"
  6. "github.com/BurntSushi/toml"
  7. )
  8. type Config struct {
  9. Paths []string `toml:"paths"`
  10. Symlinks bool `toml:"symlinks"`
  11. AppId string `toml:"appId"`
  12. LogId string `toml:"logId"`
  13. ConfigPath string `toml:"-"`
  14. MetaPath string `toml:"-"`
  15. ID string `toml:"-"`
  16. ReadFrom string `toml:"readFrom"`
  17. MaxLength int `toml:"maxLength"`
  18. IgnoreOlder xtime.Duration `toml:"ignoreOlder"`
  19. CleanFilesOlder xtime.Duration `toml:"cleanFilesOlder"`
  20. ScanFrequency xtime.Duration `toml:"scanFrequency"`
  21. CleanInactive xtime.Duration `toml:"cleanInactive"`
  22. HarvesterTTL xtime.Duration `toml:"harvesterTTL"` // harvester will stop itself if inactive longer than HarvesterTTL
  23. Multiline *MultilineConf `toml:"multiline"`
  24. Timeout xtime.Duration `toml:"timeout"`
  25. Fields map[string]interface{} `toml:"fields"`
  26. }
  27. func (c *Config) ConfigValidate() (error) {
  28. if c == nil {
  29. return errors.New("config of file Input is nil")
  30. }
  31. if len(c.Paths) == 0 {
  32. return errors.New("paths of file Input can't be nil")
  33. }
  34. if c.LogId == "" {
  35. c.LogId = "000161"
  36. }
  37. if c.AppId == "" {
  38. return errors.New("appId of file Input can't be nil")
  39. }
  40. if c.IgnoreOlder == 0 {
  41. c.IgnoreOlder = xtime.Duration(time.Hour * 24)
  42. }
  43. if c.ScanFrequency == 0 {
  44. c.ScanFrequency = xtime.Duration(time.Second * 10)
  45. }
  46. // Note: CleanInactive should be greater chan ignore_older + scan_frequency
  47. if c.CleanInactive == 0 {
  48. c.CleanInactive = xtime.Duration(time.Hour * 24 * 7)
  49. }
  50. if c.CleanInactive < c.IgnoreOlder+c.ScanFrequency {
  51. return errors.New("CleanInactive must be greater than ScanFrequency + IgnoreOlder")
  52. }
  53. if c.HarvesterTTL == 0 {
  54. c.HarvesterTTL = xtime.Duration(time.Hour * 1)
  55. }
  56. if c.Timeout == 0 {
  57. c.Timeout = xtime.Duration(time.Second * 5)
  58. }
  59. if c.ReadFrom != "" && c.ReadFrom != "newest" && c.ReadFrom != "oldest" {
  60. return errors.New("ReadFrom of file input can only be newest or oldest")
  61. }
  62. if c.ReadFrom == "" {
  63. c.ReadFrom = "newest"
  64. }
  65. if c.MaxLength == 0 || c.MaxLength > 1024*10*64 {
  66. c.MaxLength = 1024 * 10 * 64
  67. }
  68. // Symlinks is always disabled
  69. c.Symlinks = false
  70. if c.Multiline != nil {
  71. if err := c.Multiline.ConfigValidate(); err != nil {
  72. return err
  73. }
  74. }
  75. return nil
  76. }
  77. func DecodeConfig(md toml.MetaData, primValue toml.Primitive) (c interface{}, err error) {
  78. c = new(Config)
  79. if err = md.PrimitiveDecode(primValue, c); err != nil {
  80. return nil, err
  81. }
  82. return c, nil
  83. }