2
0

config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Role string `toml:"role"`
  67. ExecOnSuccess string `toml:"exec_on_success"`
  68. ExecOnFailure string `toml:"exec_on_failure"`
  69. Command string `toml:"command"`
  70. UseIPv6 bool `toml:"use_ipv6"`
  71. ExcludeFile string `toml:"exclude_file"`
  72. Password string `toml:"password"`
  73. Stage1Profile string `toml:"stage1_profile"`
  74. }
  75. // LoadConfig loads configuration
  76. func LoadConfig(cfgFile string) (*Config, error) {
  77. if _, err := os.Stat(cfgFile); err != nil {
  78. return nil, err
  79. }
  80. cfg := new(Config)
  81. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  82. logger.Errorf(err.Error())
  83. return nil, err
  84. }
  85. return cfg, nil
  86. }