From e21b61ade96d359c5a36ed5b83e55a586c09ca46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justin=20L=C3=A9ger?= Date: Sun, 4 May 2025 02:35:41 -0400 Subject: [PATCH] Support optional filter in yaml config The documentation of the yaml config indicates that the filter list is optinal. If the list was not provided, no tracks were returned as we didn't append the track since there was no match. A default of name with ".*" is now set if not other filter is present. The tests were updated to cover the new behaviour. Additionally, the filter types were updated in the tests to match the supported filters in provider.go. --- config.go | 6 ++++++ config_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 45334af..011a807 100644 --- a/config.go +++ b/config.go @@ -94,6 +94,12 @@ func LoadConfig(path string) (*Config, error) { } func (c *Config) compileFilterRegexps() error { + if len(c.Filters) == 0 { + c.Filters = []*Filter{ + {Value: ".*", Type: "name"}, + } + } + for i, filter := range c.Filters { re, err := regexp.Compile(filter.Value) if err != nil { diff --git a/config_test.go b/config_test.go index a6069f1..936c8a9 100644 --- a/config_test.go +++ b/config_test.go @@ -23,9 +23,9 @@ ffmpeg: true maxStreams: 10 filters: - filter: sports.* - type: include + type: name - filter: news|weather - type: exclude + type: group `) tmpfile, err := os.CreateTemp("", "config*.yaml") @@ -54,14 +54,58 @@ filters: assert.Equal(t, 10, config.MaxStreams) assert.Len(t, config.Filters, 2) assert.Equal(t, "sports.*", config.Filters[0].Value) - assert.Equal(t, "include", config.Filters[0].Type) + assert.Equal(t, "name", config.Filters[0].Type) assert.NotNil(t, config.Filters[0].GetRegexp()) assert.Equal(t, "news|weather", config.Filters[1].Value) - assert.Equal(t, "exclude", config.Filters[1].Type) + assert.Equal(t, "group", config.Filters[1].Type) assert.NotNil(t, config.Filters[1].GetRegexp()) assert.Equal(t, 2*time.Hour, config.RefreshInterval) }) + t.Run("Valid Configuration without filter", func(t *testing.T) { + content := []byte(` +logLevel: info +iptvUrl: http://example.com/iptv +epgUrl: http://example.com/epg +listenAddress: localhost:8080 +serverAddress: iptvserver:8080 +refreshInterval: '2h' +ffmpeg: true +maxStreams: 10 +`) + + tmpfile, err := os.CreateTemp("", "config*.yaml") + if err != nil { + t.Fatalf("Failed to create temp file: %v", err) + } + defer os.Remove(tmpfile.Name()) + + if _, err := tmpfile.Write(content); err != nil { + t.Fatalf("Failed to write to temp file: %v", err) + } + if err := tmpfile.Close(); err != nil { + t.Fatalf("Failed to close temp file: %v", err) + } + + config, err := LoadConfig(tmpfile.Name()) + assert.NoError(t, err, "error loading config") + assert.NotNil(t, config) + + assert.Equal(t, "info", config.LogLevel) + assert.Equal(t, "http://example.com/iptv", config.IPTVUrl) + assert.Equal(t, "http://example.com/epg", config.EPGUrl) + assert.Equal(t, "localhost:8080", config.ListenAddress) + assert.Equal(t, "iptvserver:8080", config.ServerAddress) + assert.True(t, config.UseFFMPEG) + assert.Equal(t, 10, config.MaxStreams) + assert.Len(t, config.Filters, 1) + assert.Equal(t, ".*", config.Filters[0].Value) + assert.Equal(t, "name", config.Filters[0].Type) + assert.NotNil(t, config.Filters[0].GetRegexp()) + assert.Equal(t, 2*time.Hour, config.RefreshInterval) + }) + + // Test with invalid regular expression t.Run("Invalid Regular Expression", func(t *testing.T) { content := []byte(` @@ -71,9 +115,9 @@ epgUrl: http://example.com/iptv serverAddress: iptvserver:8080 filters: - filter: sports.* - type: include + type: name - filter: news[ - type: exclude + type: group `) tmpfile, err := os.CreateTemp("", "config*.yaml")