-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring and user request improvements (#344)
- Loading branch information
Showing
15 changed files
with
321 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package arr | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/h3mmy/bloopyboi/bot/internal/config" | ||
"github.com/h3mmy/bloopyboi/bot/internal/log" | ||
"github.com/h3mmy/bloopyboi/bot/internal/models" | ||
"go.uber.org/zap" | ||
"golift.io/starr" | ||
"golift.io/starr/lidarr" | ||
"golift.io/starr/prowlarr" | ||
"golift.io/starr/radarr" | ||
"golift.io/starr/readarr" | ||
"golift.io/starr/sonarr" | ||
) | ||
|
||
type ArrClientMap map[string]starr.APIer | ||
type ArrClientRegister map[starr.App]ArrClientMap | ||
|
||
type ArrClientRegistry struct { | ||
meta models.BloopyMeta | ||
logger *zap.Logger | ||
registry ArrClientRegister | ||
} | ||
|
||
type ArrClientSet struct { | ||
meta models.BloopyMeta | ||
logger *zap.Logger | ||
clientMap map[string]starr.APIer | ||
} | ||
|
||
func NewArrClientSet(clients map[string]starr.APIer) *ArrClientSet { | ||
mta := models.NewBloopyMeta() | ||
lgr := log.NewZapLogger() | ||
return &ArrClientSet{ | ||
meta: mta, | ||
logger: lgr, | ||
clientMap: clients, | ||
} | ||
} | ||
|
||
func NewArrClientRegistry(controllerName string) *ArrClientRegistry { | ||
mta := models.NewBloopyMeta(controllerName) | ||
lgr := log.NewZapLogger().Named("arr_client_registry") | ||
return &ArrClientRegistry{ | ||
meta: mta, | ||
logger: lgr, | ||
registry: make(map[starr.App]ArrClientMap), | ||
} | ||
} | ||
|
||
func BuildArrClient(cfg *config.ArrClientConfig) (starr.APIer, error) { | ||
params := cfg.ToParams() | ||
starrConfig := starr.New( | ||
params[config.ApiKey], | ||
params[config.AppURL], | ||
starr.DefaultTimeout, | ||
) | ||
switch strings.ToLower(cfg.Type) { | ||
case starr.Sonarr.Lower(): | ||
return sonarr.New(starrConfig), nil | ||
case starr.Radarr.Lower(): | ||
return radarr.New(starrConfig), nil | ||
case starr.Readarr.Lower(): | ||
return readarr.New(starrConfig), nil | ||
case starr.Prowlarr.Lower(): | ||
return prowlarr.New(starrConfig), nil | ||
case starr.Lidarr.Lower(): | ||
return lidarr.New(starrConfig), nil | ||
} | ||
return nil, fmt.Errorf("Could not build client %s of type: %s", cfg.Name, cfg.Type) | ||
} | ||
|
||
func (s *ArrClientRegistry) AddClient(cfg *config.ArrClientConfig) error { | ||
logger := s.logger.With(zap.String("clientName", cfg.Name)).With(zap.String("clientType", cfg.Type)) | ||
key := starr.App(cfg.Type) | ||
if val, ok := s.registry[key]; ok { | ||
logger.Debug("register exists for client type.") | ||
client, err := BuildArrClient(cfg) | ||
if err != nil { | ||
logger.Error("error building client", zap.Error(err)) | ||
return err | ||
} | ||
if _, ok := val[cfg.Name]; ok { | ||
logger.Warn("entry already exists with client Name. Existing entry will be overwritten.") | ||
} | ||
val[cfg.Name] = client | ||
logger.Debug("added client to register") | ||
} else { | ||
logger.Debug("no existing entries for client type. adding new entry") | ||
cMap := make(map[string]starr.APIer) | ||
client, err := BuildArrClient(cfg) | ||
if err != nil { | ||
logger.Error("error building client", zap.Error(err)) | ||
return err | ||
} | ||
cMap[cfg.Name] = client | ||
logger.Debug("added client to registry") | ||
s.registry[key] = cMap | ||
logger.Debug("Added new type to register") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// func (s *ArrClientRegistry) GetClientSet(starr.App) (starr.APIer, error) { | ||
// if cMap, ok := s. | ||
// } |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package config | ||
|
||
type ArrClientParam string | ||
|
||
const ApiKey = "apiKey" | ||
const AppURL = "appUrl" | ||
|
||
type ArrClientConfig struct { | ||
Name string | ||
Type string | ||
URL string | ||
ApiKey string | ||
} | ||
|
||
func (cfg *ArrClientConfig) ToParams() map[ArrClientParam]string { | ||
params := make(map[ArrClientParam]string, 2) | ||
params[ApiKey] = cfg.ApiKey | ||
params[AppURL] = cfg.URL | ||
return params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package config | ||
|
||
type DiscordConfig struct { | ||
Token string `mapstructure:"token"` | ||
AppName string `mapstructure:"name"` | ||
AppID int64 `mapstructure:"appId"` | ||
GuildConfigs []DiscordGuildConfig `mapstructure:"guilds"` | ||
} | ||
|
||
// Guild Specific Config | ||
type DiscordGuildConfig struct { | ||
GuildId string `mapstructure:"id"` | ||
// Channel to be used for bot-specific announcements. If empty, no announcement will be sent. | ||
Announcement *AnnouncementConfig `mapstructure:"announcement"` | ||
GuildCommandConfig []GuildCommandConfig `mapstructure:"commands"` | ||
} | ||
|
||
// Config for dedicated announcement channel. If empty, no announcement will be sent. | ||
// the bot MUST have MANAGE_CHANNEL permissions | ||
// This will be a GUILD_ANNOUNCEMENT type channel https://discord.com/developers/docs/resources/channel | ||
type AnnouncementConfig struct { | ||
// Channel to be used for bot-specific announcements. | ||
Channel struct { | ||
Name string `mapstructure:"name"` | ||
ID string `mapstructure:"id"` | ||
} | ||
NSFW bool `mapstructure:"nsfw"` | ||
} | ||
|
||
type GuildCommandConfig struct { | ||
Name string `mapstructure:"name"` | ||
Enabled bool `mapstructure:"enabled"` | ||
// Allowed channels for command to be used in. If empty, command can be used in any channel. | ||
// If not empty, command can only be used in channels listed here. | ||
// Channels are case sensitive. | ||
// Example: | ||
// "channels": [ | ||
// "#general", | ||
// "#random" | ||
// ] | ||
// This command can only be used in #general and #random channels. | ||
// If empty, command can be used in any channel. | ||
// If not empty, command can only be used in channels listed here. | ||
// Channels are case sensitive. | ||
// Example: | ||
// "channels": [ | ||
// "#general", | ||
// "#random" | ||
// ] | ||
// This command can only be used in #general and #random channels. | ||
// If empty, command can be used in any channel. | ||
// If not empty, command can only be used in channels listed here. | ||
// Channels are case sensitive. | ||
// Example: | ||
// "channels": [ | ||
// "#general", | ||
Channels []int64 `mapstructure:"channels"` | ||
// Roles allowed to use command. If empty, command can be used by anyone. | ||
Roles []int64 `mapstructure:"roles"` | ||
} |
Oops, something went wrong.