config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // used to connect to worker
  24. CACert string `toml:"ca_cert"`
  25. }
  26. func loadConfig(cfgFile string, c *cli.Context) (*Config, error) {
  27. cfg := new(Config)
  28. cfg.Server.Addr = "127.0.0.1"
  29. cfg.Server.Port = 14242
  30. cfg.Debug = false
  31. cfg.Files.StatusFile = "/var/lib/tunasync/tunasync.json"
  32. cfg.Files.DBFile = "/var/lib/tunasync/tunasync.db"
  33. if cfgFile != "" {
  34. if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
  35. logger.Error(err.Error())
  36. return nil, err
  37. }
  38. }
  39. if c.String("addr") != "" {
  40. cfg.Server.Addr = c.String("addr")
  41. }
  42. if c.Int("port") > 0 {
  43. cfg.Server.Port = c.Int("port")
  44. }
  45. if c.String("cert") != "" && c.String("key") != "" {
  46. cfg.Server.SSLCert = c.String("cert")
  47. cfg.Server.SSLKey = c.String("key")
  48. }
  49. if c.String("status-file") != "" {
  50. cfg.Files.StatusFile = c.String("status-file")
  51. }
  52. if c.String("db-file") != "" {
  53. cfg.Files.DBFile = c.String("db-file")
  54. }
  55. return cfg, nil
  56. }