diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-18 21:36:54 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-18 21:36:54 +0300 |
| commit | ee2cb360bfb6ab35cb8a373fd1237993cea168d0 (patch) | |
| tree | cb44f8ec411124f5aa18edc895e0a1860c680d69 | |
| parent | a98fbd68214b9d99c425fb073051da790b0b15d0 (diff) | |
initial work on editing a post
| -rw-r--r-- | internal/entry/entry.go | 1 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 29 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 11 | ||||
| -rw-r--r-- | internal/prompt/prompt.go | 31 |
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 +} |
