Skip to content

Commit

Permalink
Convert from spf13/viper to knadh/koanf. (#117)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
aetaric and aetaric authored Nov 28, 2024
1 parent d403925 commit 11f0bde
Show file tree
Hide file tree
Showing 21 changed files with 300 additions and 211 deletions.
53 changes: 25 additions & 28 deletions check/checkrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"github.com/aetaric/checkrr/logging"
"github.com/knadh/koanf/v2"
"net/http"
"os"
"path/filepath"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/h2non/filetype/matchers"
"github.com/kalafut/imohash"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
bolt "go.etcd.io/bbolt"
"gopkg.in/vansante/go-ffprobe.v2"
)
Expand All @@ -40,8 +40,8 @@ type Checkrr struct {
removeAudio []string
removeLang []string
ignoreHidden bool
config *viper.Viper
FullConfig *viper.Viper
FullConfig *koanf.Koanf
config *koanf.Koanf
Chan *chan []string
Logger *logging.Log
}
Expand All @@ -58,10 +58,7 @@ func (c *Checkrr) Run() {
}

c.Stats = features.Stats{Log: *c.Logger, DB: c.DB}

if c.FullConfig.Sub("stats") != nil {
c.Stats.FromConfig(*c.FullConfig.Sub("stats"))
}
c.Stats.FromConfig(*c.FullConfig.Cut("stats"))

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

