diff options
| author | Paul Buetow <paul@buetow.org> | 2024-12-11 23:09:28 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-12-11 23:09:28 +0200 |
| commit | 121d2877cc7ed0aaf0bbe5c57b86abba982b0441 (patch) | |
| tree | 46893a05b71aaf6b25e971adda6126ae766eae7b /internal | |
| parent | 0f5ecb2715876d05bed8e54d7bd30754e57bcdc4 (diff) | |
initial summary support
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/config/args.go | 1 | ||||
| -rw-r--r-- | internal/entry/entry.go | 19 | ||||
| -rw-r--r-- | internal/platforms/platform.go | 14 | ||||
| -rw-r--r-- | internal/run.go | 5 | ||||
| -rw-r--r-- | internal/summary/summary.go | 52 |
5 files changed, 86 insertions, 5 deletions
diff --git a/internal/config/args.go b/internal/config/args.go index 4438189..615c607 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -22,6 +22,7 @@ type Args struct { SecretsConfigPath string Secrets Secrets OAuth2Browser string + SummaryFor []string } func (a *Args) ParsePlatforms(platformStrs string) error { diff --git a/internal/entry/entry.go b/internal/entry/entry.go index 70b8396..b866a19 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "regexp" "slices" "strings" @@ -105,6 +106,24 @@ func (en *Entry) Content() (string, []string, error) { return content, extractURLs(content), err } +// Returns the Name, e.g. foo.bar.baz from /path/foo.bar.baz.TIMESTAMP.posted +func (en *Entry) Name() string { + base := filepath.Base(en.Path) + parts := strings.Split(base, ".") + + offset := len(parts) - 1 + + switch en.State { + case Queued: + fallthrough + case Posted: + offset -= 2 + } + + // TODO: Unit test this + return strings.Join(parts[:offset], ".") +} + // Returns the content and also checks for the size limit func (en Entry) ContentWithLimit(sizeLimit int) (string, []string, error) { content, urls, err := en.Content() diff --git a/internal/platforms/platform.go b/internal/platforms/platform.go index e5bbd22..f9ff84d 100644 --- a/internal/platforms/platform.go +++ b/internal/platforms/platform.go @@ -62,23 +62,27 @@ func (p Platform) Post(ctx context.Context, args config.Args, sizeLimit int, en } func ExpandAliases(shareTag string) (string, error) { - a := make(map[string]struct{}, len(aliases)) + parts := strings.Split(shareTag, ":") if parts[0] != "share" { return "", fmt.Errorf("expected share tag, but got '%s' in '%s'", parts[0], shareTag) } elems := []string{"share"} - // Dedup + dedup := make(map[string]struct{}, len(aliases)) + for _, alias := range parts[1:] { - a[alias] = struct{}{} - } - for alias := range a { platformStr, ok := aliases[alias] if !ok { return "", fmt.Errorf("invalid platform alias '%s' in '%s'", alias, shareTag) } + if _, ok := dedup[platformStr]; ok { + // Duplicate, ignore + continue + } elems = append(elems, platformStr) + dedup[platformStr] = struct{}{} } + return strings.Join(elems, ":"), nil } diff --git a/internal/run.go b/internal/run.go index c0d8234..b3e4a1c 100644 --- a/internal/run.go +++ b/internal/run.go @@ -10,9 +10,14 @@ import ( "codeberg.org/snonux/gos/internal/prompt" "codeberg.org/snonux/gos/internal/queue" "codeberg.org/snonux/gos/internal/schedule" + "codeberg.org/snonux/gos/internal/summary" ) func Run(ctx context.Context, args config.Args) error { + if len(args.SummaryFor) > 0 { + return summary.Run(ctx, args) + } + if err := queue.Run(args); err != nil { if !softError(err) { return err diff --git a/internal/summary/summary.go b/internal/summary/summary.go new file mode 100644 index 0000000..ca38620 --- /dev/null +++ b/internal/summary/summary.go @@ -0,0 +1,52 @@ +package summary + +import ( + "context" + "fmt" + "path/filepath" + + "codeberg.org/snonux/gos/internal/colour" + "codeberg.org/snonux/gos/internal/config" + "codeberg.org/snonux/gos/internal/entry" +) + +func Run(ctx context.Context, args config.Args) error { + colour.Infoln("Generating summary for", args.SummaryFor) + entries := make(map[string]entry.Entry) + + for _, dateStr := range args.SummaryFor { + glob := filepath.Join(args.GosDir, "db/platforms/*/", fmt.Sprintf("*%s*-??????.posted", dateStr)) + + paths, err := filepath.Glob(glob) + if err != nil { + return err + } + for _, path := range paths { + en, err := entry.New(path) + if err != nil { + return err + } + // TODO: Dedup here by name + entries[en.Name()] = en + } + } + // ch, err := oi.ReadDirCh(args.GosDir, find(args.GosDir, validExtensions...)) + // if err != nil { + // return err + // } + + // for filePath := range ch { + return nil +} + +// func find(path string, suffixes ...string) func(os.DirEntry) (string, bool) { +// return func(file os.DirEntry) (string, bool) { +// filePath := filepath.Join(path, file.Name()) +// for _, suffix := range suffixes { +// if strings.HasSuffix(file.Name(), suffix) { +// return filePath, true +// } +// } +// return filePath, false +// } +// } |
