Skip to content

Commit

Permalink
chores(deps): Add support depguard rules in golangci-lint (#1965)
Browse files Browse the repository at this point in the history
Signed-off-by: dongjiang1989 <[email protected]>
  • Loading branch information
dongjiang1989 authored Sep 6, 2024
1 parent 36cb609 commit aacf585
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ linters-settings:
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
depguard:
rules:
main:
deny:
- pkg: "io/ioutil"
desc: "Use corresponding 'os' or 'io' functions instead."

linters:
fast: false
Expand All @@ -67,6 +73,7 @@ linters:
- vet
- unconvert
- staticcheck
- depguard

issues:
exclude-rules:
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -318,7 +317,7 @@ func logClusterImageSources() {

outputBytes, _ := json.MarshalIndent(images, "", " ")
filePath := filepath.Join(framework.TestContext.ReportDir, "images.json")
if err := ioutil.WriteFile(filePath, outputBytes, 0644); err != nil {
if err := os.WriteFile(filePath, outputBytes, 0644); err != nil {
framework.Logf("cluster images sources, could not write to %q: %v", filePath, err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ package framework
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"strings"
"sync"
Expand Down Expand Up @@ -332,7 +332,7 @@ func printSummaries(summaries []TestDataSummary, testBaseName string) {
} else {
// TODO: learn to extract test name and append it to the kind instead of timestamp.
filePath := path.Join(TestContext.ReportDir, summaries[i].SummaryKind()+"_"+testBaseName+"_"+now.Format(time.RFC3339)+".txt")
if err := ioutil.WriteFile(filePath, []byte(summaries[i].PrintHumanReadable()), 0644); err != nil {
if err := os.WriteFile(filePath, []byte(summaries[i].PrintHumanReadable()), 0644); err != nil {
Logf("Failed to write file %v with test performance data: %v", filePath, err)
}
}
Expand All @@ -349,7 +349,7 @@ func printSummaries(summaries []TestDataSummary, testBaseName string) {
// TODO: learn to extract test name and append it to the kind instead of timestamp.
filePath := path.Join(TestContext.ReportDir, summaries[i].SummaryKind()+"_"+testBaseName+"_"+now.Format(time.RFC3339)+".json")
Logf("Writing to %s", filePath)
if err := ioutil.WriteFile(filePath, []byte(summaries[i].PrintJSON()), 0644); err != nil {
if err := os.WriteFile(filePath, []byte(summaries[i].PrintJSON()), 0644); err != nil {
Logf("Failed to write file %v with test performance data: %v", filePath, err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package manifest

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -119,7 +119,7 @@ func DaemonSetFromURL(url string) (*appsv1.DaemonSet, error) {
}
defer response.Body.Close()

data, err := ioutil.ReadAll(response.Body)
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read html response body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/metrics/kubelet_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package metrics
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -75,7 +75,7 @@ func GrabKubeletMetricsWithoutProxy(nodeName, path string) (KubeletMetrics, erro
return KubeletMetrics{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return KubeletMetrics{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/pod/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"regexp"
Expand Down Expand Up @@ -116,7 +116,7 @@ func (d *Dialer) DialContainerPort(ctx context.Context, addr Addr) (conn net.Con
}
errorStream.Close()
go func() {
message, err := ioutil.ReadAll(errorStream)
message, err := io.ReadAll(errorStream)
switch {
case err != nil:
klog.ErrorS(err, "error reading from error stream")
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -103,7 +102,7 @@ func GetSigner(provider string) (ssh.Signer, error) {
}

func makePrivateKeySignerFromFile(key string) (ssh.Signer, error) {
buffer, err := ioutil.ReadFile(key)
buffer, err := os.ReadFile(key)
if err != nil {
return nil, fmt.Errorf("error reading SSH key %s: '%v'", key, err)
}
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"sort"
Expand Down Expand Up @@ -456,7 +455,7 @@ func AfterReadingAllFlags(t *TestContextType) {
if len(t.Host) == 0 && len(t.KubeConfig) == 0 {
// Check if we can use the in-cluster config
if clusterConfig, err := restclient.InClusterConfig(); err == nil {
if tempFile, err := ioutil.TempFile(os.TempDir(), "kubeconfig-"); err == nil {
if tempFile, err := os.CreateTemp(os.TempDir(), "kubeconfig-"); err == nil {
kubeConfig := createKubeConfig(clusterConfig)
clientcmd.WriteToFile(*kubeConfig, tempFile.Name())
t.KubeConfig = tempFile.Name()
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework/testfiles/testfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -122,7 +121,7 @@ func (r RootFileSource) ReadTestFile(filePath string) ([]byte, error) {
} else {
fullPath = filepath.Join(r.Root, filePath)
}
data, err := ioutil.ReadFile(fullPath)
data, err := os.ReadFile(fullPath)
if os.IsNotExist(err) {
// Not an error (yet), some other provider may have the file.
return nil, nil
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/generated/bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -9322,7 +9321,7 @@ func RestoreAsset(dir, name string) error {
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/reporters/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -123,7 +123,7 @@ func (reporter *ProgressReporter) postProgressToURL(b []byte) {
klog.Errorf("Unexpected response when posting progress update to %v: %v", reporter.progressURL, resp.StatusCode)
if resp.Body != nil {
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
klog.Errorf("Failed to read response body from posting progress: %v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package e2e

import (
"fmt"
"io/ioutil"
"os"
"path"
"time"

Expand Down Expand Up @@ -82,7 +82,7 @@ func gatherTestSuiteMetrics() error {
metricsJSON := metricsForE2E.PrintJSON()
if framework.TestContext.ReportDir != "" {
filePath := path.Join(framework.TestContext.ReportDir, "MetricsForE2ESuite_"+time.Now().Format(time.RFC3339)+".json")
if err := ioutil.WriteFile(filePath, []byte(metricsJSON), 0644); err != nil {
if err := os.WriteFile(filePath, []byte(metricsJSON), 0644); err != nil {
return fmt.Errorf("error writing to %q: %v", filePath, err)
}
} else {
Expand Down

0 comments on commit aacf585

Please sign in to comment.