diff options
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 4 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 5 | ||||
| -rw-r--r-- | internal/run.go | 66 |
3 files changed, 42 insertions, 33 deletions
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index ce30970..401d5a3 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -31,6 +31,10 @@ func Post(ctx context.Context, args config.Args, ent entry.Entry) error { } func post(ctx context.Context, args config.Args, ent entry.Entry) error { + if args.DryRun { + log.Println("Not posting", ent, "to LinkedIn as dry-run enabled") + return nil + } personID, accessToken, err := oauth2.LinkedInCreds(args) if err != err { return err diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index 361d1a5..7cdb513 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "log" "net/http" "codeberg.org/snonux/gos/internal/config" @@ -22,6 +23,10 @@ func Post(ctx context.Context, args config.Args, ent entry.Entry) error { if err != nil { return fmt.Errorf("failed to marshal payload: %w", err) } + if args.DryRun { + log.Println("Not posting", ent, "to Mastodon as dry-run enabled") + return nil + } if !prompt.Yes(fmt.Sprintf("%s\nDo you want to post this message to Mastodon?", string(payloadBytes))) { return prompt.ErrAborted } diff --git a/internal/run.go b/internal/run.go index b79c4ea..acf0525 100644 --- a/internal/run.go +++ b/internal/run.go @@ -21,46 +21,46 @@ func Run(ctx context.Context, args config.Args) error { } for _, platform := range args.Platforms { - 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 - } - if args.DryRun { - log.Println("Not posting", ent, "to", platform, "as dry-run enabled") - return nil - } - - log.Println("Scheduling", ent) - var postCB func(context.Context, config.Args, 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, ent); err != nil { + if err := runPlatform(ctx, args, platform); err != nil { if errors.Is(err, prompt.ErrAborted) { log.Println("Aborted posting to", platform) continue } return err } - - log.Println("Posted", ent, "to", platform) - if err := ent.MarkPosted(); err != nil { - return err - } } return nil } + +func runPlatform(ctx context.Context, args config.Args, platform string) 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, 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, ent); err != nil { + return err + } + + log.Println("Posted", ent, "to", platform) + return ent.MarkPosted() +} |
