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 11:21:12 +00:00
committed by GitHub
parent b42f3e2c4c
commit ca72cd570e
18 changed files with 62 additions and 46 deletions

View File

@@ -13,6 +13,11 @@ ignore-generated-header = false
severity = "warning"
confidence = 3
[linters-settings.depguard]
list-type = "blacklist"
include-go-root = true
packages = ["io/ioutil"]
[linters-settings.gocritic]
enabled-checks = ["ruleguard"]
[linters-settings.gocritic.settings.ruleguard]

View File

@@ -3,8 +3,8 @@ package plugins
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
@@ -22,7 +22,7 @@ func Build(ctx context.Context, grafanaDir string, p syncutil.WorkerPool, g *err
log.Printf("Building plugins in %q...", grafanaDir)
root := filepath.Join(grafanaDir, "plugins-bundled", "internal")
fis, err := ioutil.ReadDir(root)
fis, err := os.ReadDir(root)
if err != nil {
return err
}

View File

@@ -1,12 +1,13 @@
package commandstest
import (
"io/fs"
"os"
"time"
)
type FakeIoUtil struct {
FakeReadDir []os.FileInfo
FakeReadDir []fs.DirEntry
FakeIsDirectory bool
}
@@ -18,7 +19,7 @@ func (util *FakeIoUtil) RemoveAll(path string) error {
return nil
}
func (util *FakeIoUtil) ReadDir(path string) ([]os.FileInfo, error) {
func (util *FakeIoUtil) ReadDir(path string) ([]fs.DirEntry, error) {
return util.FakeReadDir, nil
}

View File

@@ -1,6 +1,7 @@
package models
import (
"io/fs"
"os"
)
@@ -49,6 +50,6 @@ type PluginRepo struct {
type IoUtil interface {
Stat(path string) (os.FileInfo, error)
RemoveAll(path string) error
ReadDir(path string) ([]os.FileInfo, error)
ReadDir(path string) ([]fs.DirEntry, error)
ReadFile(filename string) ([]byte, error)
}

View File

@@ -1,7 +1,7 @@
package services
import (
"io/ioutil"
"io/fs"
"os"
)
@@ -16,8 +16,8 @@ func (i IoUtilImp) RemoveAll(path string) error {
return os.RemoveAll(path)
}
func (i IoUtilImp) ReadDir(path string) ([]os.FileInfo, error) {
return ioutil.ReadDir(path)
func (i IoUtilImp) ReadDir(path string) ([]fs.DirEntry, error) {
return os.ReadDir(path)
}
func (i IoUtilImp) ReadFile(filename string) ([]byte, error) {

View File

@@ -6,7 +6,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@@ -42,7 +41,7 @@ func main() {
cmroot := filepath.Join(groot, "pkg", "coremodel")
tsroot := filepath.Join(groot, "packages", "grafana-schema", "src", "schema")
items, err := ioutil.ReadDir(cmroot)
items, err := os.ReadDir(cmroot)
if err != nil {
fmt.Fprintf(os.Stderr, "could not read coremodels parent dir %s: %s\n", cmroot, err)
os.Exit(1)

View File

@@ -3,7 +3,6 @@ package fs
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
@@ -126,7 +125,7 @@ func CopyRecursive(src, dst string) error {
}
}
entries, err := ioutil.ReadDir(src)
entries, err := os.ReadDir(src)
if err != nil {
return err
}

View File

@@ -3,7 +3,6 @@ package installer
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@@ -29,16 +28,21 @@ func TestInstall(t *testing.T) {
require.NoError(t, err)
// verify extracted contents
files, err := ioutil.ReadDir(filepath.Join(testDir, pluginID))
files, err := os.ReadDir(filepath.Join(testDir, pluginID))
require.NoError(t, err)
file2, err := files[2].Info()
require.NoError(t, err)
file4, err := files[4].Info()
require.NoError(t, err)
require.Len(t, files, 6)
require.Equal(t, files[0].Name(), "MANIFEST.txt")
require.Equal(t, files[1].Name(), "dashboards")
require.Equal(t, files[2].Name(), "extra")
require.Equal(t, os.ModeSymlink, files[2].Mode()&os.ModeSymlink)
require.Equal(t, os.ModeSymlink, file2.Mode()&os.ModeSymlink)
require.Equal(t, files[3].Name(), "plugin.json")
require.Equal(t, files[4].Name(), "symlink_to_txt")
require.Equal(t, os.ModeSymlink, files[4].Mode()&os.ModeSymlink)
require.Equal(t, os.ModeSymlink, file4.Mode()&os.ModeSymlink)
require.Equal(t, files[5].Name(), "text.txt")
}

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)
}
}

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)
}

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() == "" {

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -3,7 +3,6 @@ package util
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
@@ -84,17 +83,22 @@ func walk(path string, info os.FileInfo, resolvedPath string, symlinkPathsFollow
}
return walk(path, info2, path2, symlinkPathsFollowed, walkFn)
} else if info.IsDir() {
list, err := ioutil.ReadDir(path)
list, err := os.ReadDir(path)
if err != nil {
return walkFn(resolvedPath, info, err)
}
var subFiles = make([]subFile, 0)
for _, fileInfo := range list {
path2 := filepath.Join(path, fileInfo.Name())
for _, file := range list {
path2 := filepath.Join(path, file.Name())
var resolvedPath2 string
if resolvedPath != "" {
resolvedPath2 = filepath.Join(resolvedPath, fileInfo.Name())
resolvedPath2 = filepath.Join(resolvedPath, file.Name())
}
fileInfo, err := file.Info()
if err != nil {
return fmt.Errorf("unable to read file info: %v, path: %v", file.Name(), path2)
}
subFiles = append(subFiles, subFile{path: path2, resolvedPath: resolvedPath2, fileInfo: fileInfo})
}