summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-11-08 23:04:51 +0200
committerPaul Buetow <paul@buetow.org>2024-11-08 23:04:51 +0200
commit5ee4d7a6020e32159e735ff9e5b38be5da6bfe9c (patch)
treecabe16c43c3ffa4a6f55c2c3095b30bfc809aa9f
parent9b14c12e0ccd5c96dd9f17d1b20fe134308d2184 (diff)
dont use logger but color outputter
-rw-r--r--internal/colour/colour.go3
-rw-r--r--internal/config/args.go5
-rw-r--r--internal/config/secrets.go5
-rw-r--r--internal/platforms/linkedin/linkedin.go8
-rw-r--r--internal/platforms/linkedin/oauth2/oauth2.go14
-rw-r--r--internal/platforms/linkedin/preview.go6
-rw-r--r--internal/platforms/mastodon/mastodon.go4
-rw-r--r--internal/queue/queue.go15
-rw-r--r--internal/run.go10
-rw-r--r--internal/schedule/schedule.go4
-rw-r--r--internal/schedule/stats.go5
11 files changed, 43 insertions, 36 deletions
diff --git a/internal/colour/colour.go b/internal/colour/colour.go
index d003f65..5d1133a 100644
--- a/internal/colour/colour.go
+++ b/internal/colour/colour.go
@@ -4,6 +4,9 @@ import "github.com/fatih/color"
var (
// Printf function(s)
+ info = color.New(color.FgHiBlack, color.BgBlack)
+ Infof = info.PrintfFunc()
+ Infoln = info.PrintlnFunc()
Info1f = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc()
Info2f = color.New(color.FgHiYellow, color.BgHiBlack, color.Bold).PrintfFunc()
Ackf = color.New(color.FgBlack, color.BgHiYellow, color.Bold).PrintfFunc()
diff --git a/internal/config/args.go b/internal/config/args.go
index 8c9f767..a0fa0ea 100644
--- a/internal/config/args.go
+++ b/internal/config/args.go
@@ -2,11 +2,12 @@ package config
import (
"fmt"
- "log"
"slices"
"strconv"
"strings"
"time"
+
+ "codeberg.org/snonux/gos/internal/colour"
)
var validPlatforms = []string{"mastodon", "linkedin"}
@@ -38,7 +39,7 @@ func (a *Args) ParsePlatforms(platforms string) error {
return err
}
} else {
- log.Println("No message length specified for", platform, "so assuming 500")
+ colour.Infoln("No message length specified for", platform, "so assuming 500")
a.Platforms[parts[0]] = 500
}
}
diff --git a/internal/config/secrets.go b/internal/config/secrets.go
index 6ed1c1f..338d17a 100644
--- a/internal/config/secrets.go
+++ b/internal/config/secrets.go
@@ -4,8 +4,9 @@ import (
"encoding/json"
"fmt"
"io"
- "log"
"os"
+
+ "codeberg.org/snonux/gos/internal/colour"
)
// The config file containing all the secrets and credentials.
@@ -42,7 +43,7 @@ func NewSecrets(configPath string) (Secrets, error) {
}
func (s Secrets) WriteToDisk(configPath string) error {
- log.Println("Writing", configPath)
+ colour.Infoln("Writing", configPath)
bytes, err := json.MarshalIndent(s, "", " ")
if err != nil {
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go
index 1d57a9e..283ab14 100644
--- a/internal/platforms/linkedin/linkedin.go
+++ b/internal/platforms/linkedin/linkedin.go
@@ -7,10 +7,10 @@ import (
"errors"
"fmt"
"io"
- "log"
"net/http"
"time"
+ "codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/platforms/linkedin/oauth2"
@@ -27,7 +27,7 @@ const (
func Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) error {
err := post(ctx, args, sizeLimit, en)
if errors.Is(err, errUnauthorized) {
- log.Println(err, "=> trying to refresh LinkedIn access token")
+ colour.Infoln(err, "=> trying to refresh LinkedIn access token")
args.Secrets.LinkedInAccessToken = "" // Reset the token
return post(ctx, args, sizeLimit, en)
}
@@ -36,7 +36,7 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry)
func post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) error {
if args.DryRun {
- log.Println("Not posting", en, "to LinkedIn as dry-run enabled")
+ colour.Infoln("Not posting", en, "to LinkedIn as dry-run enabled")
return nil
}
@@ -63,7 +63,7 @@ func post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry)
if filePath, err = prev.DownloadImage(args.CacheDir); err != nil {
return err
}
- log.Println("Downloaded preview image to ", filePath)
+ colour.Infoln("Downloaded preview image to ", filePath)
}
question := fmt.Sprintf("Do you want to post this message to Linkedin (%v)?", prev)
diff --git a/internal/platforms/linkedin/oauth2/oauth2.go b/internal/platforms/linkedin/oauth2/oauth2.go
index 9a6c84d..51beec9 100644
--- a/internal/platforms/linkedin/oauth2/oauth2.go
+++ b/internal/platforms/linkedin/oauth2/oauth2.go
@@ -6,12 +6,12 @@ import (
"errors"
"fmt"
"io"
- "log"
"net/http"
"os/exec"
"runtime"
"time"
+ "codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/config"
"golang.org/x/oauth2"
"golang.org/x/oauth2/linkedin"
@@ -72,7 +72,7 @@ func oauthCallbackHandler(w http.ResponseWriter, r *http.Request) {
defer close(errCh)
code := r.URL.Query().Get("code")
- log.Println("Exchanging OAuth2 token")
+ colour.Infoln("Exchanging OAuth2 token")
token, err := oauthConfig.Exchange(globalCtx, code)
if err != nil {
_, _ = w.Write([]byte(err.Error()))
@@ -110,7 +110,7 @@ func LinkedInCreds(ctx context.Context, args config.Args) (string, string, error
http.HandleFunc("/callback", oauthCallbackHandler)
http.HandleFunc("/up", upHandler)
- log.Println("Listening on http://localhost:8080 for LinkedIn OAuth2")
+ colour.Infoln("Listening on http://localhost:8080 for LinkedIn OAuth2")
go func() {
if err := http.ListenAndServe(":8080", nil); err != nil {
errCh <- err
@@ -139,7 +139,7 @@ func LinkedInCreds(ctx context.Context, args config.Args) (string, string, error
}
func openURLInFirefox(browser, url string) error {
- log.Println("Opening", url, "in", browser)
+ colour.Infoln("Opening", url, "in", browser)
switch runtime.GOOS {
case "windows":
cmd := exec.Command("cmd", "/C", "start", browser, url)
@@ -162,9 +162,11 @@ func WaitUntilURLIsReachable(url string) error {
resp, err := http.Get(url)
if err != nil {
- log.Printf("URL is not reachable: %v\n", err)
+ colour.Infof("URL is not reachable: %v", err)
+ fmt.Print("\n")
} else {
- log.Printf("URL is reachable: %s - Status Code: %d\n", url, resp.StatusCode)
+ colour.Infof("URL is reachable: %s - Status Code: %d", url, resp.StatusCode)
+ fmt.Print("\n")
resp.Body.Close()
return nil
}
diff --git a/internal/platforms/linkedin/preview.go b/internal/platforms/linkedin/preview.go
index 2e40798..6a16536 100644
--- a/internal/platforms/linkedin/preview.go
+++ b/internal/platforms/linkedin/preview.go
@@ -5,12 +5,12 @@ import (
"errors"
"fmt"
"io"
- "log"
"net/http"
"net/url"
"os"
"path/filepath"
+ "codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/oi"
"golang.org/x/net/html"
)
@@ -30,11 +30,11 @@ func NewPreview(ctx context.Context, urls []string) (preview, error) {
}
title, imageURL, err := extractFromURL(ctx, urls[0])
if errors.Is(err, errNoTitleElementFound) || title == "" {
- log.Println("Setting title to", urls[0])
+ colour.Infoln("Setting title to", urls[0])
title = urls[0]
}
if errors.Is(err, errNoImageElementFound) {
- log.Println("URL", urls[0], "without any image, that's fine, though.")
+ colour.Infoln("URL", urls[0], "without any image, that's fine, though.")
err = nil
}
return preview{title: title, imageURL: imageURL, url: urls[0]}, err
diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go
index b6645f8..c0264de 100644
--- a/internal/platforms/mastodon/mastodon.go
+++ b/internal/platforms/mastodon/mastodon.go
@@ -6,10 +6,10 @@ import (
"encoding/json"
"fmt"
"io"
- "log"
"net/http"
"time"
+ "codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/prompt"
@@ -28,7 +28,7 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry)
return fmt.Errorf("failed to marshal payload: %w", err)
}
if args.DryRun {
- log.Println("Not posting", en, "to Mastodon as dry-run enabled")
+ colour.Infoln("Not posting", en, "to Mastodon as dry-run enabled")
return nil
}
if err := prompt.FileAction("Do you want to post this message to Mastodon?", content, en.Path); err != nil {
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index abcecb6..de238f0 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -2,12 +2,12 @@ package queue
import (
"fmt"
- "log"
"os"
"path/filepath"
"strings"
"time"
+ "codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/oi"
@@ -50,7 +50,7 @@ func queueEntries(args config.Args) error {
}
destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir, filepath.Base(en.Path), timestamp.Now())
if args.DryRun {
- log.Println("Not queueing entry", en.Path, "to", destPath, "as dry-run mode enabled")
+ colour.Infoln("Not queueing entry", en.Path, "to", destPath, "as dry-run mode enabled")
continue
}
if err := oi.Rename(en.Path, destPath); err != nil {
@@ -82,7 +82,7 @@ func queuePlatforms(args config.Args) error {
return err
}
if excluded {
- log.Println("Not queueing entry", en, "to platform", platform, "as it is excluded")
+ colour.Infoln("Not queueing entry", en, "to platform", platform, "as it is excluded")
continue
}
if err := queuePlatform(en, args.GosDir, platform); err != nil {
@@ -95,7 +95,8 @@ func queuePlatforms(args config.Args) error {
// Keep queued items in trash for a while.
trashPath := filepath.Join(trashDir, strings.TrimSuffix(filepath.Base(en.Path), ".queued")+".trash")
- log.Printf("Trashing %s -> %s", en.Path, trashPath)
+ colour.Infof("Trashing %s -> %s", en.Path, trashPath)
+ fmt.Print("\n")
if err := oi.EnsureParentDir(trashPath); err != nil {
return err
}
@@ -116,11 +117,11 @@ func queuePlatform(en entry.Entry, gosDir, platform string) error {
// Entry already posted platform?
if oi.IsRegular(postedFile) {
- log.Println("Not re-queueing", destPath, "as", postedFile, "already exists")
+ colour.Infoln("Not re-queueing", destPath, "as", postedFile, "already exists")
return nil
}
- log.Println("Queuing", en.Path, "->", destPath)
+ colour.Infoln("Queuing", en.Path, "->", destPath)
return oi.CopyFile(en.Path, destPath)
}
@@ -135,7 +136,7 @@ func deleteFiles(path, suffix string, olderThan time.Time) error {
return err
}
if fileInfo.ModTime().Before(olderThan) {
- log.Println("Cleaning up", filePath)
+ colour.Infoln("Cleaning up", filePath)
err := os.Remove(filePath)
if err != nil {
return err
diff --git a/internal/run.go b/internal/run.go
index d865b2c..a171ec7 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -21,13 +21,13 @@ func Run(ctx context.Context, args config.Args) error {
if !softError(err) {
return err
}
- log.Println(err)
+ colour.Infoln(err)
}
for platform, sizeLimit := range args.Platforms {
if err := runPlatform(ctx, args, platform, sizeLimit); err != nil {
if softError(err) {
- log.Println(err)
+ colour.Infoln(err)
continue
}
return err
@@ -41,16 +41,16 @@ func runPlatform(ctx context.Context, args config.Args, platform string, sizeLim
en, err := schedule.Run(args, platform)
switch {
case errors.Is(err, schedule.ErrNothingToSchedule):
- log.Println("Nothing to be scheduled for", platform)
+ colour.Infoln("Nothing to be scheduled for", platform)
return nil
case errors.Is(err, schedule.ErrNothingQueued):
- log.Println("Nothing queued for", platform)
+ colour.Infoln("Nothing queued for", platform)
return nil
case err != nil:
return err
}
- log.Println("Posting", en)
+ colour.Infoln("Posting", en)
var postCB func(context.Context, config.Args, int, entry.Entry) error
switch strings.ToLower(platform) {
case "mastodon":
diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go
index 7144a4d..4f25fb4 100644
--- a/internal/schedule/schedule.go
+++ b/internal/schedule/schedule.go
@@ -3,12 +3,12 @@ package schedule
import (
"errors"
"fmt"
- "log"
"os"
"path/filepath"
"slices"
"strings"
+ "codeberg.org/snonux/gos/internal/colour"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
"codeberg.org/snonux/gos/internal/oi"
@@ -76,7 +76,7 @@ func selectRandomEntry(dir, tag string) (entry.Entry, error) {
}
en, err := entry.New(filepath.Join(dir, file.Name()))
if err != nil {
- log.Println(err)
+ colour.Infoln(err)
return entry.Zero, false
}
return en, en.State == entry.Queued
diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go
index 09fe3d3..f792675 100644
--- a/internal/schedule/stats.go
+++ b/internal/schedule/stats.go
@@ -2,7 +2,6 @@ package schedule
import (
"fmt"
- "log"
"os"
"path/filepath"
"strconv"
@@ -81,11 +80,11 @@ func (s stats) targetHit(pauseDays, maxQueuedDays int) bool {
pauseDays--
}
if s.postsPerDay >= s.postsPerDayTarget {
- log.Println("Posts per day target hit")
+ colour.Infoln("Posts per day target hit")
return true
}
if s.lastPostDaysAgo <= float64(pauseDays) {
- log.Println("Need to wait a bit longer as last post isn't", pauseDays, "days ago yet")
+ colour.Infoln("Need to wait a bit longer as last post isn't", pauseDays, "days ago yet")
return true
}