Skip to content

Commit

Permalink
refactor: validate all plex libraries specified are of the correct ty…
Browse files Browse the repository at this point in the history
…pe for the pvr
  • Loading branch information
l3uddz committed Aug 6, 2020
1 parent 34494f0 commit 77d871c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
6 changes: 3 additions & 3 deletions cmd/plexarr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ var (
globals

// flags
PVR string `required:"1" type:"string" env:"PLEXARR_PVR" help:"PVR to match from"`
Library []string `required:"1" type:"string" env:"PLEXARR_LIBRARY" help:"Plex Library to match against"`
PVR string `required:"1" type:"string" help:"PVR to match from"`
Library []string `required:"1" type:"string" help:"Plex Library to match against"`

Config string `type:"path" default:"${config_file}" env:"PLEXARR_CONFIG" help:"Config file path"`
Log string `type:"path" default:"${log_file}" env:"PLEXARR_LOG" help:"Log file path"`
Expand Down Expand Up @@ -197,7 +197,7 @@ func main() {
}

// retrieve items from pvr
pvr, err := getPvr(cli.PVR, cfg, plexItems[0].Type)
pvr, err := getPvr(cli.PVR, cfg, plexItems)
if err != nil {
log.Fatal().
Err(err).
Expand Down
15 changes: 11 additions & 4 deletions cmd/plexarr/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type plexLibraryItem struct {

func getPlexLibraryItems(p *plex.Client, libraries []string) ([]plexLibraryItem, error) {
plexItems := make([]plexLibraryItem, 0)
totalItems := 0

for _, library := range libraries {
// get library items
Expand All @@ -25,8 +26,6 @@ func getPlexLibraryItems(p *plex.Client, libraries []string) ([]plexLibraryItem,
Bool("dry_run", cli.DryRun).
Logger()

l.Debug().Msg("Retrieving plex library items...")

items, libType, err := p.GetLibraryItems(library)
if err != nil {
return nil, fmt.Errorf("failed %q plex library items: %w", library, err)
Expand All @@ -36,8 +35,11 @@ func getPlexLibraryItems(p *plex.Client, libraries []string) ([]plexLibraryItem,
return nil, fmt.Errorf("no plex library items found for: %v", library)
}

l.Info().
Int("count", len(items)).
count := len(items)
totalItems += count

l.Debug().
Int("count", count).
Msg("Retrieved plex library items")

plexItems = append(plexItems, plexLibraryItem{
Expand All @@ -47,6 +49,11 @@ func getPlexLibraryItems(p *plex.Client, libraries []string) ([]plexLibraryItem,
})
}

log.Info().
Int("count", totalItems).
Strs("libraries", libraries).
Msg("Retrieved plex library items")

return plexItems, nil
}

Expand Down
20 changes: 17 additions & 3 deletions cmd/plexarr/pvr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ import (
"strings"
)

func getPvr(name string, cfg config, libraryType plexarr.LibraryType) (plexarr.Pvr, error) {
func getPvr(name string, cfg config, libraries []plexLibraryItem) (plexarr.Pvr, error) {
// radarr
for _, pvr := range cfg.Pvr.Radarr {
if !strings.EqualFold(name, pvr.Name) {
continue
}

// validate all libraries are movies
for _, lib := range libraries {
if lib.Type != plexarr.MovieLibrary {
return nil, errors.New("radarr only supports movie libraries")
}
}

// init pvr object
p, err := radarr.New(pvr, libraryType)
p, err := radarr.New(pvr)
if err != nil {
return nil, fmt.Errorf("failed initialising radarr pvr %v: %w", pvr.Name, err)
}
Expand All @@ -31,8 +38,15 @@ func getPvr(name string, cfg config, libraryType plexarr.LibraryType) (plexarr.P
continue
}

// validate all libraries are series
for _, lib := range libraries {
if lib.Type != plexarr.MovieLibrary {
return nil, errors.New("sonarr only supports tv libraries")
}
}

// init pvr object
p, err := sonarr.New(pvr, libraryType)
p, err := sonarr.New(pvr)
if err != nil {
return nil, fmt.Errorf("failed initialising sonarr pvr %v: %w", pvr.Name, err)
}
Expand Down
7 changes: 1 addition & 6 deletions pvrs/radarr/radarr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package radarr

import (
"errors"
"github.com/l3uddz/plexarr"
"github.com/rs/zerolog"
)
Expand All @@ -23,11 +22,7 @@ type Client struct {
rewrite plexarr.Rewriter
}

func New(c Config, libraryType plexarr.LibraryType) (*Client, error) {
if libraryType != plexarr.MovieLibrary {
return nil, errors.New("only movie libraries are supported")
}

func New(c Config) (*Client, error) {
rewriter, err := plexarr.NewRewriter(c.Rewrite)
if err != nil {
return nil, err
Expand Down
7 changes: 1 addition & 6 deletions pvrs/sonarr/sonarr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sonarr

import (
"errors"
"github.com/l3uddz/plexarr"
"github.com/rs/zerolog"
)
Expand All @@ -23,11 +22,7 @@ type Client struct {
rewrite plexarr.Rewriter
}

func New(c Config, libraryType plexarr.LibraryType) (*Client, error) {
if libraryType != plexarr.TvLibrary {
return nil, errors.New("only tv libraries are supported")
}

func New(c Config) (*Client, error) {
rewriter, err := plexarr.NewRewriter(c.Rewrite)
if err != nil {
return nil, err
Expand Down

0 comments on commit 77d871c

Please sign in to comment.