config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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) MarshalText() ([]byte, error) {
  14. switch p {
  15. case ProvCommand:
  16. return []byte("command"), nil
  17. case ProvRsync:
  18. return []byte("rsync"), nil
  19. case ProvTwoStageRsync:
  20. return []byte("two-stage-rsync"), nil
  21. default:
  22. return []byte{}, errors.New("Invalid ProviderEnum value")
  23. }
  24. }
  25. func (p *ProviderEnum) UnmarshalText(text []byte) error {
  26. s := string(text)
  27. switch s {
  28. case `command`:
  29. *p = ProvCommand
  30. case `rsync`:
  31. *p = ProvRsync
  32. case `two-stage-rsync`:
  33. *p = ProvTwoStageRsync
  34. default:
  35. return errors.New("Invalid value to provierEnum")
  36. }
  37. return nil
  38. }
  39. type Config struct {
  40. Global globalConfig `toml:"global"`
  41. Manager managerConfig `toml:"manager"`
  42. Server serverConfig `toml:"server"`
  43. Mirrors []mirrorConfig `toml:"mirrors"`
  44. }
  45. type globalConfig struct {
  46. Name string `toml:"name"`
  47. LogDir string `toml:"log_dir"`
  48. MirrorDir string `toml:"mirror_dir"`
  49. Concurrent int `toml:"concurrent"`
  50. Interval int `toml:"interval"`
  51. }
  52. type managerConfig struct {
  53. APIBase string `toml:"api_base"`
  54. CACert string `toml:"ca_cert"`
  55. Token string `toml:"token"`
  56. }
  57. type serverConfig struct {
  58. Hostname string `toml:"hostname"`
  59. Addr string `toml:"listen_addr"`
  60. Port int `toml:"listen_port"`
  61. SSLCert string `toml:"ssl_cert"`
  62. SSLKey string `toml:"ssl_key"`
  63. }
  64. type mirrorConfig struct {
  65. Name string `toml:"name"`
  66. Provider ProviderEnum `toml:"provider"`
  67. Upstream string `toml:"upstream"`
  68. Interval int `toml:"interval"`
  69. MirrorDir string `toml:"mirror_dir"`
  70. LogDir string `toml:"log_dir"`
  71. Env map[string]string `toml:"env"`
  72. Command string `toml:"command"`
  73. UseIPv6 bool `toml:"use_ipv6"`
  74. ExcludeFile string `toml:"exclude_file"`
  75. Password string `toml:"password"`
  76. Stage1Profile string `toml:"stage1_profile"`
  77. }
  78. func loadConfig(cfgFile string) (*Config, error) {
  79. if _, err := os.Stat(cfgFile); err != nil {
  80. return nil, err
  81. }
  82. cfg := new(Config)
  83. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  84. logger.Error(err.Error())
  85. return nil, err
  86. }
  87. return cfg, nil
  88. }