From c095b7fb273eed04cae0d42cae2d534cf74cfd9e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 22 Nov 2024 23:48:28 +0200 Subject: some stuff all around --- internal/config/args.go | 34 +++++++++------------------------- internal/entry/entry.go | 7 +++---- internal/entry/sharetags.go | 6 +++--- internal/entry/sharetags_test.go | 13 ++++++------- internal/platforms/platform.go | 29 +++++++++++++++++++++++++++++ internal/queue/queue.go | 8 ++++++-- internal/run.go | 36 ++++++------------------------------ 7 files changed, 62 insertions(+), 71 deletions(-) (limited to 'internal') diff --git a/internal/config/args.go b/internal/config/args.go index d1d8fe8..215c634 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -1,23 +1,18 @@ package config import ( - "fmt" - "slices" "strconv" "strings" "time" "codeberg.org/snonux/gos/internal/colour" - "codeberg.org/snonux/gos/internal/platforms" ) -var validPlatforms = []string{"mastodon", "linkedin"} - type Args struct { GosDir string CacheDir string DryRun bool - Platforms map[platforms.Platform]int // Platform and post size limits + Platforms map[string]int // Platform and post size limits Target int MinQueued int MaxDaysQueued int @@ -29,34 +24,23 @@ type Args struct { } func (a *Args) ParsePlatforms(platformStrs string) error { - a.Platforms = make(map[platforms.Platform]int) + a.Platforms = make(map[string]int) - for _, platformStr := range strings.Split(platformStrs, ",") { + for _, platformInfo := range strings.Split(platformStrs, ",") { // E.g. Mastodon:500 - parts := strings.Split(platformStr, ":") - platform, err := platforms.New(parts[0]) - if err != nil { - return err - } + parts := strings.Split(platformInfo, ":") + platformStr := parts[0] // E.g. args.Platform["mastodon"] = 500 if len(parts) > 1 { - a.Platforms[platform], err = strconv.Atoi(parts[1]) + var err error + a.Platforms[platformStr], err = strconv.Atoi(parts[1]) if err != nil { return err } } else { - colour.Infoln("No message length specified for", platform, "so assuming 500") - a.Platforms[platform] = 500 - } - } - return nil -} - -func (a *Args) Validate() error { - for platform := range a.Platforms { - if !slices.Contains(validPlatforms, platform.String()) { - return fmt.Errorf("Platform %s not supported", platform) + colour.Infoln("No message length specified for", platformStr, "so assuming 500") + a.Platforms[platformStr] = 500 } } return nil diff --git a/internal/entry/entry.go b/internal/entry/entry.go index 25fb44a..d767410 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -11,7 +11,6 @@ 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 +154,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 platforms.Platform) (bool, error) { +func (en Entry) PlatformExcluded(args config.Args, platformStr string) (bool, error) { s, err := newShareTags(args, en.tags) - return slices.Contains(s.excludes, platform.String()) || - !slices.Contains(s.includes, platform.String()), err + return slices.Contains(s.excludes, platformStr) || + !slices.Contains(s.includes, platformStr), err } func (en Entry) Edit() error { diff --git a/internal/entry/sharetags.go b/internal/entry/sharetags.go index 666a3d4..21421ab 100644 --- a/internal/entry/sharetags.go +++ b/internal/entry/sharetags.go @@ -29,11 +29,11 @@ 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, platform.String()) { + for platformStr := range args.Platforms { + if slices.Contains(s.excludes, platformStr) { continue } - s.includes = append(s.includes, platform.String()) + s.includes = append(s.includes, platformStr) } } diff --git a/internal/entry/sharetags_test.go b/internal/entry/sharetags_test.go index 7b1d742..73ba8db 100644 --- a/internal/entry/sharetags_test.go +++ b/internal/entry/sharetags_test.go @@ -6,13 +6,12 @@ import ( "testing" "codeberg.org/snonux/gos/internal/config" - "codeberg.org/snonux/gos/internal/platforms" ) func TestShareTagsPositive(t *testing.T) { - args := config.Args{Platforms: map[platforms.Platform]int{ - platforms.Platform("mastodon"): 100, - platforms.Platform("linkedin"): 100, + args := config.Args{Platforms: map[string]int{ + "mastodon": 100, + "linkedin": 100, }} testTable := map[string]shareTags{ "./foo/bar.without.tags.txt.20240101-010101.queued": { @@ -57,9 +56,9 @@ func TestShareTagsPositive(t *testing.T) { } } func TestShareTagsNegative(t *testing.T) { - args := config.Args{Platforms: map[platforms.Platform]int{ - platforms.Platform("mastodon"): 100, - platforms.Platform("linkedin"): 100, + args := config.Args{Platforms: map[string]int{ + string("mastodon"): 100, + string("linkedin"): 100, }} testTable := map[string]shareTags{ "./foo/bar.without.tags.txt.20240101-010101.queued": { diff --git a/internal/platforms/platform.go b/internal/platforms/platform.go index f899357..cabc446 100644 --- a/internal/platforms/platform.go +++ b/internal/platforms/platform.go @@ -1,8 +1,15 @@ package platforms import ( + "context" "fmt" "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/linkedin" + "codeberg.org/snonux/gos/internal/platforms/mastodon" ) type Platform string @@ -30,3 +37,25 @@ func New(platformStr string) (Platform, error) { func (p Platform) String() string { return string(p) } + +func (p Platform) Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) (err error) { + colour.Infoln("Posting", en) + switch p.String() { + case "mastodon": + err = mastodon.Post(ctx, args, sizeLimit, en) + case "linkedin": + err = linkedin.Post(ctx, args, sizeLimit, en) + default: + err = fmt.Errorf("Platform '%s' (not yet) implemented", p) + } + + if err != nil { + return err + } + if err := en.MarkPosted(); err != nil { + return err + } + + colour.Successf("Successfully posted message to %s", p) + return nil +} diff --git a/internal/queue/queue.go b/internal/queue/queue.go index aa63c71..832b877 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -77,8 +77,12 @@ func queuePlatforms(args config.Args) error { if err != nil { return err } - for platform := range args.Platforms { - excluded, err := en.PlatformExcluded(args, platform) + for platformStr := range args.Platforms { + platform, err := platforms.New(platformStr) + if err != nil { + return err + } + excluded, err := en.PlatformExcluded(args, platform.String()) if err != nil { return err } diff --git a/internal/run.go b/internal/run.go index aeb2fc2..c0d8234 100644 --- a/internal/run.go +++ b/internal/run.go @@ -3,14 +3,10 @@ package internal import ( "context" "errors" - "fmt" "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" "codeberg.org/snonux/gos/internal/queue" "codeberg.org/snonux/gos/internal/schedule" @@ -24,7 +20,11 @@ func Run(ctx context.Context, args config.Args) error { colour.Infoln(err) } - for platform, sizeLimit := range args.Platforms { + for platformStr, sizeLimit := range args.Platforms { + platform, err := platforms.New(platformStr) + if err != nil { + return err + } if err := runPlatform(ctx, args, platform, sizeLimit); err != nil { if softError(err) { colour.Infoln(err) @@ -49,37 +49,13 @@ func runPlatform(ctx context.Context, args config.Args, platform platforms.Platf case err != nil: return err } - err = postPlatform(ctx, args, platform, sizeLimit, en) + err = platform.Post(ctx, args, sizeLimit, en) if errors.Is(err, prompt.ErrRamdomOther) { return runPlatform(ctx, args, platform, sizeLimit) } return err } -func postPlatform(ctx context.Context, args config.Args, platform platforms.Platform, - sizeLimit int, en entry.Entry) (err error) { - - colour.Infoln("Posting", en) - switch platform.String() { - case "mastodon": - err = mastodon.Post(ctx, args, sizeLimit, en) - case "linkedin": - err = linkedin.Post(ctx, args, sizeLimit, en) - default: - err = fmt.Errorf("Platform '%s' (not yet) implemented", platform) - } - - if err != nil { - return err - } - if err := en.MarkPosted(); err != nil { - return err - } - - colour.Successf("Successfully posted message to %s", platform) - return nil -} - func softError(err error) bool { return errors.Is(err, prompt.ErrAborted) || errors.Is(err, prompt.ErrDeleted) } -- cgit v1.2.3