diff options
| author | Paul Buetow <paul@buetow.org> | 2024-12-11 22:07:51 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-12-11 22:07:51 +0200 |
| commit | 1dca76e728a70b70ee8001c87407acf962e731cd (patch) | |
| tree | 4636d5b8769595de135bca8977b951d0a570da48 | |
| parent | a5529938536f7a530f0c55e5a82b16a4c80af5b4 (diff) | |
refactor a bit again
| -rw-r--r-- | internal/colour/colour.go | 4 | ||||
| -rw-r--r-- | internal/config/args.go | 2 | ||||
| -rw-r--r-- | internal/config/secrets.go | 2 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 7 | ||||
| -rw-r--r-- | internal/platforms/linkedin/oauth2/oauth2.go | 10 | ||||
| -rw-r--r-- | internal/platforms/linkedin/preview.go | 6 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 2 | ||||
| -rw-r--r-- | internal/platforms/platform.go | 2 | ||||
| -rw-r--r-- | internal/queue/queue.go | 12 | ||||
| -rw-r--r-- | internal/run.go | 8 | ||||
| -rw-r--r-- | internal/schedule/schedule.go | 2 | ||||
| -rw-r--r-- | internal/schedule/stats.go | 5 | ||||
| -rw-r--r-- | internal/tags/inline.go | 2 |
13 files changed, 29 insertions, 35 deletions
diff --git a/internal/colour/colour.go b/internal/colour/colour.go index 09b9fb0..54cb1d9 100644 --- a/internal/colour/colour.go +++ b/internal/colour/colour.go @@ -8,8 +8,8 @@ import ( var ( // infoCol = color.New(color.FgCyan, color.BgBlue, color.Bold) - Infof = fmt.Printf - Infoln = fmt.Println + Infof = func(format string, args ...any) { fmt.Printf(format, args...) } + Infoln = func(args ...any) { fmt.Println(args...) } info2Col = color.New(color.FgHiYellow, color.BgBlue) Info2f = info2Col.PrintfFunc() SInfo2f = info2Col.SprintfFunc() diff --git a/internal/config/args.go b/internal/config/args.go index 4d98118..4438189 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -40,7 +40,7 @@ func (a *Args) ParsePlatforms(platformStrs string) error { return err } } else { - _, _ = colour.Infoln("No message length specified for", platformStr, "so assuming 500") + colour.Infoln("No message length specified for", platformStr, "so assuming 500") a.Platforms[platformStr] = 500 } } diff --git a/internal/config/secrets.go b/internal/config/secrets.go index 28c3515..338d17a 100644 --- a/internal/config/secrets.go +++ b/internal/config/secrets.go @@ -43,7 +43,7 @@ func NewSecrets(configPath string) (Secrets, error) { } func (s Secrets) WriteToDisk(configPath string) error { - _, _ = colour.Infoln("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 b960c01..85b95af 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -25,9 +25,7 @@ const linkedInTimeout = 10 * time.Second 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) { - if _, err = colour.Infoln(err, "=> trying to refresh LinkedIn access token"); err != nil { - return err - } + colour.Infoln(err, "=> trying to refresh LinkedIn access token") args.Secrets.LinkedInAccessToken = "" // Reset the token return post(ctx, args, sizeLimit, en) } @@ -36,8 +34,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 { - _, err := colour.Infoln("Not posting", en, "to LinkedIn as dry-run enabled") - return err + colour.Infoln("Not posting", en, "to LinkedIn as dry-run enabled") } timeout := linkedInTimeout diff --git a/internal/platforms/linkedin/oauth2/oauth2.go b/internal/platforms/linkedin/oauth2/oauth2.go index f37e913..51beec9 100644 --- a/internal/platforms/linkedin/oauth2/oauth2.go +++ b/internal/platforms/linkedin/oauth2/oauth2.go @@ -72,7 +72,7 @@ func oauthCallbackHandler(w http.ResponseWriter, r *http.Request) { defer close(errCh) code := r.URL.Query().Get("code") - _, _ = colour.Infoln("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) - _, _ = colour.Infoln("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 { - _, _ = colour.Infoln("Opening", url, "in", browser) + colour.Infoln("Opening", url, "in", browser) switch runtime.GOOS { case "windows": cmd := exec.Command("cmd", "/C", "start", browser, url) @@ -162,10 +162,10 @@ func WaitUntilURLIsReachable(url string) error { resp, err := http.Get(url) if err != nil { - _, _ = colour.Infof("URL is not reachable: %v", err) + colour.Infof("URL is not reachable: %v", err) fmt.Print("\n") } else { - _, _ = colour.Infof("URL is reachable: %s - Status Code: %d", 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 b30ddf1..41b99a5 100644 --- a/internal/platforms/linkedin/preview.go +++ b/internal/platforms/linkedin/preview.go @@ -37,11 +37,11 @@ func NewPreview(ctx context.Context, args config.Args, urls []string) (preview, if p.title, p.thumbnailURL, err = extractFromURL(ctx, urls[0]); err != nil { if errors.Is(err, errNoTitleElementFound) || p.title == "" { - _, _ = colour.Infoln("Setting title to", urls[0]) + colour.Infoln("Setting title to", urls[0]) p.title = urls[0] } if errors.Is(err, errNoImageElementFound) { - _, _ = colour.Infoln("URL", urls[0], "without any image, that's fine, though.") + colour.Infoln("URL", urls[0], "without any image, that's fine, though.") } if !errors.Is(err, errNoTitleElementFound) && !errors.Is(err, errNoImageElementFound) { return p, err @@ -52,7 +52,7 @@ func NewPreview(ctx context.Context, args config.Args, urls []string) (preview, if p.thumbnailDownloadPath, err = p.DownloadImage(args.CacheDir); err != nil { return p, err } - _, _ = colour.Infoln("Downloaded preview image to ", p.thumbnailDownloadPath) + colour.Infoln("Downloaded preview image to ", p.thumbnailDownloadPath) } return p, nil } diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index abcbea9..938cf6f 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -23,7 +23,7 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) return err } if args.DryRun { - _, _ = colour.Infoln("Not posting", en, "to Mastodon as dry-run enabled") + colour.Infoln("Not posting", en, "to Mastodon as dry-run enabled") return nil } if content, err = prompt.FileAction("Do you want to post this message to Mastodon?", content, en.Path); err != nil { diff --git a/internal/platforms/platform.go b/internal/platforms/platform.go index 3cf02c9..e5bbd22 100644 --- a/internal/platforms/platform.go +++ b/internal/platforms/platform.go @@ -39,7 +39,7 @@ func (p Platform) String() string { } func (p Platform) Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) (err error) { - _, _ = colour.Infoln("Posting", en) + colour.Infoln("Posting", en) switch p.String() { case "mastodon": err = mastodon.Post(ctx, args, sizeLimit, en) diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 4a894dc..80f6dcd 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -52,7 +52,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 { - _, _ = colour.Infoln("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 { @@ -89,7 +89,7 @@ func queuePlatforms(args config.Args) error { return err } if share.Excluded(platform.String()) { - _, _ = colour.Infoln("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 { @@ -102,7 +102,7 @@ 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") - _, _ = colour.Infof("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 @@ -124,11 +124,11 @@ func queuePlatform(en entry.Entry, gosDir string, platform platforms.Platform) e // Entry already posted platform? if oi.IsRegular(postedFile) { - _, _ = colour.Infoln("Not re-queueing", destPath, "as", postedFile, "already exists") + colour.Infoln("Not re-queueing", destPath, "as", postedFile, "already exists") return nil } - _, _ = colour.Infoln("Queuing", en.Path, "->", destPath) + colour.Infoln("Queuing", en.Path, "->", destPath) return oi.CopyFile(en.Path, destPath) } @@ -143,7 +143,7 @@ func deleteFiles(path, suffix string, olderThan time.Time) error { return err } if fileInfo.ModTime().Before(olderThan) { - _, _ = colour.Infoln("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 f6cb327..c0d8234 100644 --- a/internal/run.go +++ b/internal/run.go @@ -17,7 +17,7 @@ func Run(ctx context.Context, args config.Args) error { if !softError(err) { return err } - _, _ = colour.Infoln(err) + colour.Infoln(err) } for platformStr, sizeLimit := range args.Platforms { @@ -27,7 +27,7 @@ func Run(ctx context.Context, args config.Args) error { } if err := runPlatform(ctx, args, platform, sizeLimit); err != nil { if softError(err) { - _, _ = colour.Infoln(err) + colour.Infoln(err) continue } return err @@ -41,10 +41,10 @@ func runPlatform(ctx context.Context, args config.Args, platform platforms.Platf en, err := schedule.Run(args, platform) switch { case errors.Is(err, schedule.ErrNothingToSchedule): - _, _ = colour.Infoln("Nothing to be scheduled for", platform) + colour.Infoln("Nothing to be scheduled for", platform) return nil case errors.Is(err, schedule.ErrNothingQueued): - _, _ = colour.Infoln("Nothing queued for", platform) + colour.Infoln("Nothing queued for", platform) return nil case err != nil: return err diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 2985da2..9b31f88 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -77,7 +77,7 @@ func selectRandomEntry(dir, tag string) (entry.Entry, error) { } en, err := entry.New(filepath.Join(dir, file.Name())) if err != nil { - _, _ = colour.Infoln(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 b4f8153..a9d5ba4 100644 --- a/internal/schedule/stats.go +++ b/internal/schedule/stats.go @@ -81,13 +81,12 @@ func (s stats) targetHit(pauseDays, maxQueuedDays int) bool { pauseDays-- } if s.postsPerDay >= s.postsPerDayTarget { - _, _ = colour.Infoln("Posts per day target hit") + colour.Infoln("Posts per day target hit") return true } if s.lastPostDaysAgo <= float64(pauseDays) { - _, _ = colour.Infoln("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 - } return false } diff --git a/internal/tags/inline.go b/internal/tags/inline.go index 7f3142f..ee07f70 100644 --- a/internal/tags/inline.go +++ b/internal/tags/inline.go @@ -7,7 +7,6 @@ import ( "regexp" "strings" - "codeberg.org/snonux/gos/internal/colour" "codeberg.org/snonux/gos/internal/oi" "codeberg.org/snonux/gos/internal/platforms" ) @@ -29,7 +28,6 @@ func InlineExtract(filePath string) (string, error) { return filePath, nil } - _, _ = colour.Infof("Rewriting path '%s' to '%s' (inline tag extraction)", filePath, newFilePath) fmt.Print("\n") if err := oi.WriteFile(newFilePath, newContent); err != nil { return "", err |
