Remove ioutil.ReadDir from usage (#53550)

* add depguard rule for ioutil

* replace ioutil.ReadDir with os.ReadDir

* use legacy option in depguard supported in golangci-lint v1.40

* replace ioutil.ReadDir with os.ReadDir

* return error for file info
This commit is contained in:
Jo
2022-08-11 07:21:12 -04:00
committed by GitHub
parent b42f3e2c4c
commit ca72cd570e
18 changed files with 62 additions and 46 deletions
+10 -4
View File
@@ -3,7 +3,7 @@ package cleanup
import (
"context"
"errors"
"io/ioutil"
"io/fs"
"os"
"path"
"time"
@@ -107,17 +107,23 @@ func (srv *CleanUpService) cleanUpTmpFolder(folder string) {
return
}
files, err := ioutil.ReadDir(folder)
files, err := os.ReadDir(folder)
if err != nil {
srv.log.Error("Problem reading dir", "folder", folder, "error", err)
return
}
var toDelete []os.FileInfo
var toDelete []fs.DirEntry
var now = time.Now()
for _, file := range files {
if srv.shouldCleanupTempFile(file.ModTime(), now) {
info, err := file.Info()
if err != nil {
srv.log.Error("Problem reading file", "folder", folder, "file", file, "error", err)
continue
}
if srv.shouldCleanupTempFile(info.ModTime(), now) {
toDelete = append(toDelete, file)
}
}
+1 -2
View File
@@ -3,7 +3,6 @@ package notifier
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -53,7 +52,7 @@ func PersistTemplates(cfg *api.PostableUserConfig, path string) ([]string, bool,
}
// Now that we have the list of _actual_ templates, let's remove the ones that we don't need.
existingFiles, err := ioutil.ReadDir(path)
existingFiles, err := os.ReadDir(path)
if err != nil {
cfglogger.Error("unable to read directory for deleting Alertmanager templates", "err", err, "path", path)
}
+1 -2
View File
@@ -2,7 +2,6 @@ package notifier
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -80,7 +79,7 @@ func TestPersistTemplates(t *testing.T) {
paths, changed, persistErr := PersistTemplates(c, dir)
files := map[string]string{}
readFiles, err := ioutil.ReadDir(dir)
readFiles, err := os.ReadDir(dir)
require.NoError(t, err)
for _, f := range readFiles {
if f.IsDir() || f.Name() == "" {
@@ -3,7 +3,7 @@ package notifier
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"sync"
@@ -245,7 +245,7 @@ func (moa *MultiOrgAlertmanager) SyncAlertmanagersForOrgs(ctx context.Context, o
func (moa *MultiOrgAlertmanager) cleanupOrphanLocalOrgState(ctx context.Context,
activeOrganizations map[int64]struct{}) {
dataDir := filepath.Join(moa.settings.DataPath, workingDir)
files, err := ioutil.ReadDir(dataDir)
files, err := os.ReadDir(dataDir)
if err != nil {
moa.logger.Error("failed to list local working directory", "dir", dataDir, "err", err)
return
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -27,7 +26,7 @@ func (cr *rulesConfigReader) readConfig(ctx context.Context, path string) ([]*Al
var alertFiles []*AlertingFile
cr.log.Debug("looking for alerting provisioning files", "path", path)
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
cr.log.Error("can't read alerting provisioning files from directory", "path", path, "error", err)
return alertFiles, nil
@@ -62,7 +61,7 @@ func (cr *rulesConfigReader) isJSON(file string) bool {
return strings.HasSuffix(file, ".json")
}
func (cr *rulesConfigReader) parseConfig(path string, file fs.FileInfo) (*AlertingFileV1, error) {
func (cr *rulesConfigReader) parseConfig(path string, file fs.DirEntry) (*AlertingFileV1, error) {
filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
// nolint:gosec
// We can ignore the gosec G304 warning on this one because `filename` comes from ps.Cfg.ProvisioningPath
@@ -3,7 +3,7 @@ package dashboards
import (
"context"
"fmt"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"strings"
@@ -19,7 +19,7 @@ type configReader struct {
orgStore utils.OrgStore
}
func (cr *configReader) parseConfigs(file os.FileInfo) ([]*config, error) {
func (cr *configReader) parseConfigs(file fs.DirEntry) ([]*config, error) {
filename, _ := filepath.Abs(filepath.Join(cr.path, file.Name()))
// nolint:gosec
@@ -67,7 +67,7 @@ func (cr *configReader) parseConfigs(file os.FileInfo) ([]*config, error) {
func (cr *configReader) readConfig(ctx context.Context) ([]*config, error) {
var dashboards []*config
files, err := ioutil.ReadDir(cr.path)
files, err := os.ReadDir(cr.path)
if err != nil {
cr.log.Error("can't read dashboard provisioning files from directory", "path", cr.path, "error", err)
return dashboards, nil
@@ -3,7 +3,7 @@ package datasources
import (
"context"
"fmt"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"strings"
@@ -23,7 +23,7 @@ type configReader struct {
func (cr *configReader) readConfig(ctx context.Context, path string) ([]*configs, error) {
var datasources []*configs
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
cr.log.Error("can't read datasource provisioning files from directory", "path", path, "error", err)
return datasources, nil
@@ -50,7 +50,7 @@ func (cr *configReader) readConfig(ctx context.Context, path string) ([]*configs
return datasources, nil
}
func (cr *configReader) parseDatasourceConfig(path string, file os.FileInfo) (*configs, error) {
func (cr *configReader) parseDatasourceConfig(path string, file fs.DirEntry) (*configs, error) {
filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
// nolint:gosec
@@ -3,7 +3,7 @@ package notifiers
import (
"context"
"fmt"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"strings"
@@ -29,7 +29,7 @@ func (cr *configReader) readConfig(ctx context.Context, path string) ([]*notific
var notifications []*notificationsAsConfig
cr.log.Debug("Looking for alert notification provisioning files", "path", path)
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
cr.log.Error("Can't read alert notification provisioning files from directory", "path", path, "error", err)
return notifications, nil
@@ -65,7 +65,7 @@ func (cr *configReader) readConfig(ctx context.Context, path string) ([]*notific
return notifications, nil
}
func (cr *configReader) parseNotificationConfig(path string, file os.FileInfo) (*notificationsAsConfig, error) {
func (cr *configReader) parseNotificationConfig(path string, file fs.DirEntry) (*notificationsAsConfig, error) {
filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
// nolint:gosec
@@ -3,7 +3,7 @@ package plugins
import (
"context"
"fmt"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"strings"
@@ -30,7 +30,7 @@ func (cr *configReaderImpl) readConfig(ctx context.Context, path string) ([]*plu
var apps []*pluginsAsConfig
cr.log.Debug("Looking for plugin provisioning files", "path", path)
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
cr.log.Error("Failed to read plugin provisioning files from directory", "path", path, "error", err)
return apps, nil
@@ -65,7 +65,7 @@ func (cr *configReaderImpl) readConfig(ctx context.Context, path string) ([]*plu
return apps, nil
}
func (cr *configReaderImpl) parsePluginConfig(path string, file os.FileInfo) (*pluginsAsConfig, error) {
func (cr *configReaderImpl) parsePluginConfig(path string, file fs.DirEntry) (*pluginsAsConfig, error) {
filename, err := filepath.Abs(filepath.Join(path, file.Name()))
if err != nil {
return nil, err