diff options
| -rw-r--r-- | cmd/gos/main.go | 3 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 9 | ||||
| -rw-r--r-- | internal/prompt/prompt.go | 10 | ||||
| -rw-r--r-- | internal/queue/queue.go | 21 |
4 files changed, 35 insertions, 8 deletions
diff --git a/cmd/gos/main.go b/cmd/gos/main.go index de41866..43bda4f 100644 --- a/cmd/gos/main.go +++ b/cmd/gos/main.go @@ -15,9 +15,8 @@ import ( "codeberg.org/snonux/gos/internal/config" ) -const versionStr = "v0.0.1" +const versionStr = "v0.0.2" -// TODO: edit tag, to edit post before it is queued. // TODO: now tag, to post a post immediately, ignoring the stats. func main() { dry := flag.Bool("dry", false, "Dry run") diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 6646937..75f4693 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -19,7 +19,10 @@ import ( var errUnauthorized = errors.New("unauthorized access, refresh or create token?") -const linkedInTimeout = 10 * time.Second +const ( + linkedInPostsURL = "https://api.linkedin.com/rest/posts" + linkedInTimeout = 10 * time.Second +) func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { err := post(ctx, args, sizeLimit, ent) @@ -72,8 +75,6 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) } func callLinkedInAPI(ctx context.Context, personID, accessToken, content string, prev preview) error { - const url = "https://api.linkedin.com/rest/posts" - post := map[string]interface{}{ "author": fmt.Sprintf("urn:li:person:%s", personID), "commentary": escapeLinkedInText(content), @@ -101,7 +102,7 @@ func callLinkedInAPI(ctx context.Context, personID, accessToken, content string, if err != nil { return fmt.Errorf("Error encoding JSON:%w", err) } - req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(payload)) + req, err := http.NewRequestWithContext(ctx, "POST", linkedInPostsURL, bytes.NewBuffer(payload)) if err != nil { return fmt.Errorf("Error creating request: %w", err) } diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index fb5b870..e9c5e72 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -14,10 +14,13 @@ import ( 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() ) +// 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") @@ -39,7 +42,7 @@ func whatNow(question string) error { reader := bufio.NewReader(os.Stdin) for { - ack("%s (y=yes/n=no/e=edit):", question) + 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) @@ -53,8 +56,11 @@ func whatNow(question string) error { return ErrAborted case "e", "edit": return ErrEditContent + case "d", "delete": + // TODO: Implement + return ErrDeleteFile default: - fmt.Println("Please enter 'y' or 'n' or 'e'.") + fmt.Println("Please enter 'y' or 'n' or 'e' or 'd'.") } } } diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 2f8c780..e396761 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -1,6 +1,7 @@ package queue import ( + "errors" "fmt" "log" "os" @@ -11,6 +12,7 @@ import ( "codeberg.org/snonux/gos/internal/config" "codeberg.org/snonux/gos/internal/oi" + "codeberg.org/snonux/gos/internal/prompt" "codeberg.org/snonux/gos/internal/timestamp" ) @@ -40,6 +42,25 @@ func queueEntries(args config.Args) error { } for filePath := range ch { + if strings.Contains(filepath.Base(filePath), ".ask.") { + bytes, err := os.ReadFile(filePath) + if err != nil { + return err + } + // TODO Refactor + err = prompt.DoYouWantThis("Do you want to queue this content", strings.TrimSpace(string(bytes))) + switch { + case errors.Is(err, prompt.ErrEditContent): + err = prompt.EditFile(filePath) + case errors.Is(err, prompt.ErrDeleteFile): + if err = os.Remove(filePath); err == nil { + continue + } + } + if err != nil { + return err + } + } destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir, filepath.Base(filePath), timestamp.Now()) if args.DryRun { |
