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

Commit

Permalink
use viper config
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Jan 7, 2020
1 parent 9d6001e commit d111327
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 66 deletions.
33 changes: 13 additions & 20 deletions engine/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package engine

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"reflect"
"strings"

"github.com/c2h5oh/datasize"
"github.com/spf13/viper"
"golang.org/x/time/rate"
)

Expand Down Expand Up @@ -97,25 +94,21 @@ func (c *Config) Validate(nc *Config) uint8 {

return status
}

func (c *Config) SaveConfigFile(configPath string) error {
b, err := json.MarshalIndent(&c, "", " ")
if err != nil {
return err
}

ob, err := ioutil.ReadFile(configPath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
if bytes.Compare(b, ob) == 0 {
return nil
func (c *Config) SyncViper(nc Config) error {
cv := reflect.ValueOf(*c)
nv := reflect.ValueOf(nc)
typeOfC := cv.Type()
for i := 0; i < typeOfC.NumField(); i++ {
if cv.Field(i).Interface() != nv.Field(i).Interface() {
name := typeOfC.Field(i).Name
oval := cv.Field(i).Interface()
val := nv.Field(i).Interface()
viper.Set(name, val)
log.Println("config updated ", name, ": ", oval, " -> ", val)
}
}

return ioutil.WriteFile(configPath, b, 0644)
return viper.WriteConfigAs(viper.ConfigFileUsed())
}

func rateLimiter(rstr string) (*rate.Limiter, error) {
Expand Down
10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@ require (
github.com/NYTimes/gziphandler v1.1.1
github.com/anacrolix/log v0.5.0
github.com/anacrolix/torrent v1.11.0
github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2 // indirect
github.com/boypt/scraper v1.0.2
github.com/c2h5oh/datasize v0.0.0-20171227191756-4eba002a5eae
github.com/elazarl/go-bindata-assetfs v1.0.0
github.com/elithrar/simple-scrypt v1.3.0 // indirect
github.com/gorilla/websocket v1.4.1 // indirect
github.com/jpillora/ansi v1.0.0 // indirect
github.com/jpillora/archive v0.0.0-20160301031048-e0b3681851f1
github.com/jpillora/cloud-torrent v0.0.0-00010101000000-000000000000
github.com/jpillora/cookieauth v0.0.0-20190219222732-2ae29b2a9c76
github.com/jpillora/eventsource v0.0.0-20170920003432-7ed8c999e167 // indirect
github.com/jpillora/opts v1.1.0
github.com/jpillora/requestlog v0.0.0-20181015073026-df8817be5f82
github.com/jpillora/sizestr v0.0.0-20160130011556-e2ea2fa42fb9 // indirect
github.com/jpillora/velox v0.0.0-20180825063758-42845d323220
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
github.com/mmcdole/gofeed v1.0.0-beta2
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect
github.com/pkg/errors v0.8.1
github.com/radovskyb/watcher v1.0.6
github.com/shirou/gopsutil v2.18.12+incompatible
github.com/skratchdot/open-golang v0.0.0-20190402232053-79abb63cd66e
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce // indirect
github.com/spf13/viper v1.6.1
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
)

Expand Down
Loading

0 comments on commit d111327

Please sign in to comment.