Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
optimize config path detect
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Aug 4, 2021
1 parent 7d8ff47 commit 9b6d031
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
38 changes: 17 additions & 21 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"time"

"github.com/spf13/viper"
Expand Down Expand Up @@ -55,13 +54,17 @@ type Config struct {
AllowRuntimeConfigure bool `yaml:"AllowRuntimeConfigure"`
}

func InitConf(specPath string) (*Config, error) {

viper.SetConfigName("cloud-torrent")
viper.AddConfigPath("/etc/cloud-torrent/")
viper.AddConfigPath("/etc/")
viper.AddConfigPath("$HOME/.cloud-torrent")
viper.AddConfigPath(".")
func InitConf(specPath *string) (*Config, error) {
if *specPath != "" {
// user specific config path
viper.SetConfigFile(*specPath)
} else {
viper.SetConfigName("cloud-torrent")
viper.AddConfigPath("/etc/cloud-torrent/")
viper.AddConfigPath("/etc/")
viper.AddConfigPath("$HOME/.cloud-torrent")
viper.AddConfigPath(".")
}

viper.SetDefault("DownloadDirectory", "./downloads")
viper.SetDefault("WatchDirectory", "./torrents")
Expand All @@ -79,24 +82,19 @@ func InitConf(specPath string) (*Config, error) {
viper.SetDefault("MaxConcurrentTask", 0)
viper.SetDefault("AllowRuntimeConfigure", true)

// user specific config path
if stat, err := os.Stat(specPath); stat != nil && err == nil {
viper.SetConfigFile(specPath)
}

configExists := true
if err := viper.ReadInConfig(); err != nil {
if strings.Contains(err.Error(), "Not Found") {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// user set a config that is not exists, will write to it later
configExists = false
if specPath == "" {
specPath = "./cloud-torrent.yaml"
}
viper.SetConfigFile(specPath)
} else {
return nil, err
}
}

*specPath = viper.ConfigFileUsed()
log.Println("[config] using config file: ", *specPath)

c := &Config{}
viper.Unmarshal(c)

Expand All @@ -109,11 +107,9 @@ func InitConf(specPath string) (*Config, error) {
viper.Set("WatchDirectory", c.WatchDirectory)
}

cf := viper.ConfigFileUsed()
log.Println("[config] selected config file: ", cf)
if !configExists || dirChanged {
c.WriteDefault()
log.Println("[config] config file updated: ", cf, "exists:", configExists, "dirchanged", dirChanged)
log.Println("[config] config file updated: ", *specPath, "exists:", configExists, "dirchanged", dirChanged)
}

return c, nil
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s *Server) Run(version string) error {

//torrent engine
s.engine = engine.New(s)
c, err := engine.InitConf(s.ConfigPath)
c, err := engine.InitConf(&s.ConfigPath)
if err != nil {
return err
}
Expand Down

0 comments on commit 9b6d031

Please sign in to comment.