diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-31 23:09:54 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-31 23:09:54 +0200 |
| commit | c2b3c4f6e4d40c49d55deb5b696bb3cb8cdde531 (patch) | |
| tree | 1f83f75bf085dc1b02e7562cfcb759f4d8c6486c | |
| parent | 1c143f2e4faeb803f5f7c96f29c1ef447a0b2097 (diff) | |
intial warning when there aren't enough queued
| -rw-r--r-- | cmd/gos/main.go | 2 | ||||
| -rw-r--r-- | internal/config/args.go | 1 | ||||
| -rw-r--r-- | internal/oi/oi.go | 8 | ||||
| -rw-r--r-- | internal/prompt/file.go | 6 | ||||
| -rw-r--r-- | internal/prompt/prompt.go | 11 | ||||
| -rw-r--r-- | internal/queue/queue.go | 1 | ||||
| -rw-r--r-- | internal/run.go | 1 | ||||
| -rw-r--r-- | internal/schedule/schedule.go | 24 |
8 files changed, 32 insertions, 22 deletions
diff --git a/cmd/gos/main.go b/cmd/gos/main.go index 4926bf0..af5a619 100644 --- a/cmd/gos/main.go +++ b/cmd/gos/main.go @@ -26,6 +26,7 @@ func main() { secretsConfigPath = *flag.String("secretsConfig", secretsConfigPath, "Gos' secret config") platforms := flag.String("platforms", "Mastodon:500,LinkedIn:1000", "Platforms enabled plus their post size limits") target := flag.Int("target", 2, "How many posts per week are the target?") + minQueued := flag.Int("minQueued", 4, "Minimum of queued items until printing a warn message!") pauseDays := flag.Int("pauseDays", 3, "How many days until next post can be posted?") lookback := flag.Int("lookback", 30, "How many days look back in time for posting history") flag.Parse() @@ -40,6 +41,7 @@ func main() { GosDir: *gosDir, Platforms: make(map[string]int), Target: *target, + MinQueued: *minQueued, // TODO: Document PauseDays: *pauseDays, Lookback: time.Duration(*lookback) * time.Hour * 24, SecretsConfigPath: secretsConfigPath, diff --git a/internal/config/args.go b/internal/config/args.go index 995853a..b06d2e2 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -14,6 +14,7 @@ type Args struct { DryRun bool Platforms map[string]int // Platform name and post size limits Target int + MinQueued int PauseDays int Lookback time.Duration SecretsConfigPath string diff --git a/internal/oi/oi.go b/internal/oi/oi.go index b2a0953..2aa4417 100644 --- a/internal/oi/oi.go +++ b/internal/oi/oi.go @@ -84,20 +84,20 @@ func ReadDir[T any](dir string, cb func(file os.DirEntry) (T, bool)) ([]T, error return results, nil } -func ReadDirRandom[T any](dir string, cb func(file os.DirEntry) (T, bool)) (T, error) { +func ReadDirRandom[T any](dir string, cb func(file os.DirEntry) (T, bool)) (T, int, error) { results, err := ReadDir(dir, cb) if err != nil { var zero T - return zero, err + return zero, 0, err } if len(results) == 0 { var zero T - return zero, ErrNotFound + return zero, 0, ErrNotFound } rand.Seed(uint64(time.Now().UnixNano())) - return results[rand.Intn(len(results))], nil + return results[rand.Intn(len(results))], len(results), nil } func IsRegular(path string) bool { diff --git a/internal/prompt/file.go b/internal/prompt/file.go index d6d5d7c..ac72af5 100644 --- a/internal/prompt/file.go +++ b/internal/prompt/file.go @@ -17,14 +17,14 @@ var ( ) func FileAction(question, content, filePath string) error { - info2(filePath + ":") + Info2(filePath + ":") fmt.Print("\n") - info1(content) + Info1(content) fmt.Print("\n") reader := bufio.NewReader(os.Stdin) for { - ack("%s (y=yes/n=no/e=edit/d=delete):", question) + Ack("%s (y=yes/n=no/e=edit/d=delete):", question) input, err := reader.ReadString('\n') if err != nil { fmt.Println("Error reading input:", err) diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index 29ed86d..ab09d4b 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -9,15 +9,16 @@ import ( ) var ( - info1 = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc() - info2 = color.New(color.FgHiYellow, color.BgHiBlack, color.Bold).PrintfFunc() - ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc() + Info1 = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc() + Info2 = color.New(color.FgHiYellow, color.BgHiBlack, color.Bold).PrintfFunc() + Ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc() + Warn = color.New(color.FgBlack, color.BgHiYellow, color.Bold).PrintfFunc() ) func Acknowledge(message, content string) error { - info1(content) + Info1(content) fmt.Print("\n") - ack(message + " (press enter)") + Ack(message + " (press enter)") reader := bufio.NewReader(os.Stdin) if _, err := reader.ReadString('\n'); err != nil { return err diff --git a/internal/queue/queue.go b/internal/queue/queue.go index f6209f3..80c9d3d 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -18,7 +18,6 @@ import ( // Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files. var validExtensions = []string{".txt", ".md"} -// TODO: Red alert when there are no messages to schedule, or less than N func Run(args config.Args) error { if err := queueEntries(args); err != nil { return err diff --git a/internal/run.go b/internal/run.go index 714687a..1307d58 100644 --- a/internal/run.go +++ b/internal/run.go @@ -68,6 +68,7 @@ func runPlatform(ctx context.Context, args config.Args, platform string, sizeLim return err } + // TODO: Put all color definitions into ints own package color.New(color.FgWhite, color.BgGreen).Println("Successfully posted message to ", platform) return nil } diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 473ddc8..b2c2702 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -12,6 +12,7 @@ import ( "codeberg.org/snonux/gos/internal/config" "codeberg.org/snonux/gos/internal/entry" "codeberg.org/snonux/gos/internal/oi" + "codeberg.org/snonux/gos/internal/prompt" ) var ( @@ -28,13 +29,13 @@ func Run(args config.Args, platform string) (entry.Entry, error) { log.Println("For", platform, "stats:", stats) // Schedule random queued entry with "now" tag, ignoring the target hit stats. - ent, err := selectRandomEntry(dir, "now") + en, numQueued, err := selectRandomEntry(dir, "now") if err != nil && !errors.Is(err, oi.ErrNotFound) { // Unknown error - return ent, nil + return en, nil } if err == nil { - return ent, nil + return en, nil } if stats.targetHit(args.PauseDays) { @@ -42,30 +43,35 @@ func Run(args config.Args, platform string) (entry.Entry, error) { } // Schedule random qeued entry for platform. Find one with prio tag. - ent, err = selectRandomEntry(dir, "prio") + en, _, err = selectRandomEntry(dir, "prio") if errors.Is(err, oi.ErrNotFound) { // No entry with priority tag found, select another one. - ent, err = selectRandomEntry(dir, "") + en, numQueued, err = selectRandomEntry(dir, "") } if err != nil { return entry.Zero, fmt.Errorf("%w: %w", ErrNothingQueued, err) } - return ent, nil + // TODO: Fix this, it warns only when there are no now and prio tags used. + if numQueued < args.MinQueued { + prompt.Warn("Only %d items queued for %s, want to have %d", numQueued, platform, args.MinQueued) + fmt.Print("\n") + } + return en, nil } // Select a random queed entry with a given tag. If the tag is the empty string, // then select any random qeued entry. -func selectRandomEntry(dir, tag string) (entry.Entry, error) { +func selectRandomEntry(dir, tag string) (entry.Entry, int, error) { return oi.ReadDirRandom(dir, func(file os.DirEntry) (entry.Entry, bool) { // Is there a ".TAG." in the file name? if tag != "" && !slices.Contains(strings.Split(file.Name(), "."), tag) { return entry.Zero, false } - ent, err := entry.New(filepath.Join(dir, file.Name())) + en, err := entry.New(filepath.Join(dir, file.Name())) if err != nil { log.Println(err) return entry.Zero, false } - return ent, ent.State == entry.Queued + return en, en.State == entry.Queued }) } |
