summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-11-27 21:21:29 +0200
committerPaul Buetow <paul@buetow.org>2024-11-27 21:21:29 +0200
commitf06a04e0f003c556c9273b2c4660b201d179598e (patch)
treebd68daf994f29ab40304aa0661dc9d378170b1f1 /internal
parentc07d0ae41c7a90628550bcbb79ff684f4dfc5a33 (diff)
refactoring tags
Diffstat (limited to 'internal')
-rw-r--r--internal/entry/entry.go19
-rw-r--r--internal/entry/entry_test.go10
-rw-r--r--internal/entry/sharetags.go42
-rw-r--r--internal/queue/queue.go8
-rw-r--r--internal/tags/inline.go (renamed from internal/queue/inlinetags.go)12
-rw-r--r--internal/tags/inline_test.go (renamed from internal/queue/inlinetags_test.go)10
-rw-r--r--internal/tags/share.go46
-rw-r--r--internal/tags/share_test.go (renamed from internal/entry/sharetags_test.go)57
-rw-r--r--internal/tags/tags.go1
9 files changed, 101 insertions, 104 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index 3e21328..70b8396 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -9,7 +9,6 @@ import (
"strings"
"time"
- "codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/oi"
"codeberg.org/snonux/gos/internal/prompt"
"codeberg.org/snonux/gos/internal/timestamp"
@@ -50,7 +49,7 @@ type Entry struct {
Path string
Time time.Time
State State
- tags map[string]struct{}
+ Tags map[string]struct{}
}
func (en Entry) String() string {
@@ -64,7 +63,7 @@ func (en Entry) String() string {
// or for inboxed: /foo.txt
// or inboxed with tags: /foo.prio.ask.txt
func New(filePath string) (Entry, error) {
- en := Entry{Path: filePath, tags: make(map[string]struct{})}
+ en := Entry{Path: filePath, Tags: make(map[string]struct{})}
// We want to get the STAMP!
parts := strings.Split(filePath, ".")
@@ -147,20 +146,10 @@ func (en *Entry) MarkPosted() error {
}
func (en Entry) HasTag(tag string) bool {
- _, ok := en.tags[tag]
+ _, ok := en.Tags[tag]
return ok
}
-// Valid tags are: share:foo[,...]
-// whereas foo can be a supported platform such as linkedin, mastodon, etc.
-// foo can also be prefixed with - to exclude it. See unit tests for examples.
-func (en Entry) PlatformExcluded(args config.Args, platformStr string) (bool, error) {
- s, err := newShareTags(args, en.tags)
- fmt.Println(s)
- return slices.Contains(s.excludes, platformStr) ||
- !slices.Contains(s.includes, platformStr), err
-}
-
func (en Entry) Edit() error {
if err := prompt.EditFile(en.Path); err != nil {
return err
@@ -184,7 +173,7 @@ func (en Entry) FileAction(question string) error {
func (en Entry) extractTags(parts []string) {
for _, part := range parts {
if slices.Contains(validTags, part) || strings.HasPrefix(part, "share:") {
- en.tags[part] = struct{}{}
+ en.Tags[part] = struct{}{}
}
}
}
diff --git a/internal/entry/entry_test.go b/internal/entry/entry_test.go
index 6a54b05..f9df524 100644
--- a/internal/entry/entry_test.go
+++ b/internal/entry/entry_test.go
@@ -52,12 +52,12 @@ func TestEntryTags(t *testing.T) {
for _, expectedTag := range strings.Split(tagsStr, ".") {
if expectedTag == "invalid" {
if en.HasTag(expectedTag) {
- t.Errorf("didn't expect tag '%s' to be present, but got '%v'", expectedTag, en.tags)
+ t.Errorf("didn't expect tag '%s' to be present, but got '%v'", expectedTag, en.Tags)
}
continue
}
if !en.HasTag(expectedTag) {
- t.Errorf("expected tag '%s' to be present, but got '%v'", expectedTag, en.tags)
+ t.Errorf("expected tag '%s' to be present, but got '%v'", expectedTag, en.Tags)
}
}
}
@@ -115,12 +115,12 @@ func TestHasTag(t *testing.T) {
if err != nil {
t.Error(err)
}
- if len(expectedTags) != len(en.tags) {
- t.Errorf("expected '%d' tags but got '%d'", len(expectedTags), len(en.tags))
+ if len(expectedTags) != len(en.Tags) {
+ t.Errorf("expected '%d' tags but got '%d'", len(expectedTags), len(en.Tags))
}
for _, tag := range expectedTags {
if !en.HasTag(tag) {
- t.Errorf("expected tag '%s' but got '%s'", tag, en.tags)
+ t.Errorf("expected tag '%s' but got '%s'", tag, en.Tags)
}
}
}
diff --git a/internal/entry/sharetags.go b/internal/entry/sharetags.go
deleted file mode 100644
index 3075e2f..0000000
--- a/internal/entry/sharetags.go
+++ /dev/null
@@ -1,42 +0,0 @@
-package entry
-
-import (
- "slices"
- "strings"
-
- "codeberg.org/snonux/gos/internal/config"
-)
-
-// TODO: Own package only dealing with tags, and put all tag code in there.
-type shareTags struct {
- includes []string // The platforms to include
- excludes []string // The platforms to exclude
-}
-
-func newShareTags(args config.Args, tags map[string]struct{}) (shareTags, error) {
- var s shareTags
-
- for tag := range tags {
- if !strings.HasPrefix(tag, "share:") {
- continue
- }
- for _, t := range strings.Split(tag[6:], ":") {
- if strings.HasPrefix(t, "-") {
- s.excludes = append(s.excludes, strings.ToLower(t[1:]))
- } else {
- s.includes = append(s.includes, strings.ToLower(t))
- }
- }
- }
-
- if len(s.includes) == 0 {
- for platformStr := range args.Platforms {
- if slices.Contains(s.excludes, strings.ToLower(platformStr)) {
- continue
- }
- s.includes = append(s.includes, strings.ToLower(platformStr))
- }
- }
-
- return s, nil
-}
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index 832b877..80f6dcd 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -12,6 +12,7 @@ import (
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/oi"
"codeberg.org/snonux/gos/internal/platforms"
+ "codeberg.org/snonux/gos/internal/tags"
"codeberg.org/snonux/gos/internal/timestamp"
)
@@ -36,7 +37,7 @@ func queueEntries(args config.Args) error {
}
for filePath := range ch {
- if filePath, err = extractInlineTags(filePath); err != nil {
+ if filePath, err = tags.InlineExtract(filePath); err != nil {
return err
}
en, err := entry.New(filePath)
@@ -82,11 +83,12 @@ func queuePlatforms(args config.Args) error {
if err != nil {
return err
}
- excluded, err := en.PlatformExcluded(args, platform.String())
+ // func NewShare(args config.Args, tags map[string]struct{}) (Share, error) {
+ share, err := tags.NewShare(args, en.Tags)
if err != nil {
return err
}
- if excluded {
+ if share.Excluded(platform.String()) {
colour.Infoln("Not queueing entry", en, "to platform", platform, "as it is excluded")
continue
}
diff --git a/internal/queue/inlinetags.go b/internal/tags/inline.go
index 82ee844..51277a5 100644
--- a/internal/queue/inlinetags.go
+++ b/internal/tags/inline.go
@@ -1,4 +1,4 @@
-package queue
+package tags
import (
"fmt"
@@ -15,13 +15,13 @@ import (
var inlineTagRE = regexp.MustCompile(`^[a-z\.,:]*$`)
// Extracts the inline tags into the filepath and removes them from the content.
-func extractInlineTags(filePath string) (string, error) {
+func InlineExtract(filePath string) (string, error) {
content, err := oi.SlurpAndTrim(filePath)
if err != nil {
return "", err
}
- newFilePath, newContent, err := extractInlineTagsToFilePath(filePath, content)
+ newFilePath, newContent, err := inlineExtractTagsToFilePath(filePath, content)
if err != nil {
return "", err
}
@@ -37,8 +37,8 @@ func extractInlineTags(filePath string) (string, error) {
return newFilePath, os.Remove(filePath)
}
-func extractInlineTagsToFilePath(filePath, content string) (string, string, error) {
- tags, newContent, err := extractInlineTagsFromContent(content)
+func inlineExtractTagsToFilePath(filePath, content string) (string, string, error) {
+ tags, newContent, err := inlineExtractTagsFromContent(content)
if err != nil {
return filePath, content, err
}
@@ -55,7 +55,7 @@ func extractInlineTagsToFilePath(filePath, content string) (string, string, erro
return newFilePath, newContent, nil
}
-func extractInlineTagsFromContent(content string) ([]string, string, error) {
+func inlineExtractTagsFromContent(content string) ([]string, string, error) {
parts := strings.Split(content, " ")
if inlineTagRE.MatchString(parts[0]) {
var tags []string
diff --git a/internal/queue/inlinetags_test.go b/internal/tags/inline_test.go
index a0d71b6..dab8b74 100644
--- a/internal/queue/inlinetags_test.go
+++ b/internal/tags/inline_test.go
@@ -1,4 +1,4 @@
-package queue
+package tags
import (
"slices"
@@ -6,7 +6,7 @@ import (
"testing"
)
-func TestExtractInlineTagsToFilePath(t *testing.T) {
+func TestInlineExtractTagsToFilePath(t *testing.T) {
const filePath = "./gosdir/foo.golang.rox.txt"
table := map[string]string{
@@ -21,7 +21,7 @@ func TestExtractInlineTagsToFilePath(t *testing.T) {
for content, expectedFilePath := range table {
t.Run(content, func(t *testing.T) {
- newFilePath, _, err := extractInlineTagsToFilePath(filePath, content)
+ newFilePath, _, err := inlineExtractTagsToFilePath(filePath, content)
if err != nil {
t.Error(err)
}
@@ -32,7 +32,7 @@ func TestExtractInlineTagsToFilePath(t *testing.T) {
}
}
-func TestExtractInlineTagsFromContent(t *testing.T) {
+func TestInlineExtractTagsFromContent(t *testing.T) {
table := map[string][]string{
"foo,bar,baz blablablabla...": {"foo", "bar", "baz"},
"foo.bar.baz blablablabla...": {"foo", "bar", "baz"},
@@ -45,7 +45,7 @@ func TestExtractInlineTagsFromContent(t *testing.T) {
for input, expectedTags := range table {
t.Run(input, func(t *testing.T) {
- tags, contentWithoutTags, err := extractInlineTagsFromContent(input)
+ tags, contentWithoutTags, err := inlineExtractTagsFromContent(input)
if err != nil {
t.Error(err)
}
diff --git a/internal/tags/share.go b/internal/tags/share.go
new file mode 100644
index 0000000..e58661a
--- /dev/null
+++ b/internal/tags/share.go
@@ -0,0 +1,46 @@
+package tags
+
+import (
+ "slices"
+ "strings"
+
+ "codeberg.org/snonux/gos/internal/config"
+)
+
+// Share tags.
+type Share struct {
+ Includes []string // The platforms to include
+ Excludes []string // The platforms to exclude
+}
+
+func NewShare(args config.Args, tags map[string]struct{}) (Share, error) {
+ var s Share
+
+ for tag := range tags {
+ if !strings.HasPrefix(tag, "share:") {
+ continue
+ }
+ for _, t := range strings.Split(tag[6:], ":") {
+ if strings.HasPrefix(t, "-") {
+ s.Excludes = append(s.Excludes, strings.ToLower(t[1:]))
+ } else {
+ s.Includes = append(s.Includes, strings.ToLower(t))
+ }
+ }
+ }
+
+ if len(s.Includes) == 0 {
+ for platformStr := range args.Platforms {
+ if slices.Contains(s.Excludes, strings.ToLower(platformStr)) {
+ continue
+ }
+ s.Includes = append(s.Includes, strings.ToLower(platformStr))
+ }
+ }
+
+ return s, nil
+}
+
+func (s Share) Excluded(platformStr string) bool {
+ return slices.Contains(s.Excludes, platformStr) || !slices.Contains(s.Includes, platformStr)
+}
diff --git a/internal/entry/sharetags_test.go b/internal/tags/share_test.go
index 73ba8db..f018593 100644
--- a/internal/entry/sharetags_test.go
+++ b/internal/tags/share_test.go
@@ -1,4 +1,4 @@
-package entry
+package tags
import (
"slices"
@@ -8,85 +8,86 @@ import (
"codeberg.org/snonux/gos/internal/config"
)
-func TestShareTagsPositive(t *testing.T) {
+func TestSharePositive(t *testing.T) {
args := config.Args{Platforms: map[string]int{
"mastodon": 100,
"linkedin": 100,
}}
- testTable := map[string]shareTags{
+ testTable := map[string]Share{
"./foo/bar.without.tags.txt.20240101-010101.queued": {
- includes: []string{"mastodon", "linkedin"},
+ Includes: []string{"mastodon", "linkedin"},
},
"./foo/bar.share:linkeDin.txt.20240101-010101.queued": {
- includes: []string{"linkedin"},
+ Includes: []string{"linkedin"},
},
"./foo/bar.share:-LinkedIn.txt.20240101-010101.queued": {
- includes: []string{"mastodon"},
- excludes: []string{"linkedin"},
+ Includes: []string{"mastodon"},
+ Excludes: []string{"linkedin"},
},
"./foo/bar.share:linkedin:mastOdon.txt.20240101-010101.queued": {
- includes: []string{"linkedin", "mastodon"},
+ Includes: []string{"linkedin", "mastodon"},
},
"./foo/bar.share:linkediN:-mastodon:XCOM.txt.20240101-010101.queued": {
- includes: []string{"linkedin", "xcom"},
- excludes: []string{"mastodon"},
+ Includes: []string{"linkedin", "xcom"},
+ Excludes: []string{"mastodon"},
},
"./foo/bar/ql-e7657e8a1ab573f84ad0dbc55199e937.share:-mastodon.txt.20241018-105524.queued": {
- includes: []string{"linkedin"},
- excludes: []string{"mastodon"},
+ Includes: []string{"linkedin"},
+ Excludes: []string{"mastodon"},
},
}
for filePath, expectedResult := range testTable {
t.Run(filePath, func(t *testing.T) {
- shareTags, err := newShareTags(args, filePathTags(filePath))
+ shareTags, err := NewShare(args, filePathTags(filePath))
if err != nil {
t.Error(err)
}
- if !sameElements(shareTags.includes, expectedResult.includes) {
+ if !sameElements(shareTags.Includes, expectedResult.Includes) {
t.Errorf("Expected includes to be %v but got %v with %s",
- expectedResult.includes, shareTags.includes, filePath)
+ expectedResult.Includes, shareTags.Includes, filePath)
}
- if !sameElements(shareTags.excludes, expectedResult.excludes) {
+ if !sameElements(shareTags.Excludes, expectedResult.Excludes) {
t.Errorf("Expected excludes to be %v but got %v with %s",
- expectedResult.excludes, shareTags.excludes, filePath)
+ expectedResult.Excludes, shareTags.Excludes, filePath)
}
})
}
}
-func TestShareTagsNegative(t *testing.T) {
+
+func TestShareNegative(t *testing.T) {
args := config.Args{Platforms: map[string]int{
string("mastodon"): 100,
string("linkedin"): 100,
}}
- testTable := map[string]shareTags{
+ testTable := map[string]Share{
"./foo/bar.without.tags.txt.20240101-010101.queued": {
- includes: []string{"linkedin"},
+ Includes: []string{"linkedin"},
},
"./foo/bar.share:linkedIn.txt.20240101-010101.queued": {
- includes: []string{"mastodon"},
+ Includes: []string{"mastodon"},
},
"./foo/bar.share:-liNkedin.txt.20240101-010101.queued": {
- includes: []string{"linkedin"},
+ Includes: []string{"linkedin"},
},
"./foo/bar.share:linkedin:mastodon.txt.20240101-010101.queued": {
- includes: []string{"oups", "mastodon"},
+ Includes: []string{"oups", "mastodon"},
},
"./foo/bar.share:linkedin:-MASTODON:xcom.txt.20240101-010101.queued": {
- includes: []string{"linkedin", "xcom"},
- excludes: []string{"mastodon", "xcom"},
+ Includes: []string{"linkedin", "xcom"},
+ Excludes: []string{"mastodon", "xcom"},
},
}
for filePath, unexpectedResult := range testTable {
t.Run(filePath, func(t *testing.T) {
- shareTags, err := newShareTags(args, filePathTags(filePath))
+ shareTags, err := NewShare(args, filePathTags(filePath))
if err != nil {
t.Error(err)
}
- if sameElements(shareTags.includes, unexpectedResult.includes) &&
- sameElements(shareTags.excludes, unexpectedResult.excludes) {
+ if sameElements(shareTags.Includes, unexpectedResult.Includes) &&
+ sameElements(shareTags.Excludes, unexpectedResult.Excludes) {
t.Errorf("expected %v not to be the actual result with %s",
unexpectedResult, filePath)
}
diff --git a/internal/tags/tags.go b/internal/tags/tags.go
new file mode 100644
index 0000000..2eee961
--- /dev/null
+++ b/internal/tags/tags.go
@@ -0,0 +1 @@
+package tags