summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-18 22:03:01 +0300
committerPaul Buetow <paul@buetow.org>2024-10-18 22:03:01 +0300
commitd20ce579021f3dc087cbcb19a2e0cb2f9a4db113 (patch)
treeb3f29756524637b8d5ba6fa9c1ebe8ecefa30c8a
parentee2cb360bfb6ab35cb8a373fd1237993cea168d0 (diff)
can edit a post before posting int
-rw-r--r--internal/entry/entry.go8
-rw-r--r--internal/platforms/linkedin/linkedin.go9
-rw-r--r--internal/platforms/mastodon/mastodon.go7
-rw-r--r--internal/prompt/prompt.go13
4 files changed, 29 insertions, 8 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index 0132f38..7ad2b2c 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -7,6 +7,7 @@ import (
"strings"
"time"
+ "codeberg.org/snonux/gos/internal/prompt"
"codeberg.org/snonux/gos/internal/timestamp"
)
@@ -107,3 +108,10 @@ func (e *Entry) MarkPosted() error {
e.State = Posted
return nil
}
+
+func (e Entry) Edit() error {
+ if err := prompt.EditFile(e.Path); err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go
index d8eb91e..b07ab70 100644
--- a/internal/platforms/linkedin/linkedin.go
+++ b/internal/platforms/linkedin/linkedin.go
@@ -42,13 +42,8 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
return err
}
if err := prompt.DoYouWantThis("Do you want to post this message to Linkedin?", content); err != nil {
- // TODO: Do the same for Mastodon. Gan this be more generalized?
- if err == prompt.ErrEditContent {
- if err := prompt.EditFile(ent.Path); err != nil {
- return err
- }
- ent, err = entry.New(ent.Path)
- if err != err {
+ if errors.Is(err, prompt.ErrEditContent) {
+ if err := ent.Edit(); err != nil {
return err
}
return post(ctx, args, sizeLimit, ent)
diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go
index d4f9e7b..f582b4c 100644
--- a/internal/platforms/mastodon/mastodon.go
+++ b/internal/platforms/mastodon/mastodon.go
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
+ "errors"
"fmt"
"log"
"net/http"
@@ -28,6 +29,12 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
return nil
}
if err := prompt.DoYouWantThis("Do you want to post this message to Mastodon?", content); err != nil {
+ if errors.Is(err, prompt.ErrEditContent) {
+ if err := ent.Edit(); err != nil {
+ return err
+ }
+ return Post(ctx, args, sizeLimit, ent)
+ }
return err
}
req, err := http.NewRequestWithContext(ctx, "POST", args.Secrets.MastodonURL, bytes.NewBuffer(payloadBytes))
diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go
index bb9b1ce..e9d35c1 100644
--- a/internal/prompt/prompt.go
+++ b/internal/prompt/prompt.go
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
+ "os/exec"
"strings"
"github.com/fatih/color"
@@ -50,5 +51,15 @@ func whatNow(question string) error {
}
func EditFile(filePath string) error {
- return nil
+ editor := os.Getenv("EDITOR")
+ if editor == "" {
+ return errors.New("EDITOR environment variable is not set")
+ }
+
+ cmd := exec.Command(editor, filePath)
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ cmd.Stdin = os.Stdin
+
+ return cmd.Run()
}