// Setup CSV writer
if c.config.GetString("csvfile") != "" {
c.csv = features.CSV{FilePath: c.config.GetString("csvfile"), Log: c.Logger}
if c.config.String("csvfile") != "" {
c.csv = features.CSV{FilePath: c.config.String("csvfile"), Log: c.Logger}
c.csv.Open()
}

c.ignoreExts = c.config.GetStringSlice("ignoreexts")
c.ignorePaths = c.config.GetStringSlice("ignorepaths")
c.removeVideo = c.config.GetStringSlice("removevideo")
c.removeAudio = c.config.GetStringSlice("removeaudio")
c.removeLang = c.config.GetStringSlice("removelang")
c.ignoreHidden = c.config.GetBool("ignorehidden")
c.ignoreExts = c.config.Strings("ignoreexts")
c.ignorePaths = c.config.Strings("ignorepaths")
c.removeVideo = c.config.Strings("removevideo")
c.removeAudio = c.config.Strings("removeaudio")
c.removeLang = c.config.Strings("removelang")
c.ignoreHidden = c.config.Bool("ignorehidden")

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

c.Stats.Start()

c.Logger.Debug(c.config.GetStringSlice("checkpath"))
c.Logger.Debug(c.config.Strings("checkpath"))

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

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

func (c *Checkrr) FromConfig(conf *viper.Viper) {
func (c *Checkrr) FromConfig(conf *koanf.Koanf) {
c.config = conf
}

func (c *Checkrr) connectServices() {
if viper.GetViper().Sub("arr") != nil {
arrConfig := viper.GetViper().Sub("arr")
arrKeys := viper.GetViper().Sub("arr").AllKeys()
if c.FullConfig.Get("arr") != nil {
arrConfig := c.config.Cut("arr")
arrKeys := c.config.Cut("arr").Keys()
for _, key := range arrKeys {
if strings.Contains(key, "service") {
k := strings.Split(key, ".")[0]
config := arrConfig.Sub(k)
config := arrConfig.Cut(k)

if config.GetString("service") == "sonarr" {
if config.String("service") == "sonarr" {
sonarr := connections.Sonarr{Log: c.Logger}
sonarr.FromConfig(config)
sonarrConnected, sonarrMessage := sonarr.Connect()
Expand All @@ -207,7 +204,7 @@ func (c *Checkrr) connectServices() {
}
}

if config.GetString("service") == "radarr" {
if config.String("service") == "radarr" {
radarr := connections.Radarr{Log: c.Logger}
radarr.FromConfig(config)
radarrConnected, radarrMessage := radarr.Connect()
Expand All @@ -217,7 +214,7 @@ func (c *Checkrr) connectServices() {
}
}

if config.GetString("service") == "lidarr" {
if config.String("service") == "lidarr" {
lidarr := connections.Lidarr{Log: c.Logger}
lidarr.FromConfig(config)
lidarrConnected, lidarrMessage := lidarr.Connect()
Expand All @@ -232,9 +229,9 @@ func (c *Checkrr) connectServices() {
}

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

if c.config.GetString("csvfile") != "" {
if c.config.String("csvfile") != "" {
c.csv.Write(path, fileType)
}
}
Expand Down
18 changes: 9 additions & 9 deletions connections/lidarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package connections
import (
"fmt"
"github.com/aetaric/checkrr/logging"
"github.com/knadh/koanf/v2"
"strings"

"github.com/spf13/viper"
"golift.io/starr"
"golift.io/starr/lidarr"
)
Expand All @@ -23,15 +23,15 @@ type Lidarr struct {
Log *logging.Log
}

func (l *Lidarr) FromConfig(conf *viper.Viper) {
func (l *Lidarr) FromConfig(conf *koanf.Koanf) {
if conf != nil {
l.Address = conf.GetString("address")
l.Process = conf.GetBool("process")
l.ApiKey = conf.GetString("apikey")
l.Port = conf.GetInt("port")
l.BaseURL = conf.GetString("baseurl")
l.pathMaps = conf.GetStringMapString("mappings")
l.SSL = conf.GetBool("ssl")
l.Address = conf.String("address")
l.Process = conf.Bool("process")
l.ApiKey = conf.String("apikey")
l.Port = conf.Int("port")
l.BaseURL = conf.String("baseurl")
l.pathMaps = conf.StringMap("mappings")
l.SSL = conf.Bool("ssl")
l.Log.Debugf("Lidarr Path Maps: %v", l.pathMaps)
} else {
l.Process = false
Expand Down
18 changes: 9 additions & 9 deletions connections/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package connections
import (
"fmt"
"github.com/aetaric/checkrr/logging"
"github.com/knadh/koanf/v2"
"strings"

"github.com/spf13/viper"
"golift.io/starr"
"golift.io/starr/radarr"
)
Expand All @@ -23,15 +23,15 @@ type Radarr struct {
Log *logging.Log
}

func (r *Radarr) FromConfig(conf *viper.Viper) {
func (r *Radarr) FromConfig(conf *koanf.Koanf) {
if conf != nil {
r.Address = conf.GetString("address")
r.Process = conf.GetBool("process")
r.ApiKey = conf.GetString("apikey")
r.Port = conf.GetInt("port")
r.BaseURL = conf.GetString("baseurl")
r.pathMaps = conf.GetStringMapString("mappings")
r.SSL = conf.GetBool("ssl")
r.Address = conf.String("address")
r.Process = conf.Bool("process")
r.ApiKey = conf.String("apikey")
r.Port = conf.Int("port")
r.BaseURL = conf.String("baseurl")
r.pathMaps = conf.StringMap("mappings")
r.SSL = conf.Bool("ssl")
r.Log.Debugf("Radarr Path Maps: %v", r.pathMaps)
} else {
r.Process = false
Expand Down
18 changes: 9 additions & 9 deletions connections/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package connections
import (
"fmt"
"github.com/aetaric/checkrr/logging"
"github.com/knadh/koanf/v2"
"strings"

"github.com/spf13/viper"
"golift.io/starr"
"golift.io/starr/sonarr"
)
Expand All @@ -23,15 +23,15 @@ type Sonarr struct {
Log *logging.Log
}

func (s *Sonarr) FromConfig(conf *viper.Viper) {
func (s *Sonarr) FromConfig(conf *koanf.Koanf) {
if conf != nil {
s.Address = conf.GetString("address")
s.Process = conf.GetBool("process")
s.ApiKey = conf.GetString("apikey")
s.Port = conf.GetInt("port")
s.BaseURL = conf.GetString("baseurl")
s.pathMaps = conf.GetStringMapString("mappings")
s.SSL = conf.GetBool("ssl")
s.Address = conf.String("address")
s.Process = conf.Bool("process")
s.ApiKey = conf.String("apikey")
s.Port = conf.Int("port")
s.BaseURL = conf.String("baseurl")
s.pathMaps = conf.StringMap("mappings")
s.SSL = conf.Bool("ssl")
s.Log.Debugf("Sonarr Path Maps: %v", s.pathMaps)
} else {
s.Process = false
Expand Down
32 changes: 16 additions & 16 deletions features/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/aetaric/checkrr/logging"
"github.com/knadh/koanf/v2"
"net/http"
"os"
"strings"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/influxdata/influxdb-client-go/v2/api"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
bolt "go.etcd.io/bbolt"
)

Expand All @@ -23,7 +23,7 @@ type Stats struct {
writeAPI1 api.WriteAPIBlocking `json:"-"`
influxdb2 influxdb2.Client `json:"-"`
writeAPI2 api.WriteAPIBlocking `json:"-"`
config viper.Viper `json:"-"`
config koanf.Koanf `json:"-"`
Log logging.Log `json:"-"`
splunk Splunk `json:"-"`
splunkConfigured bool `json:"-"`
Expand Down Expand Up @@ -68,33 +68,33 @@ type Splunk struct {
token string
}

func (s *Stats) FromConfig(config viper.Viper) {
func (s *Stats) FromConfig(config koanf.Koanf) {
s.config = config
if config.Sub("influxdb1") != nil {
influx := config.Sub("influxdb1")
if len(config.Cut("influxdb1").Keys()) != 0 {
influx := config.Cut("influxdb1")

var token string
if influx.GetString("user") != "" {
token = fmt.Sprintf("%s:%s", influx.GetString("user"), influx.GetString("pass"))
if influx.String("user") != "" {
token = fmt.Sprintf("%s:%s", influx.String("user"), influx.String("pass"))
} else {
token = ""
}

s.influxdb1 = influxdb2.NewClient(influx.GetString("url"), token)
s.writeAPI1 = s.influxdb1.WriteAPIBlocking("", influx.GetString("bucket"))
s.influxdb1 = influxdb2.NewClient(influx.String("url"), token)
s.writeAPI1 = s.influxdb1.WriteAPIBlocking("", influx.String("bucket"))
s.writeAPI1.EnableBatching()
s.Log.WithFields(log.Fields{"startup": true, "influxdb": "enabled"}).Info("Sending data to InfluxDB 1.x")
}
if config.Sub("influxdb2") != nil {
influx := config.Sub("influxdb2")
s.influxdb2 = influxdb2.NewClient(influx.GetString("url"), influx.GetString("token"))
s.writeAPI2 = s.influxdb2.WriteAPIBlocking(influx.GetString("org"), influx.GetString("bucket"))
if len(config.Cut("influxdb2").Keys()) != 0 {
influx := config.Cut("influxdb2")
s.influxdb2 = influxdb2.NewClient(influx.String("url"), influx.String("token"))
s.writeAPI2 = s.influxdb2.WriteAPIBlocking(influx.String("org"), influx.String("bucket"))
s.writeAPI2.EnableBatching()
s.Log.WithFields(log.Fields{"startup": true, "influxdb": "enabled"}).Info("Sending data to InfluxDB 2.x")
}
if config.Sub("splunk") != nil {
splunk := config.Sub("splunk")
s.splunk = Splunk{address: splunk.GetString("address"), token: splunk.GetString("token")}
if len(config.Cut("splunk").Keys()) != 0 {
splunk := config.Cut("splunk")
s.splunk = Splunk{address: splunk.String("address"), token: splunk.String("token")}
s.splunkConfigured = true
s.Log.WithFields(log.Fields{"startup": true, "splunk stats": "enabled"}).Info("Sending stats data to Splunk")
}
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ require (
github.com/influxdata/influxdb-client-go/v2 v2.14.0
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/kalafut/imohash v1.1.0
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/file v1.1.2
github.com/knadh/koanf/v2 v2.1.1
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
Expand All @@ -26,6 +29,13 @@ require (
gopkg.in/vansante/go-ffprobe.v2 v2.2.0
)

require (
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
)

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand Down Expand Up @@ -122,6 +124,14 @@ github.com/kalafut/imohash v1.1.0/go.mod h1:6cn9lU0Sj8M4eu9UaQm1kR/5y3k/ayB68ynt
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
github.com/knadh/koanf/providers/file v1.1.2 h1:aCC36YGOgV5lTtAFz2qkgtWdeQsgfxUkxDOe+2nQY3w=
github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM=
github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -138,9 +148,13 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
Loading

0 comments on commit 11f0bde

Please sign in to comment.