summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/gos/main.go1
-rw-r--r--internal/config/args.go23
-rw-r--r--internal/entry/entry.go7
-rw-r--r--internal/entry/sharetags.go4
-rw-r--r--internal/entry/sharetags_test.go11
-rw-r--r--internal/platforms/platform.go32
-rw-r--r--internal/prompt/file.go5
-rw-r--r--internal/queue/inlinetags.go12
-rw-r--r--internal/queue/inlinetags_test.go5
-rw-r--r--internal/queue/queue.go5
-rw-r--r--internal/run.go6
-rw-r--r--internal/schedule/schedule.go5
-rw-r--r--internal/schedule/stats.go3
13 files changed, 85 insertions, 34 deletions
diff --git a/cmd/gos/main.go b/cmd/gos/main.go
index ee7aae7..36270a8 100644
--- a/cmd/gos/main.go
+++ b/cmd/gos/main.go
@@ -39,7 +39,6 @@ func main() {
args := config.Args{
DryRun: *dry,
GosDir: *gosDir,
- Platforms: make(map[string]int),
Target: *target,
MinQueued: *minQueued,
MaxDaysQueued: *maxDaysQueued,
diff --git a/internal/config/args.go b/internal/config/args.go
index a0fa0ea..d1d8fe8 100644
--- a/internal/config/args.go
+++ b/internal/config/args.go
@@ -8,6 +8,7 @@ import (
"time"
"codeberg.org/snonux/gos/internal/colour"
+ "codeberg.org/snonux/gos/internal/platforms"
)
var validPlatforms = []string{"mastodon", "linkedin"}
@@ -16,7 +17,7 @@ type Args struct {
GosDir string
CacheDir string
DryRun bool
- Platforms map[string]int // Platform name and post size limits
+ Platforms map[platforms.Platform]int // Platform and post size limits
Target int
MinQueued int
MaxDaysQueued int
@@ -27,20 +28,26 @@ type Args struct {
OAuth2Browser string
}
-func (a *Args) ParsePlatforms(platforms string) error {
- for _, platform := range strings.Split(platforms, ",") {
+func (a *Args) ParsePlatforms(platformStrs string) error {
+ a.Platforms = make(map[platforms.Platform]int)
+
+ for _, platformStr := range strings.Split(platformStrs, ",") {
// E.g. Mastodon:500
- parts := strings.Split(platform, ":")
- var err error
+ parts := strings.Split(platformStr, ":")
+ platform, err := platforms.New(parts[0])
+ if err != nil {
+ return err
+ }
+
// E.g. args.Platform["mastodon"] = 500
if len(parts) > 1 {
- a.Platforms[parts[0]], err = strconv.Atoi(parts[1])
+ a.Platforms[platform], err = strconv.Atoi(parts[1])
if err != nil {
return err
}
} else {
colour.Infoln("No message length specified for", platform, "so assuming 500")
- a.Platforms[parts[0]] = 500
+ a.Platforms[platform] = 500
}
}
return nil
@@ -48,7 +55,7 @@ func (a *Args) ParsePlatforms(platforms string) error {
func (a *Args) Validate() error {
for platform := range a.Platforms {
- if !slices.Contains(validPlatforms, strings.ToLower(platform)) {
+ if !slices.Contains(validPlatforms, platform.String()) {
return fmt.Errorf("Platform %s not supported", platform)
}
}
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index d289e83..e71f25b 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -11,6 +11,7 @@ import (
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/oi"
+ "codeberg.org/snonux/gos/internal/platforms"
"codeberg.org/snonux/gos/internal/prompt"
"codeberg.org/snonux/gos/internal/timestamp"
)
@@ -155,10 +156,10 @@ func (en Entry) HasTag(tag string) bool {
// 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 (en Entry) PlatformExcluded(args config.Args, platform string) (bool, error) {
+func (en Entry) PlatformExcluded(args config.Args, platform platforms.Platform) (bool, error) {
s, err := newShareTags(args, en.tags)
- return slices.Contains(s.excludes, strings.ToLower(platform)) ||
- !slices.Contains(s.includes, strings.ToLower(platform)), err
+ return slices.Contains(s.excludes, platform.String()) ||
+ !slices.Contains(s.includes, platform.String()), err
}
func (en Entry) Edit() error {
diff --git a/internal/entry/sharetags.go b/internal/entry/sharetags.go
index c0b855a..666a3d4 100644
--- a/internal/entry/sharetags.go
+++ b/internal/entry/sharetags.go
@@ -30,10 +30,10 @@ func newShareTags(args config.Args, tags map[string]struct{}) (shareTags, error)
if len(s.includes) == 0 {
for platform := range args.Platforms {
- if slices.Contains(s.excludes, strings.ToLower(platform)) {
+ if slices.Contains(s.excludes, platform.String()) {
continue
}
- s.includes = append(s.includes, strings.ToLower(platform))
+ s.includes = append(s.includes, platform.String())
}
}
diff --git a/internal/entry/sharetags_test.go b/internal/entry/sharetags_test.go
index 60589f5..7b1d742 100644
--- a/internal/entry/sharetags_test.go
+++ b/internal/entry/sharetags_test.go
@@ -6,10 +6,14 @@ import (
"testing"
"codeberg.org/snonux/gos/internal/config"
+ "codeberg.org/snonux/gos/internal/platforms"
)
func TestShareTagsPositive(t *testing.T) {
- args := config.Args{Platforms: map[string]int{"mastodon": 100, "linkedin": 100}}
+ args := config.Args{Platforms: map[platforms.Platform]int{
+ platforms.Platform("mastodon"): 100,
+ platforms.Platform("linkedin"): 100,
+ }}
testTable := map[string]shareTags{
"./foo/bar.without.tags.txt.20240101-010101.queued": {
includes: []string{"mastodon", "linkedin"},
@@ -53,7 +57,10 @@ func TestShareTagsPositive(t *testing.T) {
}
}
func TestShareTagsNegative(t *testing.T) {
- args := config.Args{Platforms: map[string]int{"mastodon": 100, "linkedin": 100}}
+ args := config.Args{Platforms: map[platforms.Platform]int{
+ platforms.Platform("mastodon"): 100,
+ platforms.Platform("linkedin"): 100,
+ }}
testTable := map[string]shareTags{
"./foo/bar.without.tags.txt.20240101-010101.queued": {
includes: []string{"linkedin"},
diff --git a/internal/platforms/platform.go b/internal/platforms/platform.go
new file mode 100644
index 0000000..f899357
--- /dev/null
+++ b/internal/platforms/platform.go
@@ -0,0 +1,32 @@
+package platforms
+
+import (
+ "fmt"
+ "strings"
+)
+
+type Platform string
+
+var aliases = map[string]string{
+ "linkedin": "linkedin",
+ "li": "linkedin",
+ "mastodon": "mastodon",
+ "ma": "mastodon",
+ "xcom": "xcom",
+ "x": "xcom",
+ "twitter": "xcom",
+ "tw": "xcom",
+}
+
+func New(platformStr string) (Platform, error) {
+ var p Platform
+ name, ok := aliases[strings.ToLower(platformStr)]
+ if !ok {
+ return p, fmt.Errorf("no such platform: '%s'", platformStr)
+ }
+ return Platform(name), nil
+}
+
+func (p Platform) String() string {
+ return string(p)
+}
diff --git a/internal/prompt/file.go b/internal/prompt/file.go
index 5d7212b..9668aed 100644
--- a/internal/prompt/file.go
+++ b/internal/prompt/file.go
@@ -17,11 +17,6 @@ var (
ErrDeleted = errors.New("deleted")
)
-// type entry interface {
-// Path() string
-// ContentWithLimit(int) (string, error)
-// }
-
// TODO: Add option to randomly select another entry when no selected?
func FileAction(question, content, filePath string) (string, error) {
colour.Info2f(filePath + ":")
diff --git a/internal/queue/inlinetags.go b/internal/queue/inlinetags.go
index bf147b1..11f05c2 100644
--- a/internal/queue/inlinetags.go
+++ b/internal/queue/inlinetags.go
@@ -10,6 +10,7 @@ import (
"codeberg.org/snonux/gos/internal/oi"
)
+// TODO: Don't treat it as inline tags when there are other characters tan [a-z,.]
// Extracts the inline tags into the filepath and removes them from the content.
func extractInlineTags(filePath string) (string, error) {
content, err := oi.SlurpAndTrim(filePath)
@@ -34,7 +35,10 @@ func extractInlineTags(filePath string) (string, error) {
}
func extractInlineTagsToFilePath(filePath, content string) (string, string, error) {
- tags, newContent := extractInlineTagsFromContent(content)
+ tags, newContent, err := extractInlineTagsFromContent(content)
+ if err != nil {
+ return filePath, content, err
+ }
if len(tags) == 0 {
return filePath, content, nil
}
@@ -48,7 +52,7 @@ func extractInlineTagsToFilePath(filePath, content string) (string, string, erro
return newFilePath, newContent, nil
}
-func extractInlineTagsFromContent(content string) ([]string, string) {
+func extractInlineTagsFromContent(content string) ([]string, string, error) {
parts := strings.Split(content, " ")
// If the first word of the content contains a dot or comma and there are
// more than 2 elems, then there are inline tags!
@@ -58,8 +62,8 @@ func extractInlineTagsFromContent(content string) ([]string, string) {
tags = append(tags, strings.Split(elem, ",")...)
}
if len(tags) > 1 {
- return tags, strings.Join(parts[1:], " ")
+ return tags, strings.Join(parts[1:], " "), nil
}
}
- return []string{}, content
+ return []string{}, content, nil
}
diff --git a/internal/queue/inlinetags_test.go b/internal/queue/inlinetags_test.go
index 5b22fe6..8b85cb2 100644
--- a/internal/queue/inlinetags_test.go
+++ b/internal/queue/inlinetags_test.go
@@ -41,7 +41,10 @@ func TestExtractInlineTagsFromContent(t *testing.T) {
for input, expectedTags := range table {
t.Run(input, func(t *testing.T) {
- tags, contentWithoutTags := extractInlineTagsFromContent(input)
+ tags, contentWithoutTags, err := extractInlineTagsFromContent(input)
+ if err != nil {
+ t.Error(err)
+ }
if len(tags) != len(expectedTags) {
t.Errorf("expected %d inline tags (%v) but got %d (%v)",
len(expectedTags), expectedTags, len(tags), tags)
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index 40cc084..aa63c71 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -11,6 +11,7 @@ import (
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/oi"
+ "codeberg.org/snonux/gos/internal/platforms"
"codeberg.org/snonux/gos/internal/timestamp"
)
@@ -110,8 +111,8 @@ func queuePlatforms(args config.Args) error {
}
// Queue ./db/queued/*.txt.STAMP.queued to ./db/platforms/PLATFORM/*.txt.STAMP.queued
-func queuePlatform(en entry.Entry, gosDir, platform string) error {
- destDir := filepath.Join(gosDir, "db/platforms", strings.ToLower(platform))
+func queuePlatform(en entry.Entry, gosDir string, platform platforms.Platform) error {
+ destDir := filepath.Join(gosDir, "db/platforms", platform.String())
destPath := filepath.Join(destDir, filepath.Base(en.Path))
postedFile := fmt.Sprintf("%s.posted", strings.TrimSuffix(destPath, ".queued"))
diff --git a/internal/run.go b/internal/run.go
index a171ec7..b143cca 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -4,11 +4,11 @@ import (
"context"
"errors"
"log"
- "strings"
"codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
+ "codeberg.org/snonux/gos/internal/platforms"
"codeberg.org/snonux/gos/internal/platforms/linkedin"
"codeberg.org/snonux/gos/internal/platforms/mastodon"
"codeberg.org/snonux/gos/internal/prompt"
@@ -37,7 +37,7 @@ func Run(ctx context.Context, args config.Args) error {
return nil
}
-func runPlatform(ctx context.Context, args config.Args, platform string, sizeLimit int) error {
+func runPlatform(ctx context.Context, args config.Args, platform platforms.Platform, sizeLimit int) error {
en, err := schedule.Run(args, platform)
switch {
case errors.Is(err, schedule.ErrNothingToSchedule):
@@ -52,7 +52,7 @@ func runPlatform(ctx context.Context, args config.Args, platform string, sizeLim
colour.Infoln("Posting", en)
var postCB func(context.Context, config.Args, int, entry.Entry) error
- switch strings.ToLower(platform) {
+ switch platform.String() {
case "mastodon":
postCB = mastodon.Post
case "linkedin":
diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go
index 4f25fb4..9b31f88 100644
--- a/internal/schedule/schedule.go
+++ b/internal/schedule/schedule.go
@@ -12,6 +12,7 @@ import (
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/oi"
+ "codeberg.org/snonux/gos/internal/platforms"
"codeberg.org/snonux/gos/internal/prompt"
)
@@ -20,8 +21,8 @@ var (
ErrNothingQueued = errors.New("nothing queued")
)
-func Run(args config.Args, platform string) (entry.Entry, error) {
- dir := fmt.Sprintf("%s/db/platforms/%s", args.GosDir, strings.ToLower(platform))
+func Run(args config.Args, platform platforms.Platform) (entry.Entry, error) {
+ dir := fmt.Sprintf("%s/db/platforms/%s", args.GosDir, platform.String())
stats, err := newStats(dir, args.Lookback, args.Target)
if err != nil {
return entry.Zero, err
diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go
index 1cbc4ec..42f9332 100644
--- a/internal/schedule/stats.go
+++ b/internal/schedule/stats.go
@@ -11,6 +11,7 @@ import (
"codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/oi"
+ "codeberg.org/snonux/gos/internal/platforms"
"codeberg.org/snonux/gos/internal/timestamp"
)
@@ -44,7 +45,7 @@ func (s stats) String() string {
)
}
-func (s stats) Render(platform string) {
+func (s stats) Render(platform platforms.Platform) {
var sb strings.Builder
sep := colour.SInfo2f("+%s+%s+", strings.Repeat("-", 22), strings.Repeat("-", 13))