diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index cb8c126..1521016 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -10,7 +10,6 @@ import ( "github.com/gabe565/castsponsorskip/internal/config" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) @@ -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() @@ -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() @@ -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() diff --git a/internal/config/config.go b/internal/config/config.go index e0d8a80..2f1b185 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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:"-"` diff --git a/internal/config/intervals.go b/internal/config/intervals.go index bcf61cd..a8368a4 100644 --- a/internal/config/intervals.go +++ b/internal/config/intervals.go @@ -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) } @@ -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) } @@ -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) } diff --git a/internal/config/log.go b/internal/config/log.go index cd584bd..2956ce2 100644 --- a/internal/config/log.go +++ b/internal/config/log.go @@ -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) } diff --git a/internal/config/network.go b/internal/config/network.go index 6db5f95..8cff622 100644 --- a/internal/config/network.go +++ b/internal/config/network.go @@ -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) } diff --git a/internal/config/sponsorblock.go b/internal/config/sponsorblock.go index e06eca8..131f591 100644 --- a/internal/config/sponsorblock.go +++ b/internal/config/sponsorblock.go @@ -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) } @@ -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) } diff --git a/internal/config/youtube.go b/internal/config/youtube.go index c167304..a4df96f 100644 --- a/internal/config/youtube.go +++ b/internal/config/youtube.go @@ -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) } @@ -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) }