summaryrefslogtreecommitdiff
path: root/internal/oi
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-09-28 11:58:41 +0300
committerPaul Buetow <paul@buetow.org>2024-09-28 11:58:41 +0300
commit076c0d5afb299fedc5ee61a13bfec7f86055fc89 (patch)
tree241e13b67f062bcf2c87f1ed86b1e136a991d6ea /internal/oi
parent9dda6355b81f6e6b93fc67d846657a388fcce4b7 (diff)
can schedule entry
Diffstat (limited to 'internal/oi')
-rw-r--r--internal/oi/oi.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/internal/oi/oi.go b/internal/oi/oi.go
index fceafd2..27112bf 100644
--- a/internal/oi/oi.go
+++ b/internal/oi/oi.go
@@ -1,10 +1,14 @@
package oi
import (
+ "errors"
"fmt"
"io"
"os"
"path/filepath"
+ "time"
+
+ "golang.org/x/exp/rand"
)
func EnsureDirExists(dir string) error {
@@ -57,6 +61,19 @@ func ReadDirSlurp(dir string, filter func(file os.DirEntry) bool) ([]string, err
return files, nil
}
+func ReadDirRandomEntry(dir string, filter func(file os.DirEntry) bool) (string, error) {
+ files, err := ReadDirSlurp(dir, filter)
+ if err != nil {
+ return "", err
+ }
+ if len(files) == 0 {
+ return "", errors.New("no entry/file found")
+ }
+
+ rand.Seed(uint64(time.Now().UnixNano()))
+ return files[rand.Intn(len(files))], nil
+}
+
func IsRegular(path string) bool {
stat, err := os.Stat(path)
if err != nil {