config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package worker
  2. import (
  3. "errors"
  4. "os"
  5. "github.com/BurntSushi/toml"
  6. )
  7. type ProviderEnum uint8
  8. const (
  9. ProvRsync ProviderEnum = iota
  10. ProvTwoStageRsync
  11. ProvCommand
  12. )
  13. func (p *ProviderEnum) UnmarshalText(text []byte) error {
  14. s := string(text)
  15. switch s {
  16. case `command`:
  17. *p = ProvCommand
  18. case `rsync`:
  19. *p = ProvRsync
  20. case `two-stage-rsync`:
  21. *p = ProvTwoStageRsync
  22. default:
  23. return errors.New("Invalid value to provierEnum")
  24. }
  25. return nil
  26. }
  27. type Config struct {
  28. Global globalConfig `toml:"global"`
  29. Manager managerConfig `toml:"manager"`
  30. Server serverConfig `toml:"server"`
  31. Cgroup cgroupConfig `toml:"cgroup"`
  32. Mirrors []mirrorConfig `toml:"mirrors"`
  33. }
  34. type globalConfig struct {
  35. Name string `toml:"name"`
  36. LogDir string `toml:"log_dir"`
  37. MirrorDir string `toml:"mirror_dir"`
  38. Concurrent int `toml:"concurrent"`
  39. Interval int `toml:"interval"`
  40. }
  41. type managerConfig struct {
  42. APIBase string `toml:"api_base"`
  43. CACert string `toml:"ca_cert"`
  44. Token string `toml:"token"`
  45. }
  46. type serverConfig struct {
  47. Hostname string `toml:"hostname"`
  48. Addr string `toml:"listen_addr"`
  49. Port int `toml:"listen_port"`
  50. SSLCert string `toml:"ssl_cert"`
  51. SSLKey string `toml:"ssl_key"`
  52. }
  53. type cgroupConfig struct {
  54. Enable bool `toml:"enable"`
  55. BasePath string `toml:"base_path"`
  56. Group string `toml:"group"`
  57. }
  58. type mirrorConfig struct {
  59. Name string `toml:"name"`
  60. Provider ProviderEnum `toml:"provider"`
  61. Upstream string `toml:"upstream"`
  62. Interval int `toml:"interval"`
  63. MirrorDir string `toml:"mirror_dir"`
  64. LogDir string `toml:"log_dir"`
  65. Env map[string]string `toml:"env"`
  66. Command string `toml:"command"`
  67. UseIPv6 bool `toml:"use_ipv6"`
  68. ExcludeFile string `toml:"exclude_file"`
  69. Password string `toml:"password"`
  70. Stage1Profile string `toml:"stage1_profile"`
  71. }
  72. // LoadConfig loads configuration
  73. func LoadConfig(cfgFile string) (*Config, error) {
  74. if _, err := os.Stat(cfgFile); err != nil {
  75. return nil, err
  76. }
  77. cfg := new(Config)
  78. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  79. logger.Error(err.Error())
  80. return nil, err
  81. }
  82. return cfg, nil
  83. }