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

Commit

Permalink
saved config will now keep keys cases
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Aug 4, 2021
1 parent 1bc69e1 commit 5302556
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
68 changes: 37 additions & 31 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/spf13/viper"
"golang.org/x/time/rate"
"gopkg.in/yaml.v2"
)

const (
Expand All @@ -26,32 +27,32 @@ const (
)

type Config struct {
AutoStart bool
EngineDebug bool
MuteEngineLog bool
ObfsPreferred bool
ObfsRequirePreferred bool
DisableTrackers bool
DisableIPv6 bool
NoDefaultPortForwarding bool
DisableUTP bool
DownloadDirectory string
WatchDirectory string
EnableUpload bool
EnableSeeding bool
IncomingPort int
DoneCmd string
SeedRatio float32
SeedTime time.Duration
UploadRate string
DownloadRate string
TrackerList string
AlwaysAddTrackers bool
ProxyURL string
RssURL string
ScraperURL string
MaxConcurrentTask int
AllowRuntimeConfigure bool
AutoStart bool `yaml:"AutoStart"`
EngineDebug bool `yaml:"EngineDebug"`
MuteEngineLog bool `yaml:"MuteEngineLog"`
ObfsPreferred bool `yaml:"ObfsPreferred"`
ObfsRequirePreferred bool `yaml:"ObfsRequirePreferred"`
DisableTrackers bool `yaml:"DisableTrackers"`
DisableIPv6 bool `yaml:"DisableIPv6"`
NoDefaultPortForwarding bool `yaml:"NoDefaultPortForwarding"`
DisableUTP bool `yaml:"DisableUTP"`
DownloadDirectory string `yaml:"DownloadDirectory"`
WatchDirectory string `yaml:"WatchDirectory"`
EnableUpload bool `yaml:"EnableUpload"`
EnableSeeding bool `yaml:"EnableSeeding"`
IncomingPort int `yaml:"IncomingPort"`
DoneCmd string `yaml:"DoneCmd"`
SeedRatio float32 `yaml:"SeedRatio"`
SeedTime time.Duration `yaml:"SeedTime"`
UploadRate string `yaml:"UploadRate"`
DownloadRate string `yaml:"DownloadRate"`
TrackerList string `yaml:"TrackerList"`
AlwaysAddTrackers bool `yaml:"AlwaysAddTrackers"`
ProxyURL string `yaml:"ProxyURL"`
RssURL string `yaml:"RssURL"`
ScraperURL string `yaml:"ScraperURL"`
MaxConcurrentTask int `yaml:"MaxConcurrentTask"`
AllowRuntimeConfigure bool `yaml:"AllowRuntimeConfigure"`
}

func InitConf(specPath string) (*Config, error) {
Expand Down Expand Up @@ -111,9 +112,7 @@ func InitConf(specPath string) (*Config, error) {
cf := viper.ConfigFileUsed()
log.Println("[config] selected config file: ", cf)
if !configExists || dirChanged {
if err := viper.WriteConfig(); err != nil {
return nil, err
}
c.WriteYaml()
log.Println("[config] config file written: ", cf, "exists:", configExists, "dirchanged", dirChanged)
}

Expand Down Expand Up @@ -207,7 +206,7 @@ func (c *Config) Validate(nc *Config) uint8 {
return status
}

func (c *Config) SyncViper(nc Config) error {
func (c *Config) SyncViper(nc Config) {
cv := reflect.ValueOf(*c)
nv := reflect.ValueOf(nc)
typeOfC := cv.Type()
Expand All @@ -220,8 +219,15 @@ func (c *Config) SyncViper(nc Config) error {
log.Println("config updated ", name, ": ", oval, " -> ", val)
}
}
}

return viper.WriteConfig()
func (c *Config) WriteYaml() error {
cf := viper.ConfigFileUsed()
d, err := yaml.Marshal(c)
if err != nil {
return err
}
return os.WriteFile(cf, d, 0666)
}

func (c *Config) GetCmdConfig() (string, []string, error) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace (
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Server struct {
UnixPerm string `opts:"help=DomainSocket file permission (default 0666),env=UNIXPERM"`
Auth string `opts:"help=Optional basic auth in form 'user:password',env=AUTH"`
ProxyURL string `opts:"help=Proxy url,env=PROXY_URL"`
ConfigPath string `opts:"help=Configuration file path (default /etc/cloud-torrent.yaml)"`
ConfigPath string `opts:"help=Configuration file path (default ./cloud-torrent.yaml)"`
KeyPath string `opts:"help=TLS Key file path"`
CertPath string `opts:"help=TLS Certicate file path,short=r"`
RestAPI string `opts:"help=Listen on a trusted port accepts /api/ requests (eg. localhost:3001),env=RESTAPI"`
Expand Down
5 changes: 3 additions & 2 deletions server/server_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,11 @@ func (s *Server) apiConfigure(data []byte) error {
}

// now it's safe to save the configure
if err := s.engineConfig.SyncViper(c); err != nil {
s.engineConfig.SyncViper(c)
s.engineConfig = &c
if err := s.engineConfig.WriteYaml(); err != nil {
return err
}
s.engineConfig = &c
log.Printf("[api] config saved")

// finally to reconfigure the engine
Expand Down

0 comments on commit 5302556

Please sign in to comment.