diff options
| -rw-r--r-- | internal/entry/entry_test.go | 7 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 8 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 9 | ||||
| -rw-r--r-- | internal/prompt/file.go | 54 | ||||
| -rw-r--r-- | internal/prompt/prompt.go | 62 | ||||
| -rw-r--r-- | internal/queue/queue.go | 13 | ||||
| -rw-r--r-- | internal/timestamp/timestamp_test.go | 2 |
7 files changed, 65 insertions, 90 deletions
diff --git a/internal/entry/entry_test.go b/internal/entry/entry_test.go index ac97f04..55d2379 100644 --- a/internal/entry/entry_test.go +++ b/internal/entry/entry_test.go @@ -81,7 +81,6 @@ func TestExtractURLs(t *testing.T) { } func TestHasTag(t *testing.T) { - // TODO: Remove t.Parallel() everywhere table := map[string][]string{ "foo.txt": []string{}, "foo.prio.txt": []string{"prio"}, @@ -94,12 +93,12 @@ func TestHasTag(t *testing.T) { if err != nil { t.Error(err) } - if len(expectedTags) != len(ent.tags) { - t.Errorf("expected '%d' tags but got '%d'", len(expectedTags), len(ent.tags)) + if len(expectedTags) != len(ent.simpleTags) { + t.Errorf("expected '%d' tags but got '%d'", len(expectedTags), len(ent.simpleTags)) } for _, tag := range expectedTags { if !ent.HasTag(tag) { - t.Errorf("expected tag '%s' but got '%s'", tag, ent.tags) + t.Errorf("expected tag '%s' but got '%s'", tag, ent.simpleTags) } } } diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 75f4693..0c5de92 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -59,13 +59,7 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) } question := fmt.Sprintf("Do you want to post this message to Linkedin (%v)?", prev) - if err := prompt.DoYouWantThis(question, content); err != nil { - if errors.Is(err, prompt.ErrEditContent) { - if err := ent.Edit(); err != nil { - return err - } - return post(ctx, args, sizeLimit, ent) - } + if err := prompt.FileAction(question, content, ent.Path); err != nil { return err } diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index f896290..919d39d 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "log" @@ -32,13 +31,7 @@ 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 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) - } + if err := prompt.FileAction("Do you want to post this message to Mastodon?", content, ent.Path); err != nil { return err } diff --git a/internal/prompt/file.go b/internal/prompt/file.go new file mode 100644 index 0000000..d01f066 --- /dev/null +++ b/internal/prompt/file.go @@ -0,0 +1,54 @@ +package prompt + +import ( + "bufio" + "errors" + "fmt" + "os" + "os/exec" + "strings" +) + +func FileAction(question, content, filePath string) error { + info2(filePath) + fmt.Print(":\n") + info1(content) + fmt.Print("\n") + reader := bufio.NewReader(os.Stdin) + + for { + ack("%s (y=yes/n=no/e=edit/d=delete):", question) + input, err := reader.ReadString('\n') + if err != nil { + fmt.Println("Error reading input:", err) + continue + } + + switch strings.ToLower(strings.TrimSpace(input)) { + case "y", "yes": + return nil + case "n", "no": + return ErrAborted + case "e", "edit": + return EditFile(filePath) + case "d", "delete": + return os.Remove(filePath) + default: + fmt.Println("Please enter 'y' or 'n' or 'e' or 'd'.") + } + } +} + +func EditFile(filePath string) error { + editor, ok := os.LookupEnv("EDITOR") + if !ok { + 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() +} diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index e9c5e72..d8684f3 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -5,30 +5,19 @@ import ( "errors" "fmt" "os" - "os/exec" - "strings" "github.com/fatih/color" ) var ( - ErrAborted = errors.New("aborted") - ErrEditContent = errors.New("edit content") - ErrDeleteFile = errors.New("delete file") - info = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc() - ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc() + ErrAborted = errors.New("aborted") + info1 = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc() + info2 = color.New(color.FgHiYellow, color.BgBlue, color.Bold).PrintfFunc() + ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc() ) -// TODO: Refactor this prompt, including all operations done on the file, like abort, edit, remove, etc. -// TODO: And also don't use error for control flow. -func DoYouWantThis(question, content string) error { - info(content) - fmt.Print("\n") - return whatNow(question) -} - func Acknowledge(message, content string) error { - info(content) + info1(content) fmt.Print("\n") ack(message + " (press enter)") reader := bufio.NewReader(os.Stdin) @@ -37,44 +26,3 @@ func Acknowledge(message, content string) error { } return nil } - -func whatNow(question string) error { - reader := bufio.NewReader(os.Stdin) - - for { - ack("%s (y=yes/n=no/e=edit/d=delete):", question) - input, err := reader.ReadString('\n') - if err != nil { - fmt.Println("Error reading input:", err) - continue - } - - switch strings.ToLower(strings.TrimSpace(input)) { - case "y", "yes": - return nil - case "n", "no": - return ErrAborted - case "e", "edit": - return ErrEditContent - case "d", "delete": - // TODO: Implement - return ErrDeleteFile - default: - fmt.Println("Please enter 'y' or 'n' or 'e' or 'd'.") - } - } -} - -func EditFile(filePath string) error { - editor, ok := os.LookupEnv("EDITOR") - if !ok { - 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() -} diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 59882cc..74ba00f 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -1,7 +1,6 @@ package queue import ( - "errors" "fmt" "log" "os" @@ -53,17 +52,7 @@ func queueEntries(args config.Args) error { if err != nil { return err } - // TODO Refactor this prompting mechanism plus possible choices for DRY - err = prompt.DoYouWantThis("Do you want to queue this content", content) - switch { - case errors.Is(err, prompt.ErrEditContent): - err = ent.Edit() - case errors.Is(err, prompt.ErrDeleteFile): - if err = ent.Remove(); err == nil { - continue - } - } - if err != nil { + if err := prompt.FileAction("Do you want to queue this content", content, ent.Path); err != nil { return err } } diff --git a/internal/timestamp/timestamp_test.go b/internal/timestamp/timestamp_test.go index d408480..cdcd0e3 100644 --- a/internal/timestamp/timestamp_test.go +++ b/internal/timestamp/timestamp_test.go @@ -6,8 +6,6 @@ import ( ) func TestUpdateInFilename(t *testing.T) { - t.Parallel() - var ( filePath = "gosdir/db/platforms/mastodon/1728240487.txt.20241009-232530.queued" nowTime = NowTime() |
