summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-31 22:50:00 +0200
committerPaul Buetow <paul@buetow.org>2024-10-31 22:50:00 +0200
commitedfbee2da241372271d2a32cff8ab7409e910727 (patch)
tree2d3abe6ce6187661cb90f0620751f4821b1552c0 /internal
parent84e72c59d507cbd7347465c38044e9199ee7903a (diff)
restructure
Diffstat (limited to 'internal')
-rw-r--r--internal/entry/entry.go20
-rw-r--r--internal/entry/sharetags.go (renamed from internal/queue/sharetags.go)11
-rw-r--r--internal/entry/sharetags_test.go (renamed from internal/queue/sharetags_test.go)2
-rw-r--r--internal/queue/queue.go37
4 files changed, 37 insertions, 33 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index be3bb80..d757632 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -9,6 +9,7 @@ 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"
@@ -18,7 +19,7 @@ type State int
const (
Unknown State = iota
- Inboxed // TODO: Implement
+ Inboxed
Queued
Posted
)
@@ -152,6 +153,15 @@ func (e Entry) HasTag(tag string) bool {
return slices.Contains(e.simpleTags, tag)
}
+// 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 (e Entry) ExcludedByTags(args config.Args, platform string) (bool, error) {
+ s, err := newShareTags(args, e.Path)
+ return slices.Contains(s.excludes, strings.ToLower(platform)) ||
+ !slices.Contains(s.includes, strings.ToLower(platform)), err
+}
+
func (e Entry) Edit() error {
if err := prompt.EditFile(e.Path); err != nil {
return err
@@ -163,6 +173,14 @@ func (e Entry) Remove() error {
return os.Remove(e.Path)
}
+func (e Entry) FileAction(question string) error {
+ content, _, err := e.Content()
+ if err != nil {
+ return err
+ }
+ return prompt.FileAction(question, content, e.Path)
+}
+
func extractURLs(input string) []string {
urlPattern := `(http://|https://|ftp://)[^\s]+`
re := regexp.MustCompile(urlPattern)
diff --git a/internal/queue/sharetags.go b/internal/entry/sharetags.go
index 044d6bd..4a7f0cf 100644
--- a/internal/queue/sharetags.go
+++ b/internal/entry/sharetags.go
@@ -1,4 +1,4 @@
-package queue
+package entry
import (
"fmt"
@@ -43,12 +43,3 @@ func newShareTags(args config.Args, filePath string) (shareTags, error) {
return s, nil
}
-
-// 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)), err
-}
diff --git a/internal/queue/sharetags_test.go b/internal/entry/sharetags_test.go
index d13c79e..ba97842 100644
--- a/internal/queue/sharetags_test.go
+++ b/internal/entry/sharetags_test.go
@@ -1,4 +1,4 @@
-package queue
+package entry
import (
"slices"
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index 74ba00f..6df59df 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -12,7 +12,6 @@ import (
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/oi"
- "codeberg.org/snonux/gos/internal/prompt"
"codeberg.org/snonux/gos/internal/timestamp"
)
@@ -48,20 +47,16 @@ func queueEntries(args config.Args) error {
return err
}
if ent.HasTag("ask") {
- content, _, err := ent.Content()
- if err != nil {
- return err
- }
- if err := prompt.FileAction("Do you want to queue this content", content, ent.Path); err != nil {
+ if err := ent.FileAction("Do you want to queue this content"); err != nil {
return err
}
}
- destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir, filepath.Base(filePath), timestamp.Now())
+ destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir, filepath.Base(ent.Path), timestamp.Now())
if args.DryRun {
- log.Println("Not queueing entry", filePath, "to", destPath, "as dry-run mode enabled")
+ log.Println("Not queueing entry", ent.Path, "to", destPath, "as dry-run mode enabled")
continue
}
- if err := oi.Rename(filePath, destPath); err != nil {
+ if err := oi.Rename(ent.Path, destPath); err != nil {
return err
}
}
@@ -80,21 +75,20 @@ func queuePlatforms(args config.Args) error {
trashDir := filepath.Join(args.GosDir, "db", "trashbin")
for filePath := range ch {
- _, err := entry.New(filePath)
+ ent, err := entry.New(filePath)
if err != nil {
return err
}
for platform := range args.Platforms {
- // TODO: Move excludedByTags to entry.Entry.IsSared(args, platform)
- excluded, err := excludedByTags(args, filePath, platform)
+ excluded, err := ent.ExcludedByTags(args, platform)
if err != nil {
return err
}
if excluded {
- log.Println("Not queueing entry", filePath, "to platform", platform, "as it is excluded")
+ log.Println("Not queueing entry", ent, "to platform", platform, "as it is excluded")
continue
}
- if err := queuePlatform(filePath, args.GosDir, platform); err != nil {
+ if err := queuePlatform(ent, args.GosDir, platform); err != nil {
return err
}
}
@@ -103,12 +97,12 @@ func queuePlatforms(args config.Args) error {
}
// Keep queued items in trash for a while.
- trashPath := filepath.Join(trashDir, strings.TrimSuffix(filepath.Base(filePath), ".queued")+".trash")
- log.Printf("Trashing %s -> %s", filePath, trashPath)
+ trashPath := filepath.Join(trashDir, strings.TrimSuffix(filepath.Base(ent.Path), ".queued")+".trash")
+ log.Printf("Trashing %s -> %s", ent.Path, trashPath)
if err := oi.EnsureParentDir(trashPath); err != nil {
return err
}
- if err := os.Rename(filePath, trashPath); err != nil {
+ if err := os.Rename(ent.Path, trashPath); err != nil {
return err
}
}
@@ -118,9 +112,10 @@ func queuePlatforms(args config.Args) error {
}
// Queue ./db/queued/*.txt.STAMP.queued to ./db/platforms/PLATFORM/*.txt.STAMP.queued
-func queuePlatform(entryPath, gosDir, platform string) error {
+// TODO: Rename all ent to en
+func queuePlatform(ent entry.Entry, gosDir, platform string) error {
destDir := filepath.Join(gosDir, "db/platforms", strings.ToLower(platform))
- destPath := filepath.Join(destDir, filepath.Base(entryPath))
+ destPath := filepath.Join(destDir, filepath.Base(ent.Path))
postedFile := fmt.Sprintf("%s.posted", strings.TrimSuffix(destPath, ".queued"))
// Entry already posted platform?
@@ -129,8 +124,8 @@ func queuePlatform(entryPath, gosDir, platform string) error {
return nil
}
- log.Println("Queuing", entryPath, "->", destPath)
- return oi.CopyFile(entryPath, destPath)
+ log.Println("Queuing", ent.Path, "->", destPath)
+ return oi.CopyFile(ent.Path, destPath)
}
func deleteFiles(path, suffix string, olderThan time.Time) error {