config.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. 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.Errorf(err.Error())
  38. return nil, err
  39. }
  40. }
  41. if c == nil {
  42. return cfg, nil
  43. }
  44. if c.String("addr") != "" {
  45. cfg.Server.Addr = c.String("addr")
  46. }
  47. if c.Int("port") > 0 {
  48. cfg.Server.Port = c.Int("port")
  49. }
  50. if c.String("cert") != "" && c.String("key") != "" {
  51. cfg.Server.SSLCert = c.String("cert")
  52. cfg.Server.SSLKey = c.String("key")
  53. }
  54. if c.String("status-file") != "" {
  55. cfg.Files.StatusFile = c.String("status-file")
  56. }
  57. if c.String("db-file") != "" {
  58. cfg.Files.DBFile = c.String("db-file")
  59. }
  60. if c.String("db-type") != "" {
  61. cfg.Files.DBFile = c.String("db-type")
  62. }
  63. return cfg, nil
  64. }