diff options
| -rw-r--r-- | internal/entry/entry.go | 84 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 14 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 8 | ||||
| -rw-r--r-- | internal/queue/queue.go | 9 |
4 files changed, 57 insertions, 58 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go index d757632..040b509 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -53,132 +53,132 @@ type Entry struct { simpleTags []string } -func (e Entry) String() string { - if e.State == Inboxed { - return fmt.Sprintf("Path:%s;State:%s", e.Path, e.State) +func (en Entry) String() string { + if en.State == Inboxed { + return fmt.Sprintf("Path:%s;State:%s", en.Path, en.State) } - return fmt.Sprintf("Path:%s;Stamp:%s,State:%s", e.Path, e.Time.Format(timestamp.Format), e.State) + return fmt.Sprintf("Path:%s;Stamp:%s,State:%s", en.Path, en.Time.Format(timestamp.Format), en.State) } // filePath format: /foo/foobarbaz.something.here.txt.STAMP.{posted,queued} // or for inboxed: /foo.txt // or inboxed with tags: /foo.prio.ask.txt func New(filePath string) (Entry, error) { - e := Entry{Path: filePath} + en := Entry{Path: filePath} // We want to get the STAMP! parts := strings.Split(filePath, ".") if len(parts) < 2 { // Could be 2 if inboxed - return e, fmt.Errorf("not a valid entry path: %s", filePath) + return en, fmt.Errorf("not a valid entry path: %s", filePath) } for _, part := range parts { if slices.Contains(validTags, part) { - e.simpleTags = append(e.simpleTags, part) + en.simpleTags = append(en.simpleTags, part) } } switch parts[len(parts)-1] { case "queued": - e.State = Queued + en.State = Queued case "posted": - e.State = Posted + en.State = Posted default: - e.State = Inboxed - return e, nil + en.State = Inboxed + return en, nil } if len(parts) < 4 { // If not inboxed, must be longer. - return e, fmt.Errorf("not a valid entry path: %s", filePath) + return en, fmt.Errorf("not a valid entry path: %s", filePath) } var err error - if e.Time, err = timestamp.Parse(parts[len(parts)-2]); err != nil { - return e, err + if en.Time, err = timestamp.Parse(parts[len(parts)-2]); err != nil { + return en, err } - if e.Time.Before(timestamp.OldestValidTime()) { - return e, fmt.Errorf("entry time does not seem legit, it is too old: %v", e.Time) + if en.Time.Before(timestamp.OldestValidTime()) { + return en, fmt.Errorf("entry time does not seem legit, it is too old: %v", en.Time) } - return e, nil + return en, nil } -func (e *Entry) Content() (string, []string, error) { - content, err := oi.SlurpAndTrim(e.Path) +func (en *Entry) Content() (string, []string, error) { + content, err := oi.SlurpAndTrim(en.Path) return content, extractURLs(content), err } -func (e Entry) ContentWithLimit(sizeLimit int) (string, []string, error) { - content, urls, err := e.Content() +func (en Entry) ContentWithLimit(sizeLimit int) (string, []string, error) { + content, urls, err := en.Content() if err != nil { return "", urls, err } if len(content) > sizeLimit { - err := fmt.Errorf("%w (%d > %d): %v", ErrSizeLimitExceeded, len(content), sizeLimit, e) + err := fmt.Errorf("%w (%d > %d): %v", ErrSizeLimitExceeded, len(content), sizeLimit, en) if err2 := prompt.Acknowledge("You need to shorten the content as "+err.Error(), content); err2 != nil { return "", urls, errors.Join(err, err2) } - if err2 := e.Edit(); err2 != nil { + if err2 := en.Edit(); err2 != nil { return "", urls, errors.Join(err, err2) } - return e.ContentWithLimit(sizeLimit) + return en.ContentWithLimit(sizeLimit) } return content, urls, nil } -func (e *Entry) MarkPosted() error { - if e.State == Inboxed { +func (en *Entry) MarkPosted() error { + if en.State == Inboxed { return errors.New("entry still inboxed, can not mark as posted") } - if e.State != Queued { + if en.State != Queued { return errors.New("entry is not queued") } - if e.State == Posted { + if en.State == Posted { return errors.New("entry is already posted") } - newPath, err := timestamp.UpdateInFilename(strings.TrimSuffix(e.Path, ".queued")+".posted", -2) + newPath, err := timestamp.UpdateInFilename(strings.TrimSuffix(en.Path, ".queued")+".posted", -2) if err != nil { return err } - if err := os.Rename(e.Path, newPath); err != nil { + if err := os.Rename(en.Path, newPath); err != nil { return err } - e.State = Posted + en.State = Posted return nil } -func (e Entry) HasTag(tag string) bool { - return slices.Contains(e.simpleTags, tag) +func (en Entry) HasTag(tag string) bool { + return slices.Contains(en.simpleTags, tag) } // Valid tags are: share:foo[,...] // whereas foo can be a supported plutform such as linkedin, mastodon, etc. // foo can also be prefixed with - to exclude it. See unit tests for examples. -func (e Entry) ExcludedByTags(args config.Args, platform string) (bool, error) { - s, err := newShareTags(args, e.Path) +func (en Entry) ExcludedByTags(args config.Args, platform string) (bool, error) { + s, err := newShareTags(args, en.Path) return slices.Contains(s.excludes, strings.ToLower(platform)) || !slices.Contains(s.includes, strings.ToLower(platform)), err } -func (e Entry) Edit() error { - if err := prompt.EditFile(e.Path); err != nil { +func (en Entry) Edit() error { + if err := prompt.EditFile(en.Path); err != nil { return err } return nil } -func (e Entry) Remove() error { - return os.Remove(e.Path) +func (en Entry) Remove() error { + return os.Remove(en.Path) } -func (e Entry) FileAction(question string) error { - content, _, err := e.Content() +func (en Entry) FileAction(question string) error { + content, _, err := en.Content() if err != nil { return err } - return prompt.FileAction(question, content, e.Path) + return prompt.FileAction(question, content, en.Path) } func extractURLs(input string) []string { diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 0c5de92..bb40a65 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -24,19 +24,19 @@ const ( linkedInTimeout = 10 * time.Second ) -func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { - err := post(ctx, args, sizeLimit, ent) +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") args.Secrets.LinkedInAccessToken = "" // Reset the token - return post(ctx, args, sizeLimit, ent) + return post(ctx, args, sizeLimit, en) } return err } -func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { +func post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) error { if args.DryRun { - log.Println("Not posting", ent, "to LinkedIn as dry-run enabled") + log.Println("Not posting", en, "to LinkedIn as dry-run enabled") return nil } @@ -46,7 +46,7 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) if err != nil { return err } - content, urls, err := ent.ContentWithLimit(sizeLimit) + content, urls, err := en.ContentWithLimit(sizeLimit) if err != nil { return err } @@ -59,7 +59,7 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) } question := fmt.Sprintf("Do you want to post this message to Linkedin (%v)?", prev) - if err := prompt.FileAction(question, content, ent.Path); err != nil { + if err := prompt.FileAction(question, content, en.Path); err != nil { return err } diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index 919d39d..b6645f8 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -17,8 +17,8 @@ import ( const mastodonTimeout = 10 * time.Second -func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { - content, _, err := ent.ContentWithLimit(sizeLimit) +func Post(ctx context.Context, args config.Args, sizeLimit int, en entry.Entry) error { + content, _, err := en.ContentWithLimit(sizeLimit) if err != nil { return err } @@ -28,10 +28,10 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) return fmt.Errorf("failed to marshal payload: %w", err) } if args.DryRun { - log.Println("Not posting", ent, "to Mastodon as dry-run enabled") + log.Println("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, ent.Path); err != nil { + if err := prompt.FileAction("Do you want to post this message to Mastodon?", content, en.Path); err != nil { return err } diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 6df59df..f6209f3 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -112,10 +112,9 @@ func queuePlatforms(args config.Args) error { } // Queue ./db/queued/*.txt.STAMP.queued to ./db/platforms/PLATFORM/*.txt.STAMP.queued -// TODO: Rename all ent to en -func queuePlatform(ent entry.Entry, gosDir, platform string) error { +func queuePlatform(en entry.Entry, gosDir, platform string) error { destDir := filepath.Join(gosDir, "db/platforms", strings.ToLower(platform)) - destPath := filepath.Join(destDir, filepath.Base(ent.Path)) + destPath := filepath.Join(destDir, filepath.Base(en.Path)) postedFile := fmt.Sprintf("%s.posted", strings.TrimSuffix(destPath, ".queued")) // Entry already posted platform? @@ -124,8 +123,8 @@ func queuePlatform(ent entry.Entry, gosDir, platform string) error { return nil } - log.Println("Queuing", ent.Path, "->", destPath) - return oi.CopyFile(ent.Path, destPath) + log.Println("Queuing", en.Path, "->", destPath) + return oi.CopyFile(en.Path, destPath) } func deleteFiles(path, suffix string, olderThan time.Time) error { |
