diff options
| -rw-r--r-- | internal/queue/queue.go | 3 | ||||
| -rw-r--r-- | internal/queue/sharetags.go | 40 | ||||
| -rw-r--r-- | internal/queue/sharetags_test.go | 84 |
3 files changed, 127 insertions, 0 deletions
diff --git a/internal/queue/queue.go b/internal/queue/queue.go index fcef897..adc16e3 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -16,6 +16,9 @@ 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 diff --git a/internal/queue/sharetags.go b/internal/queue/sharetags.go new file mode 100644 index 0000000..90036d7 --- /dev/null +++ b/internal/queue/sharetags.go @@ -0,0 +1,40 @@ +package queue + +import ( + "strings" + + "codeberg.org/snonux/gos/internal/config" +) + +type shareTags struct { + // The platforms to include + includes []string + // The platforms to exclude + excludes []string +} + +// 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 { + var s shareTags + + parts := strings.Split(filePath, ".") + tagStr := parts[len(parts)-2] + if len(parts) > 2 && strings.HasPrefix(tagStr, "share:") { + for _, tag := range strings.Split(tagStr[6:], ":") { + if strings.HasPrefix(tag, "-") { + s.excludes = append(s.excludes, tag[1:]) + } else { + s.includes = append(s.includes, tag) + } + } + } + + if len(s.includes) == 0 && len(s.excludes) == 0 { + // If nothing found, include all of them + s.includes = args.Platforms + } + + return s +} diff --git a/internal/queue/sharetags_test.go b/internal/queue/sharetags_test.go new file mode 100644 index 0000000..05ba449 --- /dev/null +++ b/internal/queue/sharetags_test.go @@ -0,0 +1,84 @@ +package queue + +import ( + "slices" + "testing" + + "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{ + "./foo/bar.without.tags.txt": { + includes: args.Platforms, // No tags: default platforms + }, + "./foo/bar.share:linkedin.txt": { + includes: []string{"linkedin"}, + }, + "./foo/bar.share:-linkedin.txt": { + excludes: []string{"linkedin"}, + }, + "./foo/bar.share:linkedin:mastodon.txt": { + includes: []string{"linkedin", "mastodon"}, + }, + "./foo/bar.share:linkedin:-mastodon:xcom.txt": { + includes: []string{"linkedin", "xcom"}, + excludes: []string{"mastodon"}, + }, + } + + for filePath, expectedResult := range testTable { + t.Run(filePath, func(t *testing.T) { + shareTags := newShareTags(args, filePath) + if !slices.Equal(shareTags.includes, expectedResult.includes) { + t.Errorf("Expected includes to be %v but got %v", expectedResult.includes, shareTags.includes) + } + if !slices.Equal(shareTags.excludes, expectedResult.excludes) { + t.Errorf("Expected excludes to be %v but got %v", expectedResult.excludes, shareTags.excludes) + } + }) + + } +} +func TestShareTagsNegative(t *testing.T) { + t.Parallel() + + args := config.Args{Platforms: []string{"mastodon", "linkedin"}} + testTable := map[string]expectedResult{ + "./foo/bar.without.tags.txt": { + includes: []string{"linkedin"}, + }, + "./foo/bar.share:linkedin.txt": { + includes: []string{"mastodon"}, + }, + "./foo/bar.share:-linkedin.txt": { + includes: []string{"linkedin"}, + }, + "./foo/bar.share:linkedin:mastodon.txt": { + includes: []string{"oups", "mastodon"}, + }, + "./foo/bar.share:linkedin:-mastodon:xcom.txt": { + includes: []string{"linkedin", "xcom"}, + excludes: []string{"mastodon", "xcom"}, + }, + } + + for filePath, unexpectedResult := range testTable { + t.Run(filePath, func(t *testing.T) { + shareTags := newShareTags(args, filePath) + if slices.Equal(shareTags.includes, unexpectedResult.includes) && + slices.Equal(shareTags.excludes, unexpectedResult.excludes) { + t.Errorf("expected %v not to be the actual result", unexpectedResult) + } + }) + + } +} |
