Skip to content

Commit

Permalink
Merge pull request #2278 from Juneezee/deprecate-ioutil
Browse files Browse the repository at this point in the history
refactor: move from io/ioutil to io and os package
  • Loading branch information
nabokihms authored Sep 17, 2021
2 parents 40b426b + 5510220 commit 5169c43
Show file tree
Hide file tree
Showing 26 changed files with 77 additions and 73 deletions.
9 changes: 8 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ run:
timeout: 2m

linters-settings:
depguard:
list-type: blacklist
include-go-root: true
packages:
- io/ioutil
packages-with-error-message:
- io/ioutil: "The 'io/ioutil' package is deprecated. Use corresponding 'os' or 'io' functions instead."
gci:
local-prefixes: github.com/dexidp/dex
goimports:
Expand All @@ -13,6 +20,7 @@ linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- exhaustive
- exportloopref
Expand Down Expand Up @@ -64,7 +72,6 @@ linters:
# - scopelint

# unused
# - depguard
# - goheader
# - gomodguard

Expand Down
5 changes: 2 additions & 3 deletions cmd/dex/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -76,7 +75,7 @@ func commandServe() *cobra.Command {

func runServe(options serveOptions) error {
configFile := options.config
configData, err := ioutil.ReadFile(configFile)
configData, err := os.ReadFile(configFile)
if err != nil {
return fmt.Errorf("failed to read config file %s: %v", configFile, err)
}
Expand Down Expand Up @@ -148,7 +147,7 @@ func runServe(options serveOptions) error {
if c.GRPC.TLSClientCA != "" {
// Parse certificates from client CA file to a new CertPool.
cPool := x509.NewCertPool()
clientCert, err := ioutil.ReadFile(c.GRPC.TLSClientCA)
clientCert, err := os.ReadFile(c.GRPC.TLSClientCA)
if err != nil {
return fmt.Errorf("invalid config: reading from client CA file: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions connector/atlassiancrowd/atlassiancrowd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -432,7 +431,7 @@ func (c *crowdConnector) crowdUserManagementRequest(ctx context.Context, method

// validateCrowdResponse validates unique not JSON responses from API
func (c *crowdConnector) validateCrowdResponse(resp *http.Response) ([]byte, error) {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("crowd: read user body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions connector/atlassiancrowd/atlassiancrowd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -152,7 +152,7 @@ func newTestCrowdConnector(baseURL string) crowdConnector {
connector := crowdConnector{}
connector.BaseURL = baseURL
connector.logger = &logrus.Logger{
Out: ioutil.Discard,
Out: io.Discard,
Level: logrus.DebugLevel,
Formatter: &logrus.TextFormatter{DisableColors: true},
}
Expand Down
4 changes: 2 additions & 2 deletions connector/bitbucketcloud/bitbucketcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -453,7 +453,7 @@ func get(ctx context.Context, client *http.Client, apiURL string, v interface{})
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("bitbucket: read body: %s: %v", resp.Status, err)
}
Expand Down
4 changes: 2 additions & 2 deletions connector/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"sync"
Expand Down Expand Up @@ -252,7 +252,7 @@ func (c *giteaConnector) user(ctx context.Context, client *http.Client) (giteaUs
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return u, fmt.Errorf("gitea: read body: %v", err)
}
Expand Down
7 changes: 4 additions & 3 deletions connector/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -210,7 +211,7 @@ func (e *oauth2Error) Error() string {
// newHTTPClient returns a new HTTP client that trusts the custom declared rootCA cert.
func newHTTPClient(rootCA string) (*http.Client, error) {
tlsConfig := tls.Config{RootCAs: x509.NewCertPool()}
rootCABytes, err := ioutil.ReadFile(rootCA)
rootCABytes, err := os.ReadFile(rootCA)
if err != nil {
return nil, fmt.Errorf("failed to read root-ca: %v", err)
}
Expand Down Expand Up @@ -488,7 +489,7 @@ func get(ctx context.Context, client *http.Client, apiURL string, v interface{})
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("github: read body: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions connector/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"

Expand Down Expand Up @@ -232,7 +232,7 @@ func (c *gitlabConnector) user(ctx context.Context, client *http.Client) (gitlab
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return u, fmt.Errorf("gitlab: read body: %v", err)
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func (c *gitlabConnector) userGroups(ctx context.Context, client *http.Client) (
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("gitlab: read body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions connector/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"

"github.com/coreos/go-oidc/v3/oidc"
Expand Down Expand Up @@ -274,7 +274,7 @@ func createDirectoryService(serviceAccountFilePath string, email string) (*admin
if serviceAccountFilePath == "" || email == "" {
return nil, fmt.Errorf("directory service requires both serviceAccountFilePath and adminEmail")
}
jsonCredentials, err := ioutil.ReadFile(serviceAccountFilePath)
jsonCredentials, err := os.ReadFile(serviceAccountFilePath)
if err != nil {
return nil, fmt.Errorf("error reading credentials from file: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions connector/keystone/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/dexidp/dex/connector"
Expand Down Expand Up @@ -133,7 +133,7 @@ func (p *conn) Login(ctx context.Context, scopes connector.Scopes, username, pas
return identity, false, nil
}
token := resp.Header.Get("X-Subject-Token")
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return identity, false, err
}
Expand Down Expand Up @@ -260,7 +260,7 @@ func (p *conn) getUser(ctx context.Context, userID string, token string) (*userR
return nil, err
}

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (p *conn) getUserGroups(ctx context.Context, userID string, token string) (
return nil, err
}

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions connector/keystone/keystone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -78,7 +78,7 @@ func getAdminToken(t *testing.T, adminName, adminPass string) (token, id string)

token = resp.Header.Get("X-Subject-Token")

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func createUser(t *testing.T, token, userName, userEmail, userPass string) strin
t.Fatal(err)
}

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func createGroup(t *testing.T, token, description, name string) string {
t.Fatal(err)
}

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions connector/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"

"github.com/go-ldap/ldap/v3"

Expand Down Expand Up @@ -257,7 +257,7 @@ func (c *Config) openConnector(logger log.Logger) (*ldapConnector, error) {
data := c.RootCAData
if len(data) == 0 {
var err error
if data, err = ioutil.ReadFile(c.RootCA); err != nil {
if data, err = os.ReadFile(c.RootCA); err != nil {
return nil, fmt.Errorf("ldap: read ca file: %v", err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions connector/ldap/ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ldap
import (
"context"
"fmt"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -555,7 +555,7 @@ func runTests(t *testing.T, connMethod connectionMethod, config *Config, tests [
c.BindDN = "cn=admin,dc=example,dc=org"
c.BindPW = "admin"

l := &logrus.Logger{Out: ioutil.Discard, Formatter: &logrus.TextFormatter{}}
l := &logrus.Logger{Out: io.Discard, Formatter: &logrus.TextFormatter{}}

conn, err := c.openConnector(l)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions connector/linkedin/linkedin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -169,7 +169,7 @@ func (c *linkedInConnector) primaryEmail(ctx context.Context, client *http.Clien
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return email, fmt.Errorf("read body: %v", err)
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *linkedInConnector) profile(ctx context.Context, client *http.Client) (p
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return p, fmt.Errorf("read body: %v", err)
}
Expand Down
7 changes: 4 additions & 3 deletions connector/openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"os"
"strings"
"time"

Expand Down Expand Up @@ -195,7 +196,7 @@ func (c *openshiftConnector) user(ctx context.Context, client *http.Client) (u u
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return u, fmt.Errorf("read body: %v", err)
}
Expand Down Expand Up @@ -223,7 +224,7 @@ func newHTTPClient(insecureCA bool, rootCA string) (*http.Client, error) {
tlsConfig = tls.Config{InsecureSkipVerify: true}
} else if rootCA != "" {
tlsConfig = tls.Config{RootCAs: x509.NewCertPool()}
rootCABytes, err := ioutil.ReadFile(rootCA)
rootCABytes, err := os.ReadFile(rootCA)
if err != nil {
return nil, fmt.Errorf("failed to read root-ca: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions connector/saml/saml.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"encoding/pem"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -194,7 +194,7 @@ func (c *Config) openConnector(logger log.Logger) (*provider, error) {

var caData []byte
if c.CA != "" {
data, err := ioutil.ReadFile(c.CA)
data, err := os.ReadFile(c.CA)
if err != nil {
return nil, fmt.Errorf("read ca file: %v", err)
}
Expand Down
Loading

0 comments on commit 5169c43

Please sign in to comment.