config.go 1.5 KB

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