config.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package manager
  2. import (
  3. "github.com/BurntSushi/toml"
  4. "github.com/urfave/cli"
  5. )
  6. // A Config is the top-level toml-serializaible config struct
  7. type Config struct {
  8. Debug bool `toml:"debug"`
  9. Server ServerConfig `toml:"server"`
  10. Files FileConfig `toml:"files"`
  11. }
  12. // A ServerConfig represents the configuration for HTTP server
  13. type ServerConfig struct {
  14. Addr string `toml:"addr"`
  15. Port int `toml:"port"`
  16. SSLCert string `toml:"ssl_cert"`
  17. SSLKey string `toml:"ssl_key"`
  18. }
  19. // A FileConfig contains paths to special files
  20. type FileConfig struct {
  21. StatusFile string `toml:"status_file"`
  22. DBFile string `toml:"db_file"`
  23. DBType string `toml:"db_type"`
  24. // used to connect to worker
  25. CACert string `toml:"ca_cert"`
  26. }
  27. // LoadConfig loads config from specified file
  28. func LoadConfig(cfgFile string, c *cli.Context) (*Config, error) {
  29. cfg := new(Config)
  30. cfg.Server.Addr = "127.0.0.1"
  31. cfg.Server.Port = 14242
  32. cfg.Debug = false
  33. cfg.Files.StatusFile = "/var/lib/tunasync/tunasync.json"
  34. cfg.Files.DBFile = "/var/lib/tunasync/tunasync.db"
  35. cfg.Files.DBType = "bolt"
  36. if cfgFile != "" {
  37. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  38. logger.Errorf(err.Error())
  39. return nil, err
  40. }
  41. }
  42. if c == nil {
  43. return cfg, nil
  44. }
  45. if c.String("addr") != "" {
  46. cfg.Server.Addr = c.String("addr")
  47. }
  48. if c.Int("port") > 0 {
  49. cfg.Server.Port = c.Int("port")
  50. }
  51. if c.String("cert") != "" && c.String("key") != "" {
  52. cfg.Server.SSLCert = c.String("cert")
  53. cfg.Server.SSLKey = c.String("key")
  54. }
  55. if c.String("status-file") != "" {
  56. cfg.Files.StatusFile = c.String("status-file")
  57. }
  58. if c.String("db-file") != "" {
  59. cfg.Files.DBFile = c.String("db-file")
  60. }
  61. if c.String("db-type") != "" {
  62. cfg.Files.DBFile = c.String("db-type")
  63. }
  64. return cfg, nil
  65. }