diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-18 11:23:44 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-18 11:23:44 +0300 |
| commit | fceb1eb4e6be044931fd0674fa587f9702dd1f65 (patch) | |
| tree | a9a51a8b8b914bdf5dbe5a87555eb3c6bede026a /internal/queue | |
| parent | 564e56404e5aecee3924e957adbe95bbb5880676 (diff) | |
refactor
Diffstat (limited to 'internal/queue')
| -rw-r--r-- | internal/queue/queue.go | 6 | ||||
| -rw-r--r-- | internal/queue/sharetags.go | 28 | ||||
| -rw-r--r-- | internal/queue/sharetags_test.go | 10 |
3 files changed, 26 insertions, 18 deletions
diff --git a/internal/queue/queue.go b/internal/queue/queue.go index caf4f2e..0724f7b 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -66,7 +66,11 @@ func queuePlatforms(args config.Args) error { for filePath := range ch { for platform := range args.Platforms { - if newShareTags(args, filePath).IsExcluded(platform) { + excluded, err := excludedByTags(args, filePath, platform) + if err != nil { + return err + } + if excluded { log.Println("Not queueing entry", filePath, "to platform", platform, "as it is excluded") continue } diff --git a/internal/queue/sharetags.go b/internal/queue/sharetags.go index 79a94d8..044d6bd 100644 --- a/internal/queue/sharetags.go +++ b/internal/queue/sharetags.go @@ -1,29 +1,27 @@ package queue import ( - "log" + "fmt" "slices" "strings" "codeberg.org/snonux/gos/internal/config" ) -// TODO: Refactor this file, maybe a simple function is enough not a newShareTags - type shareTags struct { includes []string // The platforms to include excludes []string // The platforms to exclude } -// Valid tags are: share:foo[,...] -// whereas foo can be a supported plutform such as linkedin, mastodon, etc. -// foo can also be prefixed with - to exclude it. See unit tests for examples. -func newShareTags(args config.Args, filePath string) shareTags { +func newShareTags(args config.Args, filePath string) (shareTags, error) { var s shareTags parts := strings.Split(filePath, ".") - // TODO: Defensively test whether the parts is long enough + if len(parts) < 4 { + return s, fmt.Errorf("invalid file path: %s", filePath) + } tagStr := parts[len(parts)-4] + if len(parts) > 2 && strings.HasPrefix(tagStr, "share:") { for _, tag := range strings.Split(tagStr[6:], ":") { if strings.HasPrefix(tag, "-") { @@ -43,14 +41,14 @@ func newShareTags(args config.Args, filePath string) shareTags { } } - return s + return s, nil } -// TODO: Write unit test -func (s shareTags) IsExcluded(platform string) bool { - log.Println("DEBUG", platform, s) - log.Println("DEBUG is it in the excludes", slices.Contains(s.excludes, strings.ToLower(platform))) - log.Println("DEBUG is it not in the includes", !slices.Contains(s.includes, strings.ToLower(platform))) +// Valid tags are: share:foo[,...] +// whereas foo can be a supported plutform such as linkedin, mastodon, etc. +// foo can also be prefixed with - to exclude it. See unit tests for examples. +func excludedByTags(args config.Args, filePath, platform string) (bool, error) { + s, err := newShareTags(args, filePath) return slices.Contains(s.excludes, strings.ToLower(platform)) || - !slices.Contains(s.includes, strings.ToLower(platform)) + !slices.Contains(s.includes, strings.ToLower(platform)), err } diff --git a/internal/queue/sharetags_test.go b/internal/queue/sharetags_test.go index 3aa6b58..0b43d99 100644 --- a/internal/queue/sharetags_test.go +++ b/internal/queue/sharetags_test.go @@ -37,7 +37,10 @@ func TestShareTagsPositive(t *testing.T) { for filePath, expectedResult := range testTable { t.Run(filePath, func(t *testing.T) { - shareTags := newShareTags(args, filePath) + shareTags, err := newShareTags(args, filePath) + if err != nil { + t.Error(err) + } if !sameElements(shareTags.includes, expectedResult.includes) { t.Errorf("Expected includes to be %v but got %v with %s", expectedResult.includes, shareTags.includes, filePath) @@ -75,7 +78,10 @@ func TestShareTagsNegative(t *testing.T) { for filePath, unexpectedResult := range testTable { t.Run(filePath, func(t *testing.T) { - shareTags := newShareTags(args, filePath) + shareTags, err := newShareTags(args, filePath) + if err != nil { + t.Error(err) + } if sameElements(shareTags.includes, unexpectedResult.includes) && sameElements(shareTags.excludes, unexpectedResult.excludes) { t.Errorf("expected %v not to be the actual result with %s", |
