From c26156d0805c3cfc90f607ac41e5740042509c57 Mon Sep 17 00:00:00 2001 From: redwrasse Date: Wed, 13 Mar 2024 18:26:27 -0700 Subject: [PATCH] move fileutil.readDirWithSuffix to normal function Signed-off-by: redwrasse --- client/pkg/fileutil/purge.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/client/pkg/fileutil/purge.go b/client/pkg/fileutil/purge.go index 765f5bb42ad..026ea03230f 100644 --- a/client/pkg/fileutil/purge.go +++ b/client/pkg/fileutil/purge.go @@ -52,28 +52,12 @@ func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval zap.Uint("max", max), zap.Duration("interval", interval)) - readDirWithSuffix := func(dirname string) ([]string, error) { - fnames, err := ReadDir(dirname) - if err != nil { - return nil, err - } - // filter in place (ref. https://go.dev/wiki/SliceTricks#filtering-without-allocating) - fnamesWithSuffix := fnames[:0] - for _, fname := range fnames { - if strings.HasSuffix(fname, suffix) { - fnamesWithSuffix = append(fnamesWithSuffix, fname) - } - } - - return fnamesWithSuffix, nil - } - go func() { if donec != nil { defer close(donec) } for { - fnamesWithSuffix, err := readDirWithSuffix(dirname) + fnamesWithSuffix, err := readDirWithSuffix(dirname, suffix) if err != nil { errC <- err return @@ -119,3 +103,18 @@ func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval }() return errC } + +func readDirWithSuffix(dirname string, suffix string) ([]string, error) { + fnames, err := ReadDir(dirname) + if err != nil { + return nil, err + } + // filter in place (ref. https://go.dev/wiki/SliceTricks#filtering-without-allocating) + fnamesWithSuffix := fnames[:0] + for _, fname := range fnames { + if strings.HasSuffix(fname, suffix) { + fnamesWithSuffix = append(fnamesWithSuffix, fname) + } + } + return fnamesWithSuffix, nil +}