fix config flag

This commit is contained in:
Henrique Dias 2017-07-29 14:57:45 +01:00
parent e09bd03eec
commit 2334bb601d
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730

View File

@ -35,7 +35,7 @@ var (
)
func init() {
flag.StringVarP(&config, "config", "c", "", "JSON configuration file")
flag.StringVarP(&config, "config", "c", "", "Configuration file")
flag.IntVarP(&port, "port", "p", 0, "HTTP Port (default is random)")
flag.StringVarP(&addr, "address", "a", "", "Address to listen to (default is all of them)")
flag.StringVarP(&database, "database", "d", "./filemanager.db", "Database file")
@ -81,7 +81,16 @@ func main() {
// Add a configuration file if set.
if config != "" {
viper.SetConfigName(strings.TrimSuffix(config, filepath.Ext(config)))
ext := filepath.Ext(config)
dir := filepath.Dir(config)
config = strings.TrimSuffix(config, ext)
if dir != "" {
viper.AddConfigPath(dir)
config = strings.TrimPrefix(config, dir)
}
viper.SetConfigName(config)
}
// Read configuration from a file if exists.