diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-02 10:48:53 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-02 10:48:53 +0300 |
| commit | 87593db7993d1f3689d9602eed3ef095ac69cfb4 (patch) | |
| tree | 5d06908aa52ac43d0163224e320163cead88eb17 | |
| parent | a93d91223724b94480b7166970ede97d8d190bdb (diff) | |
rename some funcs
| -rw-r--r-- | internal/oi/oi.go | 28 | ||||
| -rw-r--r-- | internal/queue/queue.go | 6 | ||||
| -rw-r--r-- | internal/schedule/schedule.go | 3 |
3 files changed, 16 insertions, 21 deletions
diff --git a/internal/oi/oi.go b/internal/oi/oi.go index 967109d..1f7de6d 100644 --- a/internal/oi/oi.go +++ b/internal/oi/oi.go @@ -13,22 +13,21 @@ import ( var ErrNotFound = errors.New("no file/entry found") -func EnsureDirExists(dir string) error { +func EnsureDir(dir string) error { if _, err := os.Stat(dir); os.IsNotExist(err) { return os.MkdirAll(dir, os.ModePerm) } return nil } -func EnsureParentDirExists(dir string) error { - return EnsureDirExists(filepath.Dir(dir)) +func EnsureParentDir(dir string) error { + return EnsureDir(filepath.Dir(dir)) } -// Rename to ReadDirCh -func ReadDirFilter[T any](dir string, cb func(file os.DirEntry) (T, bool)) (chan T, error) { +func ReadDirCh[T any](dir string, cb func(file os.DirEntry) (T, bool)) (chan T, error) { ch := make(chan T) - if err := EnsureDirExists(dir); err != nil { + if err := EnsureDir(dir); err != nil { return ch, err } @@ -48,8 +47,9 @@ func ReadDirFilter[T any](dir string, cb func(file os.DirEntry) (T, bool)) (chan return ch, nil } + func TraverseDir(dir string, cb func(file os.DirEntry) error) error { - if err := EnsureDirExists(dir); err != nil { + if err := EnsureDir(dir); err != nil { return err } @@ -68,11 +68,10 @@ func TraverseDir(dir string, cb func(file os.DirEntry) error) error { return errors.Join(errs...) } -// Rename to ReadDir -func ReadDirSlurp[T any](dir string, cb func(file os.DirEntry) (T, bool)) ([]T, error) { +func ReadDir[T any](dir string, cb func(file os.DirEntry) (T, bool)) ([]T, error) { var results []T - ch, err := ReadDirFilter(dir, cb) + ch, err := ReadDirCh(dir, cb) if err != err { return results, err } @@ -84,9 +83,8 @@ func ReadDirSlurp[T any](dir string, cb func(file os.DirEntry) (T, bool)) ([]T, return results, nil } -// Rename to ReadDirRandom -func ReadDirRandomEntry[T any](dir string, cb func(file os.DirEntry) (T, bool)) (T, error) { - results, err := ReadDirSlurp(dir, cb) +func ReadDirRandom[T any](dir string, cb func(file os.DirEntry) (T, bool)) (T, error) { + results, err := ReadDir(dir, cb) if err != nil { var zero T @@ -120,7 +118,7 @@ func CopyFile(srcPath, dstPath string) error { } defer source.Close() - if err := EnsureParentDirExists(dstPath); err != nil { + if err := EnsureParentDir(dstPath); err != nil { return err } @@ -135,7 +133,7 @@ func CopyFile(srcPath, dstPath string) error { } func Rename(srcPath, dstPath string) error { - if err := EnsureParentDirExists(dstPath); err != nil { + if err := EnsureParentDir(dstPath); err != nil { return err } return os.Rename(srcPath, dstPath) diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 94ea1e0..ae8e377 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -29,7 +29,7 @@ func queueEntries(args config.Args) error { // Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files. var validExtensions = []string{".txt", ".md"} - ch, err := oi.ReadDirFilter(args.GosDir, func(file os.DirEntry) (string, bool) { + ch, err := oi.ReadDirCh(args.GosDir, func(file os.DirEntry) (string, bool) { filePath := filepath.Join(args.GosDir, file.Name()) return filePath, slices.Contains(validExtensions, filepath.Ext(file.Name())) && file.Type().IsRegular() @@ -54,7 +54,7 @@ func queueEntries(args config.Args) error { // for each PLATFORM func queuePlatforms(args config.Args) error { dbDir := filepath.Join(args.GosDir, "db") - ch, err := oi.ReadDirFilter(dbDir, func(file os.DirEntry) (string, bool) { + ch, err := oi.ReadDirCh(dbDir, func(file os.DirEntry) (string, bool) { filePath := filepath.Join(dbDir, file.Name()) return filePath, strings.HasSuffix(file.Name(), ".queued") }) @@ -81,9 +81,7 @@ func queuePlatforms(args config.Args) error { // Queue ./db/queued/*.txt.STAMP.queued to ./db/platforms/PLATFORM/*.txt.STAMP.queued func queuePlatform(entryPath, gosDir, platform string) error { destDir := filepath.Join(gosDir, "db/platform", strings.ToLower(platform)) - // destDir := fmt.Sprintf("%s/db/platforms/%s/", gosDir, strings.ToLower(platform)) destPath := filepath.Join(destDir, filepath.Base(entryPath)) - // destPath := fmt.Sprintf("%s/%s", destDir, filepath.Base(entryPath)) postedFile := fmt.Sprintf("%s.posted", strings.TrimSuffix(destPath, ".queued")) // Entry already posted platform? diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 4ed3094..171f208 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -32,8 +32,7 @@ func Run(args config.Args, platform string) (entry.Entry, error) { } // Schedule random qeued entry for platform - // TODO: Rename ReadDirRandomEntry to ReadDirRandom - ent, err := oi.ReadDirRandomEntry(dir, func(file os.DirEntry) (entry.Entry, bool) { + ent, err := oi.ReadDirRandom(dir, func(file os.DirEntry) (entry.Entry, bool) { if ent, err := entry.New(filepath.Join(dir, file.Name())); err != nil { return ent, ent.State == entry.Queued } |
