config.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Mirrors []mirrorConfig `toml:"mirrors"`
  32. }
  33. type globalConfig struct {
  34. Name string `toml:"name"`
  35. LogDir string `toml:"log_dir"`
  36. MirrorDir string `toml:"mirror_dir"`
  37. Concurrent int `toml:"concurrent"`
  38. Interval int `toml:"interval"`
  39. }
  40. type managerConfig struct {
  41. APIBase string `toml:"api_base"`
  42. CACert string `toml:"ca_cert"`
  43. Token string `toml:"token"`
  44. }
  45. type serverConfig struct {
  46. Hostname string `toml:"hostname"`
  47. Addr string `toml:"listen_addr"`
  48. Port int `toml:"listen_port"`
  49. SSLCert string `toml:"ssl_cert"`
  50. SSLKey string `toml:"ssl_key"`
  51. }
  52. type mirrorConfig struct {
  53. Name string `toml:"name"`
  54. Provider ProviderEnum `toml:"provider"`
  55. Upstream string `toml:"upstream"`
  56. Interval int `toml:"interval"`
  57. MirrorDir string `toml:"mirror_dir"`
  58. LogDir string `toml:"log_dir"`
  59. Env map[string]string `toml:"env"`
  60. Command string `toml:"command"`
  61. UseIPv6 bool `toml:"use_ipv6"`
  62. ExcludeFile string `toml:"exclude_file"`
  63. Password string `toml:"password"`
  64. Stage1Profile string `toml:"stage1_profile"`
  65. }
  66. func loadConfig(cfgFile string) (*Config, error) {
  67. if _, err := os.Stat(cfgFile); err != nil {
  68. return nil, err
  69. }
  70. cfg := new(Config)
  71. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  72. logger.Error(err.Error())
  73. return nil, err
  74. }
  75. return cfg, nil
  76. }