diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-18 21:20:46 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-18 21:20:46 +0300 |
| commit | a98fbd68214b9d99c425fb073051da790b0b15d0 (patch) | |
| tree | 9a312a15e4ec73a31d7e6ea9dc704b2df6a9011b | |
| parent | 84b404e392c94174d1c5ae4feb8afc4f5d334346 (diff) | |
initial edit
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 9 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 8 | ||||
| -rw-r--r-- | internal/prompt/prompt.go | 35 |
3 files changed, 39 insertions, 13 deletions
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 4be231c..4503c53 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -64,8 +64,15 @@ func callLinkedInAPI(ctx context.Context, personID, accessToken, message string) if err != nil { return fmt.Errorf("Error encoding JSON:%w", err) } - if !prompt.YesWithContent("Do you want to post this message to LinkedIn?", message) { + + 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)) diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index 2cb280f..390de4c 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -27,8 +27,14 @@ 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 } - if !prompt.YesWithContent("Do you want to post this message to Mastodon?", content) { + 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") } 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 9feff70..3db9f83 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -10,21 +10,32 @@ import ( "github.com/fatih/color" ) -var ErrAborted = errors.New("aborted") -var contentSprintf = color.New(color.FgCyan, color.BgBlue, color.Bold).SprintFunc() -var dangerSprintf = color.New(color.FgWhite, color.BgRed, color.Bold).SprintFunc() +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() +) + +type PromptSelection int + +const ( + Unknown PromptSelection = iota + Yes + No + Edit +) -func YesWithContent(question, content string) bool { - fmt.Print(contentSprintf(content)) +func DoYouWantThis(question, content string) PromptSelection { + fmt.Print(contentColor(content)) fmt.Print("\n") - return Yes(question) + return whatNow(question) } -func Yes(question string) bool { +func whatNow(question string) PromptSelection { reader := bufio.NewReader(os.Stdin) for { - fmt.Printf("%s ", dangerSprintf(fmt.Sprintf("%s (y/n):", question))) + fmt.Printf("%s ", dangerColor(fmt.Sprintf("%s (y=yes/n=no/e=edit):", question))) input, err := reader.ReadString('\n') if err != nil { fmt.Println("Error reading input:", err) @@ -34,11 +45,13 @@ func Yes(question string) bool { input = strings.TrimSpace(input) switch strings.ToLower(input) { case "y", "yes": - return true + return Yes case "n", "no": - return false + return No + case "e", "edit": + return Edit default: - fmt.Println("Please enter 'y' or 'n'.") + fmt.Println("Please enter 'y' or 'n' or 'e'.") } } } |
