Skip to content

Commit

Permalink
style(config): Change how defaults are handled and add reset func
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 3, 2023
1 parent 715ec0c commit 6413b58
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
10 changes: 3 additions & 7 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/gabe565/castsponsorskip/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,8 +21,7 @@ func randDuration() time.Duration {

func TestFlags(t *testing.T) {
defer func() {
viper.Reset()
config.Default = &config.Config{}
config.Reset()
}()

discoverInterval := randDuration()
Expand Down Expand Up @@ -66,8 +64,7 @@ func TestFlags(t *testing.T) {

func TestEnvs(t *testing.T) {
defer func() {
viper.Reset()
config.Default = &config.Config{}
config.Reset()
}()

discoverInterval := randDuration()
Expand Down Expand Up @@ -119,8 +116,7 @@ func TestEnvs(t *testing.T) {

func TestSBCEnvs(t *testing.T) {
defer func() {
viper.Reset()
config.Default = &config.Config{}
config.Reset()
}()

discoverInterval := randDuration()
Expand Down
24 changes: 23 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,29 @@ import (
"github.com/spf13/viper"
)

var Default = &Config{}
var Default Config

func init() {
Reset()
}

func Reset() {
Default = Config{
LogLevel: "info",

DiscoverInterval: 5 * time.Minute,
PausedInterval: time.Minute,
PlayingInterval: 500 * time.Millisecond,

NetworkInterface: "",

Categories: []string{"sponsor"},
ActionTypes: []string{"skip", "mute"},

YouTubeAPIKey: "",
MuteAds: true,
}
}

type Config struct {
viper *viper.Viper `mapstructure:"-"`
Expand Down
6 changes: 3 additions & 3 deletions internal/config/intervals.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func (c *Config) RegisterDiscoverInterval(cmd *cobra.Command) {
key := "discover-interval"
cmd.PersistentFlags().Duration(key, 5*time.Minute, "Interval to restart the DNS discovery client")
cmd.PersistentFlags().Duration(key, Default.DiscoverInterval, "Interval to restart the DNS discovery client")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand All @@ -34,7 +34,7 @@ func (c *Config) RegisterDiscoverInterval(cmd *cobra.Command) {

func (c *Config) RegisterPausedInterval(cmd *cobra.Command) {
key := "paused-interval"
cmd.PersistentFlags().Duration(key, time.Minute, "Interval to scan paused devices")
cmd.PersistentFlags().Duration(key, Default.PausedInterval, "Interval to scan paused devices")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand All @@ -47,7 +47,7 @@ func (c *Config) RegisterPausedInterval(cmd *cobra.Command) {

func (c *Config) RegisterPlayingInterval(cmd *cobra.Command) {
key := "playing-interval"
cmd.PersistentFlags().Duration(key, 500*time.Millisecond, "Interval to scan playing devices")
cmd.PersistentFlags().Duration(key, Default.PlayingInterval, "Interval to scan playing devices")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func (c *Config) RegisterLogLevel(cmd *cobra.Command) {
key := "log-level"
cmd.PersistentFlags().String(key, "info", "Log level (debug, info, warn, error)")
cmd.PersistentFlags().String(key, Default.LogLevel, "Log level (debug, info, warn, error)")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func (c *Config) RegisterNetworkInterface(cmd *cobra.Command) {
key := "network-interface"
cmd.PersistentFlags().StringP(key, "i", "", "Network interface to use for multicast dns discovery")
cmd.PersistentFlags().StringP(key, "i", Default.NetworkInterface, "Network interface to use for multicast dns discovery")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/sponsorblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func (c *Config) RegisterCategories(cmd *cobra.Command) {
key := "categories"
cmd.PersistentFlags().StringSliceP(key, "c", []string{"sponsor"}, "Comma-separated list of SponsorBlock categories to skip")
cmd.PersistentFlags().StringSliceP(key, "c", Default.Categories, "Comma-separated list of SponsorBlock categories to skip")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand All @@ -30,7 +30,7 @@ func (c *Config) RegisterCategories(cmd *cobra.Command) {

func (c *Config) RegisterActionTypes(cmd *cobra.Command) {
key := "action-types"
cmd.PersistentFlags().StringSlice(key, []string{"skip", "mute"}, "SponsorBlock action types to handle. Shorter segments that overlap with content can be muted instead of skipped.")
cmd.PersistentFlags().StringSlice(key, Default.ActionTypes, "SponsorBlock action types to handle. Shorter segments that overlap with content can be muted instead of skipped.")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func (c *Config) RegisterYouTubeAPIKey(cmd *cobra.Command) {
key := "youtube-api-key"
cmd.PersistentFlags().String(key, "", "YouTube API key for fallback video identification (required on some Chromecast devices).")
cmd.PersistentFlags().String(key, Default.YouTubeAPIKey, "YouTube API key for fallback video identification (required on some Chromecast devices).")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand All @@ -23,7 +23,7 @@ func (c *Config) RegisterYouTubeAPIKey(cmd *cobra.Command) {

func (c *Config) RegisterMuteAds(cmd *cobra.Command) {
key := "mute-ads"
cmd.PersistentFlags().Bool(key, true, "Mutes the device while an ad is playing")
cmd.PersistentFlags().Bool(key, Default.MuteAds, "Mutes the device while an ad is playing")
if err := c.viper.BindPFlag(key, cmd.PersistentFlags().Lookup(key)); err != nil {
panic(err)
}
Expand Down

0 comments on commit 6413b58

Please sign in to comment.