summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-31 22:27:25 +0200
committerPaul Buetow <paul@buetow.org>2024-10-31 22:27:25 +0200
commit177979b5df6336e477d5aa3cdc64b3021288bb92 (patch)
tree9b4c146b1413bf99b2fc6ec4fc58a7ab77c9cc03 /internal
parent368d58e61ab3f9978c7cf51ee4c0d45263b9e5c5 (diff)
refactor
Diffstat (limited to 'internal')
-rw-r--r--internal/entry/entry.go9
-rw-r--r--internal/oi/oi.go9
-rw-r--r--internal/prompt/file.go26
-rw-r--r--internal/prompt/prompt.go8
-rw-r--r--internal/run.go13
5 files changed, 46 insertions, 19 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index 620c103..1fb5ce1 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -9,6 +9,7 @@ import (
"strings"
"time"
+ "codeberg.org/snonux/gos/internal/oi"
"codeberg.org/snonux/gos/internal/prompt"
"codeberg.org/snonux/gos/internal/timestamp"
)
@@ -104,12 +105,8 @@ func New(filePath string) (Entry, error) {
}
func (e *Entry) Content() (string, []string, error) {
- bytes, err := os.ReadFile(e.Path)
- if err != nil {
- return "", []string{}, err
- }
- content := strings.TrimSpace(string(bytes))
- return content, extractURLs(content), nil
+ content, err := oi.SlurpAndTrim(e.Path)
+ return content, extractURLs(content), err
}
func (e Entry) ContentWithLimit(sizeLimit int) (string, []string, error) {
diff --git a/internal/oi/oi.go b/internal/oi/oi.go
index 8ee29d5..b2a0953 100644
--- a/internal/oi/oi.go
+++ b/internal/oi/oi.go
@@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
+ "strings"
"time"
"golang.org/x/exp/rand"
@@ -138,3 +139,11 @@ func Rename(srcPath, dstPath string) error {
}
return os.Rename(srcPath, dstPath)
}
+
+func SlurpAndTrim(filePath string) (string, error) {
+ bytes, err := os.ReadFile(filePath)
+ if err != nil {
+ return "", err
+ }
+ return strings.TrimSpace(string(bytes)), nil
+}
diff --git a/internal/prompt/file.go b/internal/prompt/file.go
index d01f066..d6d5d7c 100644
--- a/internal/prompt/file.go
+++ b/internal/prompt/file.go
@@ -7,11 +7,18 @@ import (
"os"
"os/exec"
"strings"
+
+ "codeberg.org/snonux/gos/internal/oi"
+)
+
+var (
+ ErrAborted = errors.New("aborted")
+ ErrDeleted = errors.New("deleted")
)
func FileAction(question, content, filePath string) error {
- info2(filePath)
- fmt.Print(":\n")
+ info2(filePath + ":")
+ fmt.Print("\n")
info1(content)
fmt.Print("\n")
reader := bufio.NewReader(os.Stdin)
@@ -28,11 +35,20 @@ func FileAction(question, content, filePath string) error {
case "y", "yes":
return nil
case "n", "no":
- return ErrAborted
+ return fmt.Errorf("%w %s", ErrAborted, filePath)
case "e", "edit":
- return EditFile(filePath)
+ if err := EditFile(filePath); err != nil {
+ return err
+ }
+ if content, err = oi.SlurpAndTrim(filePath); err != nil {
+ return err
+ }
+ return FileAction(question, content, filePath)
case "d", "delete":
- return os.Remove(filePath)
+ if err := os.Remove(filePath); err != nil {
+ return err
+ }
+ return fmt.Errorf("%w %s", ErrDeleted, filePath)
default:
fmt.Println("Please enter 'y' or 'n' or 'e' or 'd'.")
}
diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go
index d8684f3..29ed86d 100644
--- a/internal/prompt/prompt.go
+++ b/internal/prompt/prompt.go
@@ -2,7 +2,6 @@ package prompt
import (
"bufio"
- "errors"
"fmt"
"os"
@@ -10,10 +9,9 @@ import (
)
var (
- 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()
+ info1 = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc()
+ info2 = color.New(color.FgHiYellow, color.BgHiBlack, color.Bold).PrintfFunc()
+ ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc()
)
func Acknowledge(message, content string) error {
diff --git a/internal/run.go b/internal/run.go
index 98fb501..714687a 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -18,13 +18,16 @@ import (
func Run(ctx context.Context, args config.Args) error {
if err := queue.Run(args); err != nil {
- return err
+ if !softError(err) {
+ return err
+ }
+ log.Println(err)
}
for platform, sizeLimit := range args.Platforms {
if err := runPlatform(ctx, args, platform, sizeLimit); err != nil {
- if errors.Is(err, prompt.ErrAborted) {
- log.Println("Aborted posting to", platform)
+ if softError(err) {
+ log.Println(err)
continue
}
return err
@@ -68,3 +71,7 @@ func runPlatform(ctx context.Context, args config.Args, platform string, sizeLim
color.New(color.FgWhite, color.BgGreen).Println("Successfully posted message to ", platform)
return nil
}
+
+func softError(err error) bool {
+ return errors.Is(err, prompt.ErrAborted) || errors.Is(err, prompt.ErrDeleted)
+}