summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/entry/entry.go1
-rw-r--r--internal/platforms/linkedin/linkedin.go29
-rw-r--r--internal/platforms/mastodon/mastodon.go11
-rw-r--r--internal/prompt/prompt.go31
4 files changed, 33 insertions, 39 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index b8752e3..0132f38 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -80,6 +80,7 @@ func (e Entry) Content() (string, error) {
return string(bytes), err
}
+// TODO: Optionally open editor when a content is too large.
func (e Entry) ContentWithLimit(sizeLimit int) (string, error) {
content, err := e.Content()
if err != nil {
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go
index 4503c53..d8eb91e 100644
--- a/internal/platforms/linkedin/linkedin.go
+++ b/internal/platforms/linkedin/linkedin.go
@@ -41,15 +41,29 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
if err != nil {
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 {
+ return err
+ }
+ return post(ctx, args, sizeLimit, ent)
+ }
+ return err
+ }
return callLinkedInAPI(ctx, personID, accessToken, content)
}
-func callLinkedInAPI(ctx context.Context, personID, accessToken, message string) error {
+func callLinkedInAPI(ctx context.Context, personID, accessToken, content string) error {
const url = "https://api.linkedin.com/v2/posts"
post := map[string]interface{}{
"author": fmt.Sprintf("urn:li:person:%s", personID),
- "commentary": message, // TODO: Can't post (...) paretenthesis? escape them?
+ "commentary": content, // TODO: Can't post (...) paretenthesis? escape them?
"visibility": "PUBLIC",
"distribution": map[string]interface{}{
"feedDistribution": "MAIN_FEED",
@@ -64,17 +78,6 @@ func callLinkedInAPI(ctx context.Context, personID, accessToken, message string)
if err != nil {
return fmt.Errorf("Error encoding JSON:%w", err)
}
-
- switch prompt.DoYouWantThis("Do you want to post this message to Linkedin?", message) {
- case prompt.No:
- return prompt.ErrAborted
- case prompt.Yes:
- case prompt.Edit:
- panic("edit not yet implemented") // TODO
- default:
- panic("should never happen")
- }
-
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("Error creating request: %w", err)
diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go
index 390de4c..d4f9e7b 100644
--- a/internal/platforms/mastodon/mastodon.go
+++ b/internal/platforms/mastodon/mastodon.go
@@ -27,16 +27,9 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
log.Println("Not posting", ent, "to Mastodon as dry-run enabled")
return nil
}
- switch prompt.DoYouWantThis("Do you want to post this message to Mastodon?", content) {
- case prompt.No:
- return prompt.ErrAborted
- case prompt.Yes:
- case prompt.Edit:
- panic("edit not yet implemented") // TODO
- default:
- panic("should never happen")
+ if err := prompt.DoYouWantThis("Do you want to post this message to Mastodon?", content); err != nil {
+ return err
}
-
req, err := http.NewRequestWithContext(ctx, "POST", args.Secrets.MastodonURL, bytes.NewBuffer(payloadBytes))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go
index 3db9f83..bb9b1ce 100644
--- a/internal/prompt/prompt.go
+++ b/internal/prompt/prompt.go
@@ -11,27 +11,20 @@ import (
)
var (
- ErrAborted = errors.New("aborted")
- contentColor = color.New(color.FgCyan, color.BgBlue, color.Bold).SprintFunc()
- dangerColor = color.New(color.FgWhite, color.BgRed, color.Bold).SprintFunc()
+ ErrAborted = errors.New("aborted")
+ // TODO: Add edit functionality. 1. configure EDITOR, 2. fork EDITOR process on the given file.
+ ErrEditContent = errors.New("edit content")
+ contentColor = color.New(color.FgCyan, color.BgBlue, color.Bold).SprintFunc()
+ dangerColor = color.New(color.FgWhite, color.BgRed, color.Bold).SprintFunc()
)
-type PromptSelection int
-
-const (
- Unknown PromptSelection = iota
- Yes
- No
- Edit
-)
-
-func DoYouWantThis(question, content string) PromptSelection {
+func DoYouWantThis(question, content string) error {
fmt.Print(contentColor(content))
fmt.Print("\n")
return whatNow(question)
}
-func whatNow(question string) PromptSelection {
+func whatNow(question string) error {
reader := bufio.NewReader(os.Stdin)
for {
@@ -45,13 +38,17 @@ func whatNow(question string) PromptSelection {
input = strings.TrimSpace(input)
switch strings.ToLower(input) {
case "y", "yes":
- return Yes
+ return nil
case "n", "no":
- return No
+ return ErrAborted
case "e", "edit":
- return Edit
+ return ErrEditContent
default:
fmt.Println("Please enter 'y' or 'n' or 'e'.")
}
}
}
+
+func EditFile(filePath string) error {
+ return nil
+}