summaryrefslogtreecommitdiff
path: root/internal/run.go
blob: ed70511a529f5d806a54ed1929a8bbb0fe8c488a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package internal

import (
	"context"
	"errors"
	"log"
	"strings"

	"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"
	"codeberg.org/snonux/gos/internal/prompt"
	"codeberg.org/snonux/gos/internal/queue"
	"codeberg.org/snonux/gos/internal/schedule"
)

func Run(ctx context.Context, args config.Args) error {
	if err := queue.Run(args); err != nil {
		return err
	}

	for platform, sizeLimit := range args.Platforms {
		if err := runPlatform(ctx, args, platform, sizeLimit); err != nil {
			if errors.Is(err, prompt.ErrAborted) {
				log.Println("Aborted posting to", platform)
				continue
			}
			return err
		}
	}

	return nil
}

func runPlatform(ctx context.Context, args config.Args, platform string, sizeLimit int) error {
	ent, err := schedule.Run(args, platform)
	switch {
	case errors.Is(err, schedule.ErrNothingToSchedule):
		log.Println("Nothing to be scheduled for", platform)
		return nil
	case errors.Is(err, schedule.ErrNothingQueued):
		log.Println("Nothing queued for", platform)
		return nil
	case err != nil:
		return err
	}

	log.Println("Scheduling", ent)
	var postCB func(context.Context, config.Args, int, entry.Entry) error
	switch strings.ToLower(platform) {
	case "mastodon":
		postCB = mastodon.Post
	case "linkedin":
		postCB = linkedin.Post
	default:
		log.Fatal("Platform", platform, "(not yet) implemented")
	}

	if err := postCB(ctx, args, sizeLimit, ent); err != nil {
		return err
	}

	log.Println("Posted", ent, "to", platform)
	return ent.MarkPosted()
}