diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/main.go | 2 | ||||
| -rw-r--r-- | internal/platforms/platform.go | 3 | ||||
| -rw-r--r-- | internal/platforms/snonux/snonux.go | 70 | ||||
| -rw-r--r-- | internal/platforms/snonux/snonux_test.go | 31 | ||||
| -rw-r--r-- | internal/run.go | 5 | ||||
| -rw-r--r-- | internal/summary/summary.go | 8 | ||||
| -rw-r--r-- | internal/version.go | 2 |
7 files changed, 110 insertions, 11 deletions
diff --git a/internal/main.go b/internal/main.go index ea5d527..efc4782 100644 --- a/internal/main.go +++ b/internal/main.go @@ -25,7 +25,7 @@ func Main(composeModeDefault bool) { // cacheDir := flag.String("cacheDir", filepath.Join(*gosDir, "cache"), "Go's cache dir") browser := flag.String("browser", "firefox", "OAuth2 browser") configPath := flag.String("configPath", filepath.Join(os.Getenv("HOME"), ".config/gos/gos.json"), "Gos' config file path") - platforms := flag.String("platforms", "Mastodon:500,LinkedIn:1000,Noop:2000", "Platforms enabled plus their post size limits") + platforms := flag.String("platforms", "Mastodon:500,LinkedIn:1000,Noop:2000,Snonux:2000", "Platforms enabled plus their post size limits") target := flag.Int("target", 2, "How many posts per week are the target?") minQueued := flag.Int("minQueued", 42, "Minimum of queued items until printing a warn message!") maxDaysQueued := flag.Int("maxDaysQueued", 365*2, "Maximum days worth of queued posts until target++ and pauseDays--") diff --git a/internal/platforms/platform.go b/internal/platforms/platform.go index 95cade2..ff46a57 100644 --- a/internal/platforms/platform.go +++ b/internal/platforms/platform.go @@ -11,6 +11,7 @@ import ( "codeberg.org/snonux/gos/internal/platforms/linkedin" "codeberg.org/snonux/gos/internal/platforms/mastodon" "codeberg.org/snonux/gos/internal/platforms/noop" + "codeberg.org/snonux/gos/internal/platforms/snonux" ) type Platform string @@ -54,7 +55,7 @@ func (p Platform) Post(ctx context.Context, args config.Args, sizeLimit int, en case "noop": err = noop.Post(ctx, args, sizeLimit, en) case "snonux": - err = noop.Post(ctx, args, sizeLimit, en) + err = snonux.Post(ctx, args, sizeLimit, en) default: err = fmt.Errorf("Platform '%s' (not yet) implemented", p) } diff --git a/internal/platforms/snonux/snonux.go b/internal/platforms/snonux/snonux.go new file mode 100644 index 0000000..5773a23 --- /dev/null +++ b/internal/platforms/snonux/snonux.go @@ -0,0 +1,70 @@ +package snonux + +import ( + "context" + "fmt" + "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" + "codeberg.org/snonux/gos/internal/prompt" + "codeberg.org/snonux/gos/internal/timestamp" +) + +// Post writes the entry body as plain text under <gosDir>/snonux/inbox (filename .md). +func Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) error { + if err := ctx.Err(); err != nil { + return err + } + content, _, err := en.ContentWithLimit(sizeLimit) + if err != nil { + return err + } + if args.DryRun { + colour.Infoln("Not writing", en, "to Snonux inbox as dry-run enabled") + return nil + } + if _, err = prompt.FileAction("Do you want to post this message to Snonux (save to inbox)?", + content, en.Path, prompt.RandomOption); err != nil { + return err + } + path, err := inboxMarkdownPath(args.GosDir, en) + if err != nil { + return err + } + if err := oi.EnsureParentDir(path); err != nil { + return err + } + out := strings.TrimSuffix(content, "\n") + "\n" + if err := oi.WriteFile(path, out); err != nil { + return err + } + colour.Infoln("Wrote Snonux inbox file", path) + return nil +} + +func inboxMarkdownPath(gosDir string, en entry.Entry) (string, error) { + inbox := filepath.Join(gosDir, "snonux", "inbox") + base := strings.TrimSuffix(filepath.Base(en.Path), ".queued") + base = strings.Map(func(r rune) rune { + switch { + case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '-', r == '_', r == '.': + return r + default: + return '-' + } + }, base) + base = strings.Trim(base, "-.") + if base == "" { + base = "post" + } + if len(base) > 120 { + base = base[:120] + } + name := fmt.Sprintf("%s-%09d-%s.md", timestamp.Now(), time.Now().Nanosecond(), base) + return filepath.Join(inbox, name), nil +} diff --git a/internal/platforms/snonux/snonux_test.go b/internal/platforms/snonux/snonux_test.go new file mode 100644 index 0000000..113e32d --- /dev/null +++ b/internal/platforms/snonux/snonux_test.go @@ -0,0 +1,31 @@ +package snonux + +import ( + "path/filepath" + "strings" + "testing" + + "codeberg.org/snonux/gos/internal/entry" +) + +func TestInboxMarkdownPath(t *testing.T) { + en, err := entry.New("/queue/foo.bar.share:snonux.txt.20250101-120000.queued") + if err != nil { + t.Fatal(err) + } + path, err := inboxMarkdownPath("/home/u/.gosdir", en) + if err != nil { + t.Fatal(err) + } + wantDir := filepath.FromSlash("/home/u/.gosdir/snonux/inbox") + if dir := filepath.Dir(path); dir != wantDir { + t.Errorf("dir = %q want %q", dir, wantDir) + } + base := filepath.Base(path) + if !strings.HasSuffix(base, ".md") { + t.Errorf("expected .md suffix, got %q", base) + } + if !strings.Contains(base, "foo") { + t.Errorf("expected entry name in filename, got %q", base) + } +} diff --git a/internal/run.go b/internal/run.go index 43874bf..695757b 100644 --- a/internal/run.go +++ b/internal/run.go @@ -67,11 +67,6 @@ func runPlatform(ctx context.Context, args config.Args, platform platforms.Platf return err } - if args.ComposeMode { - colour.Infoln("Not posting any entry in compose mode!") - return nil - } - err = platform.Post(ctx, args, sizeLimit, en) if errors.Is(err, prompt.ErrRamdomOther) || errors.Is(err, prompt.ErrDeleted) { return runPlatform(ctx, args, platform, sizeLimit) diff --git a/internal/summary/summary.go b/internal/summary/summary.go index 1a416c3..32232e9 100644 --- a/internal/summary/summary.go +++ b/internal/summary/summary.go @@ -11,6 +11,7 @@ import ( "codeberg.org/snonux/gos/internal/config" "codeberg.org/snonux/gos/internal/entry" + "codeberg.org/snonux/gos/internal/platforms" ) const maxLinkLength = 80 @@ -64,9 +65,6 @@ func generateGemtext(args config.Args, entries []entry.Entry, title string) (str sb.WriteString("\n\n### ") sb.WriteString(firstFewWords(content, 50)) - if err != err { - return "", err - } sb.WriteString("\n\n") sb.WriteString(content) @@ -98,6 +96,10 @@ func matchingEntries(args config.Args) iter.Seq2[entry.Entry, error] { return } for _, path := range paths { + rel, err := filepath.Rel(filepath.Join(args.GosDir, "db", "platforms"), filepath.Dir(path)) + if err == nil && strings.EqualFold(rel, platforms.Platform("snonux").String()) { + continue + } en, err := entry.New(path) if !yield(en, err) { return diff --git a/internal/version.go b/internal/version.go index c76328b..5e688a8 100644 --- a/internal/version.go +++ b/internal/version.go @@ -6,7 +6,7 @@ import ( "codeberg.org/snonux/gos/internal/table" ) -const versionStr = "v1.2.6" +const versionStr = "v1.3.0" func printVersion() { table.New(). |
