LoadConfig loads config from specified file
(cfgFile string, c *cli.Context)
| 31 | |
| 32 | // LoadConfig loads config from specified file |
| 33 | func LoadConfig(cfgFile string, c *cli.Context) (*Config, error) { |
| 34 | |
| 35 | cfg := new(Config) |
| 36 | cfg.Server.Addr = "127.0.0.1" |
| 37 | cfg.Server.Port = 14242 |
| 38 | cfg.Debug = false |
| 39 | cfg.Files.StatusFile = "/var/lib/tunasync/tunasync.json" |
| 40 | cfg.Files.DBFile = "/var/lib/tunasync/tunasync.db" |
| 41 | cfg.Files.DBType = "bolt" |
| 42 | |
| 43 | if cfgFile != "" { |
| 44 | if _, err := toml.DecodeFile(cfgFile, cfg); err != nil { |
| 45 | logger.Error(err.Error()) |
| 46 | return nil, err |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if c == nil { |
| 51 | return cfg, nil |
| 52 | } |
| 53 | |
| 54 | if c.String("addr") != "" { |
| 55 | cfg.Server.Addr = c.String("addr") |
| 56 | } |
| 57 | if c.Int("port") > 0 { |
| 58 | cfg.Server.Port = c.Int("port") |
| 59 | } |
| 60 | if c.String("cert") != "" && c.String("key") != "" { |
| 61 | cfg.Server.SSLCert = c.String("cert") |
| 62 | cfg.Server.SSLKey = c.String("key") |
| 63 | } |
| 64 | if c.String("status-file") != "" { |
| 65 | cfg.Files.StatusFile = c.String("status-file") |
| 66 | } |
| 67 | if c.String("db-file") != "" { |
| 68 | cfg.Files.DBFile = c.String("db-file") |
| 69 | } |
| 70 | if c.String("db-type") != "" { |
| 71 | cfg.Files.DBType = c.String("db-type") |
| 72 | } |
| 73 | |
| 74 | return cfg, nil |
| 75 | } |