2
0

config.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package manager
  2. import (
  3. "github.com/BurntSushi/toml"
  4. "github.com/codegangsta/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. func loadConfig(cfgFile string, c *cli.Context) (*Config, error) {
  28. cfg := new(Config)
  29. cfg.Server.Addr = "127.0.0.1"
  30. cfg.Server.Port = 14242
  31. cfg.Debug = false
  32. cfg.Files.StatusFile = "/var/lib/tunasync/tunasync.json"
  33. cfg.Files.DBFile = "/var/lib/tunasync/tunasync.db"
  34. cfg.Files.DBType = "bolt"
  35. if cfgFile != "" {
  36. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  37. logger.Error(err.Error())
  38. return nil, err
  39. }
  40. }
  41. if c.String("addr") != "" {
  42. cfg.Server.Addr = c.String("addr")
  43. }
  44. if c.Int("port") > 0 {
  45. cfg.Server.Port = c.Int("port")
  46. }
  47. if c.String("cert") != "" && c.String("key") != "" {
  48. cfg.Server.SSLCert = c.String("cert")
  49. cfg.Server.SSLKey = c.String("key")
  50. }
  51. if c.String("status-file") != "" {
  52. cfg.Files.StatusFile = c.String("status-file")
  53. }
  54. if c.String("db-file") != "" {
  55. cfg.Files.DBFile = c.String("db-file")
  56. }
  57. if c.String("db-type") != "" {
  58. cfg.Files.DBFile = c.String("db-type")
  59. }
  60. return cfg, nil
  61. }