Skip to content

Commit 11f0bde

Browse files
authored
Convert from spf13/viper to knadh/koanf. (#117)
* Closes #116. Convert from spf13/viper to knadh/koanf. Things might be slightly broken. * Various changes. Fix typos in scheduler function for webserver.go. Remove old testing logic. Provide more robust logging if no config was specified and we are trying to load a default config. --------- Co-authored-by: aetaric <aetaric@aetaric.ninja>
1 parent d403925 commit 11f0bde

21 files changed

Lines changed: 300 additions & 211 deletions

check/checkrr.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"github.com/aetaric/checkrr/logging"
9+
"github.com/knadh/koanf/v2"
910
"net/http"
1011
"os"
1112
"path/filepath"
@@ -20,7 +21,6 @@ import (
2021
"github.com/h2non/filetype/matchers"
2122
"github.com/kalafut/imohash"
2223
log "github.com/sirupsen/logrus"
23-
"github.com/spf13/viper"
2424
bolt "go.etcd.io/bbolt"
2525
"gopkg.in/vansante/go-ffprobe.v2"
2626
)
@@ -40,8 +40,8 @@ type Checkrr struct {
4040
removeAudio []string
4141
removeLang []string
4242
ignoreHidden bool
43-
config *viper.Viper
44-
FullConfig *viper.Viper
43+
FullConfig *koanf.Koanf
44+
config *koanf.Koanf
4545
Chan *chan []string
4646
Logger *logging.Log
4747
}
@@ -58,10 +58,7 @@ func (c *Checkrr) Run() {
5858
}
5959

6060
c.Stats = features.Stats{Log: *c.Logger, DB: c.DB}
61-
62-
if c.FullConfig.Sub("stats") != nil {
63-
c.Stats.FromConfig(*c.FullConfig.Sub("stats"))
64-
}
61+
c.Stats.FromConfig(*c.FullConfig.Cut("stats"))
6562

6663
// Connect to Sonarr, Radarr, and Lidarr
6764
c.connectServices()
@@ -70,17 +67,17 @@ func (c *Checkrr) Run() {
7067
c.connectNotifications()
7168

7269
// Setup CSV writer
73-
if c.config.GetString("csvfile") != "" {
74-
c.csv = features.CSV{FilePath: c.config.GetString("csvfile"), Log: c.Logger}
70+
if c.config.String("csvfile") != "" {
71+
c.csv = features.CSV{FilePath: c.config.String("csvfile"), Log: c.Logger}
7572
c.csv.Open()
7673
}
7774

78-
c.ignoreExts = c.config.GetStringSlice("ignoreexts")
79-
c.ignorePaths = c.config.GetStringSlice("ignorepaths")
80-
c.removeVideo = c.config.GetStringSlice("removevideo")
81-
c.removeAudio = c.config.GetStringSlice("removeaudio")
82-
c.removeLang = c.config.GetStringSlice("removelang")
83-
c.ignoreHidden = c.config.GetBool("ignorehidden")
75+
c.ignoreExts = c.config.Strings("ignoreexts")
76+
c.ignorePaths = c.config.Strings("ignorepaths")
77+
c.removeVideo = c.config.Strings("removevideo")
78+
c.removeAudio = c.config.Strings("removeaudio")
79+
c.removeLang = c.config.Strings("removelang")
80+
c.ignoreHidden = c.config.Bool("ignorehidden")
8481

8582
// I'm tired of waiting for filetype to support this. We'll force it by adding to the matchers on the fly.
8683
// TODO: if h2non/filetype#120 ever gets completed, remove this logic
@@ -91,9 +88,9 @@ func (c *Checkrr) Run() {
9188

9289
c.Stats.Start()
9390

94-
c.Logger.Debug(c.config.GetStringSlice("checkpath"))
91+
c.Logger.Debug(c.config.Strings("checkpath"))
9592

96-
for _, path := range c.config.GetStringSlice("checkpath") {
93+
for _, path := range c.config.Strings("checkpath") {
9794
c.Logger.WithFields(log.Fields{"startup": true}).Debugf("Path: %v", path)
9895

9996
err := filepath.WalkDir(path, func(path string, d os.DirEntry, err error) error {
@@ -184,20 +181,20 @@ func (c *Checkrr) Run() {
184181
ch <- []string{"time"}
185182
}
186183

187-
func (c *Checkrr) FromConfig(conf *viper.Viper) {
184+
func (c *Checkrr) FromConfig(conf *koanf.Koanf) {
188185
c.config = conf
189186
}
190187

191188
func (c *Checkrr) connectServices() {
192-
if viper.GetViper().Sub("arr") != nil {
193-
arrConfig := viper.GetViper().Sub("arr")
194-
arrKeys := viper.GetViper().Sub("arr").AllKeys()
189+
if c.FullConfig.Get("arr") != nil {
190+
arrConfig := c.config.Cut("arr")
191+
arrKeys := c.config.Cut("arr").Keys()
195192
for _, key := range arrKeys {
196193
if strings.Contains(key, "service") {
197194
k := strings.Split(key, ".")[0]
198-
config := arrConfig.Sub(k)
195+
config := arrConfig.Cut(k)
199196

200-
if config.GetString("service") == "sonarr" {
197+
if config.String("service") == "sonarr" {
201198
sonarr := connections.Sonarr{Log: c.Logger}
202199
sonarr.FromConfig(config)
203200
sonarrConnected, sonarrMessage := sonarr.Connect()
@@ -207,7 +204,7 @@ func (c *Checkrr) connectServices() {
207204
}
208205
}
209206

210-
if config.GetString("service") == "radarr" {
207+
if config.String("service") == "radarr" {
211208
radarr := connections.Radarr{Log: c.Logger}
212209
radarr.FromConfig(config)
213210
radarrConnected, radarrMessage := radarr.Connect()
@@ -217,7 +214,7 @@ func (c *Checkrr) connectServices() {
217214
}
218215
}
219216

220-
if config.GetString("service") == "lidarr" {
217+
if config.String("service") == "lidarr" {
221218
lidarr := connections.Lidarr{Log: c.Logger}
222219
lidarr.FromConfig(config)
223220
lidarrConnected, lidarrMessage := lidarr.Connect()
@@ -232,9 +229,9 @@ func (c *Checkrr) connectServices() {
232229
}
233230

234231
func (c *Checkrr) connectNotifications() {
235-
if viper.GetViper().Sub("notifications") != nil {
232+
if c.FullConfig.Cut("notifications") != nil {
236233
c.notifications = notifications.Notifications{Log: c.Logger}
237-
c.notifications.FromConfig(*viper.GetViper().Sub("notifications"))
234+
c.notifications.FromConfig(c.config.Cut("notifications"))
238235
c.notifications.Connect()
239236
} else {
240237
c.Logger.WithFields(log.Fields{"Startup": true, "Notifications Connected": false}).Warn("No config options for notifications found.")
@@ -434,7 +431,7 @@ func (c *Checkrr) recordBadFile(path string, fileType string, reason string) {
434431
c.Logger.WithFields(log.Fields{"DB Update": "Failure"}).Warnf("Error: %v", err.Error())
435432
}
436433

437-
if c.config.GetString("csvfile") != "" {
434+
if c.config.String("csvfile") != "" {
438435
c.csv.Write(path, fileType)
439436
}
440437
}

connections/lidarr.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package connections
33
import (
44
"fmt"
55
"github.com/aetaric/checkrr/logging"
6+
"github.com/knadh/koanf/v2"
67
"strings"
78

8-
"github.com/spf13/viper"
99
"golift.io/starr"
1010
"golift.io/starr/lidarr"
1111
)
@@ -23,15 +23,15 @@ type Lidarr struct {
2323
Log *logging.Log
2424
}
2525

26-
func (l *Lidarr) FromConfig(conf *viper.Viper) {
26+
func (l *Lidarr) FromConfig(conf *koanf.Koanf) {
2727
if conf != nil {
28-
l.Address = conf.GetString("address")
29-
l.Process = conf.GetBool("process")
30-
l.ApiKey = conf.GetString("apikey")
31-
l.Port = conf.GetInt("port")
32-
l.BaseURL = conf.GetString("baseurl")
33-
l.pathMaps = conf.GetStringMapString("mappings")
34-
l.SSL = conf.GetBool("ssl")
28+
l.Address = conf.String("address")
29+
l.Process = conf.Bool("process")
30+
l.ApiKey = conf.String("apikey")
31+
l.Port = conf.Int("port")
32+
l.BaseURL = conf.String("baseurl")
33+
l.pathMaps = conf.StringMap("mappings")
34+
l.SSL = conf.Bool("ssl")
3535
l.Log.Debugf("Lidarr Path Maps: %v", l.pathMaps)
3636
} else {
3737
l.Process = false

connections/radarr.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package connections
33
import (
44
"fmt"
55
"github.com/aetaric/checkrr/logging"
6+
"github.com/knadh/koanf/v2"
67
"strings"
78

8-
"github.com/spf13/viper"
99
"golift.io/starr"
1010
"golift.io/starr/radarr"
1111
)
@@ -23,15 +23,15 @@ type Radarr struct {
2323
Log *logging.Log
2424
}
2525

26-
func (r *Radarr) FromConfig(conf *viper.Viper) {
26+
func (r *Radarr) FromConfig(conf *koanf.Koanf) {
2727
if conf != nil {
28-
r.Address = conf.GetString("address")
29-
r.Process = conf.GetBool("process")
30-
r.ApiKey = conf.GetString("apikey")
31-
r.Port = conf.GetInt("port")
32-
r.BaseURL = conf.GetString("baseurl")
33-
r.pathMaps = conf.GetStringMapString("mappings")
34-
r.SSL = conf.GetBool("ssl")
28+
r.Address = conf.String("address")
29+
r.Process = conf.Bool("process")
30+
r.ApiKey = conf.String("apikey")
31+
r.Port = conf.Int("port")
32+
r.BaseURL = conf.String("baseurl")
33+
r.pathMaps = conf.StringMap("mappings")
34+
r.SSL = conf.Bool("ssl")
3535
r.Log.Debugf("Radarr Path Maps: %v", r.pathMaps)
3636
} else {
3737
r.Process = false

connections/sonarr.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package connections
33
import (
44
"fmt"
55
"github.com/aetaric/checkrr/logging"
6+
"github.com/knadh/koanf/v2"
67
"strings"
78

8-
"github.com/spf13/viper"
99
"golift.io/starr"
1010
"golift.io/starr/sonarr"
1111
)
@@ -23,15 +23,15 @@ type Sonarr struct {
2323
Log *logging.Log
2424
}
2525

26-
func (s *Sonarr) FromConfig(conf *viper.Viper) {
26+
func (s *Sonarr) FromConfig(conf *koanf.Koanf) {
2727
if conf != nil {
28-
s.Address = conf.GetString("address")
29-
s.Process = conf.GetBool("process")
30-
s.ApiKey = conf.GetString("apikey")
31-
s.Port = conf.GetInt("port")
32-
s.BaseURL = conf.GetString("baseurl")
33-
s.pathMaps = conf.GetStringMapString("mappings")
34-
s.SSL = conf.GetBool("ssl")
28+
s.Address = conf.String("address")
29+
s.Process = conf.Bool("process")
30+
s.ApiKey = conf.String("apikey")
31+
s.Port = conf.Int("port")
32+
s.BaseURL = conf.String("baseurl")
33+
s.pathMaps = conf.StringMap("mappings")
34+
s.SSL = conf.Bool("ssl")
3535
s.Log.Debugf("Sonarr Path Maps: %v", s.pathMaps)
3636
} else {
3737
s.Process = false

features/stats.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"github.com/aetaric/checkrr/logging"
8+
"github.com/knadh/koanf/v2"
89
"net/http"
910
"os"
1011
"strings"
@@ -14,7 +15,6 @@ import (
1415
"github.com/influxdata/influxdb-client-go/v2/api"
1516
"github.com/jedib0t/go-pretty/v6/table"
1617
log "github.com/sirupsen/logrus"
17-
"github.com/spf13/viper"
1818
bolt "go.etcd.io/bbolt"
1919
)
2020

@@ -23,7 +23,7 @@ type Stats struct {
2323
writeAPI1 api.WriteAPIBlocking `json:"-"`
2424
influxdb2 influxdb2.Client `json:"-"`
2525
writeAPI2 api.WriteAPIBlocking `json:"-"`
26-
config viper.Viper `json:"-"`
26+
config koanf.Koanf `json:"-"`
2727
Log logging.Log `json:"-"`
2828
splunk Splunk `json:"-"`
2929
splunkConfigured bool `json:"-"`
@@ -68,33 +68,33 @@ type Splunk struct {
6868
token string
6969
}
7070

71-
func (s *Stats) FromConfig(config viper.Viper) {
71+
func (s *Stats) FromConfig(config koanf.Koanf) {
7272
s.config = config
73-
if config.Sub("influxdb1") != nil {
74-
influx := config.Sub("influxdb1")
73+
if len(config.Cut("influxdb1").Keys()) != 0 {
74+
influx := config.Cut("influxdb1")
7575

7676
var token string
77-
if influx.GetString("user") != "" {
78-
token = fmt.Sprintf("%s:%s", influx.GetString("user"), influx.GetString("pass"))
77+
if influx.String("user") != "" {
78+
token = fmt.Sprintf("%s:%s", influx.String("user"), influx.String("pass"))
7979
} else {
8080
token = ""
8181
}
8282

83-
s.influxdb1 = influxdb2.NewClient(influx.GetString("url"), token)
84-
s.writeAPI1 = s.influxdb1.WriteAPIBlocking("", influx.GetString("bucket"))
83+
s.influxdb1 = influxdb2.NewClient(influx.String("url"), token)
84+
s.writeAPI1 = s.influxdb1.WriteAPIBlocking("", influx.String("bucket"))
8585
s.writeAPI1.EnableBatching()
8686
s.Log.WithFields(log.Fields{"startup": true, "influxdb": "enabled"}).Info("Sending data to InfluxDB 1.x")
8787
}
88-
if config.Sub("influxdb2") != nil {
89-
influx := config.Sub("influxdb2")
90-
s.influxdb2 = influxdb2.NewClient(influx.GetString("url"), influx.GetString("token"))
91-
s.writeAPI2 = s.influxdb2.WriteAPIBlocking(influx.GetString("org"), influx.GetString("bucket"))
88+
if len(config.Cut("influxdb2").Keys()) != 0 {
89+
influx := config.Cut("influxdb2")
90+
s.influxdb2 = influxdb2.NewClient(influx.String("url"), influx.String("token"))
91+
s.writeAPI2 = s.influxdb2.WriteAPIBlocking(influx.String("org"), influx.String("bucket"))
9292
s.writeAPI2.EnableBatching()
9393
s.Log.WithFields(log.Fields{"startup": true, "influxdb": "enabled"}).Info("Sending data to InfluxDB 2.x")
9494
}
95-
if config.Sub("splunk") != nil {
96-
splunk := config.Sub("splunk")
97-
s.splunk = Splunk{address: splunk.GetString("address"), token: splunk.GetString("token")}
95+
if len(config.Cut("splunk").Keys()) != 0 {
96+
splunk := config.Cut("splunk")
97+
s.splunk = Splunk{address: splunk.String("address"), token: splunk.String("token")}
9898
s.splunkConfigured = true
9999
s.Log.WithFields(log.Fields{"startup": true, "splunk stats": "enabled"}).Info("Sending stats data to Splunk")
100100
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ require (
1616
github.com/influxdata/influxdb-client-go/v2 v2.14.0
1717
github.com/jedib0t/go-pretty/v6 v6.5.9
1818
github.com/kalafut/imohash v1.1.0
19+
github.com/knadh/koanf/parsers/yaml v0.1.0
20+
github.com/knadh/koanf/providers/file v1.1.2
21+
github.com/knadh/koanf/v2 v2.1.1
1922
github.com/robfig/cron/v3 v3.0.1
2023
github.com/sirupsen/logrus v1.9.3
2124
github.com/spf13/pflag v1.0.5
@@ -26,6 +29,13 @@ require (
2629
gopkg.in/vansante/go-ffprobe.v2 v2.2.0
2730
)
2831

32+
require (
33+
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
34+
github.com/knadh/koanf/maps v0.1.1 // indirect
35+
github.com/mitchellh/copystructure v1.2.0 // indirect
36+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
37+
)
38+
2939
require (
3040
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
3141
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27
9090
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
9191
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
9292
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
93+
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
94+
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
9395
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
9496
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
9597
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@@ -122,6 +124,14 @@ github.com/kalafut/imohash v1.1.0/go.mod h1:6cn9lU0Sj8M4eu9UaQm1kR/5y3k/ayB68ynt
122124
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
123125
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
124126
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
127+
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
128+
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
129+
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
130+
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
131+
github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUkxDOe+2nQY3w=
132+
github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
133+
github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM=
134+
github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es=
125135
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
126136
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
127137
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@@ -138,9 +148,13 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
138148
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
139149
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
140150
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
151+
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
152+
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
141153
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
142154
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
143155
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
156+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
157+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
144158
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
145159
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
146160
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

0 commit comments

Comments
 (0)