diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-15 00:11:00 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-15 00:11:00 +0300 |
| commit | a66b2114ee9fb9078d6f3c12566d7178e0662903 (patch) | |
| tree | 2c5074a140daaa83748d746bc3b199314caa521b /internal/queue | |
| parent | 439cd968fd2bd171bcf1d0f527e86e8584ba85b1 (diff) | |
implemented share tags
Diffstat (limited to 'internal/queue')
| -rw-r--r-- | internal/queue/queue.go | 7 | ||||
| -rw-r--r-- | internal/queue/sharetags.go | 15 | ||||
| -rw-r--r-- | internal/queue/sharetags_test.go | 68 |
3 files changed, 76 insertions, 14 deletions
diff --git a/internal/queue/queue.go b/internal/queue/queue.go index adc16e3..73d5789 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -16,9 +16,6 @@ import ( // Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files. var validExtensions = []string{".txt", ".md"} -// TODO: add support of parsing the share tags -// sdlfkjsdflksdjfsldfkj.share:linkedin:-mastodon:x.txt -// etc func Run(args config.Args) error { if err := queueEntries(args); err != nil { return err @@ -69,6 +66,10 @@ func queuePlatforms(args config.Args) error { for filePath := range ch { for _, platform := range args.Platforms { + if newShareTags(args, filePath).IsExcluded(platform) { + log.Println("Not queueing entry", filePath, "to platform", platform, "as it is excluded") + continue + } if err := queuePlatform(filePath, args.GosDir, platform); err != nil { return err } diff --git a/internal/queue/sharetags.go b/internal/queue/sharetags.go index 90036d7..53d6306 100644 --- a/internal/queue/sharetags.go +++ b/internal/queue/sharetags.go @@ -1,16 +1,15 @@ package queue import ( + "slices" "strings" "codeberg.org/snonux/gos/internal/config" ) type shareTags struct { - // The platforms to include - includes []string - // The platforms to exclude - excludes []string + includes []string // The platforms to include + excludes []string // The platforms to exclude } // Valid tags are: share:foo[,...] @@ -38,3 +37,11 @@ func newShareTags(args config.Args, filePath string) shareTags { return s } + +func (s shareTags) IsIncluded(platform string) bool { + return slices.Contains(s.includes, platform) && !slices.Contains(s.excludes, platform) +} + +func (s shareTags) IsExcluded(platform string) bool { + return slices.Contains(s.excludes, platform) || !slices.Contains(s.includes, platform) +} diff --git a/internal/queue/sharetags_test.go b/internal/queue/sharetags_test.go index 05ba449..b2dbeec 100644 --- a/internal/queue/sharetags_test.go +++ b/internal/queue/sharetags_test.go @@ -7,16 +7,11 @@ import ( "codeberg.org/snonux/gos/internal/config" ) -type expectedResult struct { - includes []string - excludes []string -} - func TestShareTagsPositive(t *testing.T) { t.Parallel() args := config.Args{Platforms: []string{"mastodon", "linkedin"}} - testTable := map[string]expectedResult{ + testTable := map[string]shareTags{ "./foo/bar.without.tags.txt": { includes: args.Platforms, // No tags: default platforms }, @@ -52,7 +47,7 @@ func TestShareTagsNegative(t *testing.T) { t.Parallel() args := config.Args{Platforms: []string{"mastodon", "linkedin"}} - testTable := map[string]expectedResult{ + testTable := map[string]shareTags{ "./foo/bar.without.tags.txt": { includes: []string{"linkedin"}, }, @@ -82,3 +77,62 @@ func TestShareTagsNegative(t *testing.T) { } } + +func TestShareTagsIsIncluded(t *testing.T) { + t.Parallel() + + assertIncluded := func(shareTags shareTags, platforms ...string) { + for _, platform := range platforms { + if !shareTags.IsIncluded(platform) { + t.Errorf("expected %s included in %v", platform, shareTags) + } + if shareTags.IsExcluded(platform) { + t.Errorf("expected %s not to be excluded in %v", platform, shareTags) + } + } + } + assertExcluded := func(shareTags shareTags, platforms ...string) { + for _, platform := range platforms { + if shareTags.IsIncluded(platform) { + t.Errorf("expected %s not to be included in %v", platform, shareTags) + } + if !shareTags.IsExcluded(platform) { + t.Errorf("expected %s to be excluded in %v", platform, shareTags) + } + } + } + args := config.Args{Platforms: []string{"mastodon", "linkedin"}} + + filePath := "foo/bar/baz.txt" + t.Run(filePath, func(t *testing.T) { + assertIncluded(newShareTags(args, filePath), "mastodon", "linkedin") + }) + + filePath = "foo/bar/baz.share:mastodon.txt" + t.Run(filePath, func(t *testing.T) { + assertIncluded(newShareTags(args, filePath), "mastodon") + assertExcluded(newShareTags(args, filePath), "linkedin") + }) + + filePath = "foo/bar/baz.share:mastodon.txt" + t.Run(filePath, func(t *testing.T) { + assertIncluded(newShareTags(args, filePath), "mastodon") + assertExcluded(newShareTags(args, filePath), "linkedin") + }) + + filePath = "foo/bar/baz.share:linkedin:mastodon.txt" + t.Run(filePath, func(t *testing.T) { + assertIncluded(newShareTags(args, filePath), "mastodon", "linkedin") + }) + + filePath = "foo/bar/baz.share:-linkedin:-mastodon.txt" + t.Run(filePath, func(t *testing.T) { + assertExcluded(newShareTags(args, filePath), "mastodon", "linkedin") + }) + + filePath = "foo/bar/baz.share:-linkedin:mastodon.txt" + t.Run(filePath, func(t *testing.T) { + assertIncluded(newShareTags(args, filePath), "mastodon") + assertExcluded(newShareTags(args, filePath), "linkedin") + }) +} |
