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

Commit

Permalink
norm dir logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Jan 7, 2020
1 parent d8546ed commit dec2efd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
29 changes: 29 additions & 0 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package engine

import (
"errors"
"fmt"
"log"
"path/filepath"
"reflect"
"strings"

Expand Down Expand Up @@ -41,6 +43,33 @@ type Config struct {
RssURL string
}

func (c *Config) NormlizeConfigDir() (bool, error) {
var changed bool
if c.DownloadDirectory != "" {
dldir, err := filepath.Abs(c.DownloadDirectory)
if err != nil {
return false, fmt.Errorf("Invalid path %s, %w", c.WatchDirectory, err)
}
if c.DownloadDirectory != dldir {
changed = true
c.DownloadDirectory = dldir
}
}

if c.WatchDirectory != "" {
wdir, err := filepath.Abs(c.WatchDirectory)
if err != nil {
return false, fmt.Errorf("Invalid path %s, %w", c.WatchDirectory, err)
}
if c.WatchDirectory != wdir {
changed = true
c.WatchDirectory = wdir
}
}

return changed, nil
}

func (c *Config) UploadLimiter() *rate.Limiter {
l, err := rateLimiter(c.UploadRate)
if err != nil {
Expand Down
41 changes: 18 additions & 23 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -108,7 +107,7 @@ func (s *Server) viperConf() (*engine.Config, error) {
viper.AddConfigPath("/etc/cloud-torrent/")
viper.AddConfigPath("$HOME/.cloud-torrent")

if stat, err := os.Stat(s.ConfigPath); stat != nil && err == nil {
if fileExists(s.ConfigPath) {
viper.SetConfigFile(s.ConfigPath)
}

Expand Down Expand Up @@ -189,8 +188,14 @@ func (s *Server) Run(version string) error {
}

// normalriz config file
if err := s.normlizeConfigDir(c); err != nil {
return fmt.Errorf("initial configure failed: %w", err)
dirChanged, err := c.NormlizeConfigDir()
if err != nil {
return err
}

if dirChanged {
viper.Set("DownloadDirectory", c.DownloadDirectory)
viper.Set("WatchDirectory", c.WatchDirectory)
}

if err := detectDiskStat(c.DownloadDirectory); err != nil {
Expand All @@ -207,8 +212,7 @@ func (s *Server) Run(version string) error {
viper.Debug()
}

log.Println("Current Configfile: ", viper.ConfigFileUsed())

log.Println("[config] current file path: ", viper.ConfigFileUsed())
if s.ConvYAML {
pyml := path.Join(path.Dir(s.ConfigPath), "cloud-torrent.yaml")
if err := viper.WriteConfigAs(pyml); err != nil {
Expand All @@ -217,8 +221,11 @@ func (s *Server) Run(version string) error {
return fmt.Errorf("Config file written to %s", pyml)
}

if err := viper.WriteConfigAs(viper.ConfigFileUsed()); err != nil {
return err
if cf := viper.ConfigFileUsed(); !fileExists(cf) || dirChanged {
log.Println("[config] config file writted: ", cf)
if err := viper.WriteConfigAs(cf); err != nil {
return err
}
}

s.backgroundRoutines()
Expand Down Expand Up @@ -290,19 +297,7 @@ func (s *Server) Run(version string) error {
return server.ListenAndServe()
}

func (s *Server) normlizeConfigDir(c *engine.Config) error {
dldir, err := filepath.Abs(c.DownloadDirectory)
if err != nil {
return fmt.Errorf("Invalid path %s, %w", c.WatchDirectory, err)
}
c.DownloadDirectory = dldir
viper.Set("DownloadDirectory", dldir)

wdir, err := filepath.Abs(c.WatchDirectory)
if err != nil {
return fmt.Errorf("Invalid path %s, %w", c.WatchDirectory, err)
}
c.WatchDirectory = wdir
viper.Set("WatchDirectory", wdir)
return nil
func fileExists(fn string) bool {
stat, err := os.Stat(fn)
return stat != nil && err == nil
}
2 changes: 1 addition & 1 deletion server/server_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (s *Server) apiConfigure(data []byte) error {
if err := json.Unmarshal(data, &c); err != nil {
return err
}
if err := s.normlizeConfigDir(&c); err != nil {
if _, err := c.NormlizeConfigDir(); err != nil {
return err
}

Expand Down

0 comments on commit dec2efd

Please sign in to comment.