summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/platforms/linkedin/linkedin.go2
-rw-r--r--internal/prompt/prompt.go34
-rw-r--r--internal/run.go53
3 files changed, 56 insertions, 33 deletions
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go
index 3addd46..ce30970 100644
--- a/internal/platforms/linkedin/linkedin.go
+++ b/internal/platforms/linkedin/linkedin.go
@@ -47,7 +47,7 @@ func callLinkedInAPI(personID, accessToken, message string) error {
post := map[string]interface{}{
"author": fmt.Sprintf("urn:li:person:%s", personID),
- "commentary": message,
+ "commentary": message, // TODO: Can't post (...) paretenthesis? escape them?
"visibility": "PUBLIC",
"distribution": map[string]interface{}{
"feedDistribution": "MAIN_FEED",
diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go
new file mode 100644
index 0000000..0afe98e
--- /dev/null
+++ b/internal/prompt/prompt.go
@@ -0,0 +1,34 @@
+package prompt
+
+import (
+ "bufio"
+ "errors"
+ "fmt"
+ "os"
+ "strings"
+)
+
+var ErrAborted = errors.New("aborted")
+
+func Yes(question string) bool {
+ reader := bufio.NewReader(os.Stdin)
+
+ for {
+ fmt.Print(question, " (y/n): ")
+ input, err := reader.ReadString('\n')
+ if err != nil {
+ fmt.Println("Error reading input:", err)
+ continue
+ }
+
+ input = strings.TrimSpace(input)
+ switch strings.ToLower(input) {
+ case "y", "yes":
+ return true
+ case "n", "no":
+ return false
+ default:
+ fmt.Println("Please enter 'y' or 'n'.")
+ }
+ }
+}
diff --git a/internal/run.go b/internal/run.go
index 0457f36..b79c4ea 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -7,6 +7,7 @@ import (
"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"
@@ -31,46 +32,34 @@ func Run(ctx context.Context, args config.Args) error {
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":
- if args.DryRun {
- log.Println("Not posting", ent, "to", platform, "as dry-run enabled")
- continue
- }
- if err := mastodon.Post(ctx, args, ent); err != nil {
- if errors.Is(err, prompt.ErrAborted) {
- log.Println("Aborted posting to", platform)
- continue
- }
- return err
- }
- if err := ent.MarkPosted(); err != nil {
- return err
- }
- log.Println("Posted", ent, "to", platform)
+ postCB = mastodon.Post
case "linkedin":
- if args.DryRun {
- log.Println("Not posting", ent, "to", platform, "as dry-run enabled")
- continue
- }
- if err := linkedin.Post(ctx, args, ent); err != nil {
- if errors.Is(err, prompt.ErrAborted) {
- log.Println("Aborted posting to", platform)
- continue
- }
- return err
- }
- if err := ent.MarkPosted(); err != nil {
- return err
- }
- log.Println("Posted", ent, "to", platform)
+ postCB = linkedin.Post
default:
- // TODO: Once we have LinkedIn implemented, make the above code
- // more generic so that it can be used with LinkedIn as well.
log.Fatal("Platform", platform, "(not yet) implemented")
}
+
+ if err := postCB(ctx, args, ent); 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