Skip to content

Commit f0186ff

Browse files
committed
refactor: move from io/ioutil to io and os package
The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 40b426b commit f0186ff

File tree

25 files changed

+69
-72
lines changed

25 files changed

+69
-72
lines changed

cmd/dex/serve.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"crypto/x509"
77
"errors"
88
"fmt"
9-
"io/ioutil"
109
"net"
1110
"net/http"
1211
"os"
@@ -76,7 +75,7 @@ func commandServe() *cobra.Command {
7675

7776
func runServe(options serveOptions) error {
7877
configFile := options.config
79-
configData, err := ioutil.ReadFile(configFile)
78+
configData, err := os.ReadFile(configFile)
8079
if err != nil {
8180
return fmt.Errorf("failed to read config file %s: %v", configFile, err)
8281
}
@@ -148,7 +147,7 @@ func runServe(options serveOptions) error {
148147
if c.GRPC.TLSClientCA != "" {
149148
// Parse certificates from client CA file to a new CertPool.
150149
cPool := x509.NewCertPool()
151-
clientCert, err := ioutil.ReadFile(c.GRPC.TLSClientCA)
150+
clientCert, err := os.ReadFile(c.GRPC.TLSClientCA)
152151
if err != nil {
153152
return fmt.Errorf("invalid config: reading from client CA file: %v", err)
154153
}

connector/atlassiancrowd/atlassiancrowd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"net"
1211
"net/http"
1312
"strings"
@@ -432,7 +431,7 @@ func (c *crowdConnector) crowdUserManagementRequest(ctx context.Context, method
432431

433432
// validateCrowdResponse validates unique not JSON responses from API
434433
func (c *crowdConnector) validateCrowdResponse(resp *http.Response) ([]byte, error) {
435-
body, err := ioutil.ReadAll(resp.Body)
434+
body, err := io.ReadAll(resp.Body)
436435
if err != nil {
437436
return nil, fmt.Errorf("crowd: read user body: %v", err)
438437
}

connector/atlassiancrowd/atlassiancrowd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"crypto/tls"
77
"encoding/json"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"net/http/httptest"
1212
"reflect"
@@ -152,7 +152,7 @@ func newTestCrowdConnector(baseURL string) crowdConnector {
152152
connector := crowdConnector{}
153153
connector.BaseURL = baseURL
154154
connector.logger = &logrus.Logger{
155-
Out: ioutil.Discard,
155+
Out: io.Discard,
156156
Level: logrus.DebugLevel,
157157
Formatter: &logrus.TextFormatter{DisableColors: true},
158158
}

connector/bitbucketcloud/bitbucketcloud.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"sync"
1212
"time"
@@ -453,7 +453,7 @@ func get(ctx context.Context, client *http.Client, apiURL string, v interface{})
453453
defer resp.Body.Close()
454454

455455
if resp.StatusCode != http.StatusOK {
456-
body, err := ioutil.ReadAll(resp.Body)
456+
body, err := io.ReadAll(resp.Body)
457457
if err != nil {
458458
return fmt.Errorf("bitbucket: read body: %s: %v", resp.Status, err)
459459
}

connector/gitea/gitea.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"strconv"
1212
"sync"
@@ -252,7 +252,7 @@ func (c *giteaConnector) user(ctx context.Context, client *http.Client) (giteaUs
252252
defer resp.Body.Close()
253253

254254
if resp.StatusCode != http.StatusOK {
255-
body, err := ioutil.ReadAll(resp.Body)
255+
body, err := io.ReadAll(resp.Body)
256256
if err != nil {
257257
return u, fmt.Errorf("gitea: read body: %v", err)
258258
}

connector/github/github.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"net"
1313
"net/http"
14+
"os"
1415
"regexp"
1516
"strconv"
1617
"strings"
@@ -210,7 +211,7 @@ func (e *oauth2Error) Error() string {
210211
// newHTTPClient returns a new HTTP client that trusts the custom declared rootCA cert.
211212
func newHTTPClient(rootCA string) (*http.Client, error) {
212213
tlsConfig := tls.Config{RootCAs: x509.NewCertPool()}
213-
rootCABytes, err := ioutil.ReadFile(rootCA)
214+
rootCABytes, err := os.ReadFile(rootCA)
214215
if err != nil {
215216
return nil, fmt.Errorf("failed to read root-ca: %v", err)
216217
}
@@ -488,7 +489,7 @@ func get(ctx context.Context, client *http.Client, apiURL string, v interface{})
488489
defer resp.Body.Close()
489490

490491
if resp.StatusCode != http.StatusOK {
491-
body, err := ioutil.ReadAll(resp.Body)
492+
body, err := io.ReadAll(resp.Body)
492493
if err != nil {
493494
return "", fmt.Errorf("github: read body: %v", err)
494495
}

connector/gitlab/gitlab.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"strconv"
1212

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

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

268268
if resp.StatusCode != http.StatusOK {
269-
body, err := ioutil.ReadAll(resp.Body)
269+
body, err := io.ReadAll(resp.Body)
270270
if err != nil {
271271
return nil, fmt.Errorf("gitlab: read body: %v", err)
272272
}

connector/google/google.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"io/ioutil"
98
"net/http"
9+
"os"
1010
"time"
1111

1212
"github.com/coreos/go-oidc/v3/oidc"
@@ -274,7 +274,7 @@ func createDirectoryService(serviceAccountFilePath string, email string) (*admin
274274
if serviceAccountFilePath == "" || email == "" {
275275
return nil, fmt.Errorf("directory service requires both serviceAccountFilePath and adminEmail")
276276
}
277-
jsonCredentials, err := ioutil.ReadFile(serviceAccountFilePath)
277+
jsonCredentials, err := os.ReadFile(serviceAccountFilePath)
278278
if err != nil {
279279
return nil, fmt.Errorf("error reading credentials from file: %v", err)
280280
}

connector/keystone/keystone.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"context"
77
"encoding/json"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111

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

263-
data, err := ioutil.ReadAll(resp.Body)
263+
data, err := io.ReadAll(resp.Body)
264264
if err != nil {
265265
return nil, err
266266
}
@@ -290,7 +290,7 @@ func (p *conn) getUserGroups(ctx context.Context, userID string, token string) (
290290
return nil, err
291291
}
292292

293-
data, err := ioutil.ReadAll(resp.Body)
293+
data, err := io.ReadAll(resp.Body)
294294
if err != nil {
295295
return nil, err
296296
}

connector/keystone/keystone_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"os"
1111
"reflect"
@@ -78,7 +78,7 @@ func getAdminToken(t *testing.T, adminName, adminPass string) (token, id string)
7878

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

81-
data, err := ioutil.ReadAll(resp.Body)
81+
data, err := io.ReadAll(resp.Body)
8282
if err != nil {
8383
t.Fatal(err)
8484
}
@@ -122,7 +122,7 @@ func createUser(t *testing.T, token, userName, userEmail, userPass string) strin
122122
t.Fatal(err)
123123
}
124124

125-
data, err := ioutil.ReadAll(resp.Body)
125+
data, err := io.ReadAll(resp.Body)
126126
if err != nil {
127127
t.Fatal(err)
128128
}
@@ -183,7 +183,7 @@ func createGroup(t *testing.T, token, description, name string) string {
183183
t.Fatal(err)
184184
}
185185

186-
data, err := ioutil.ReadAll(resp.Body)
186+
data, err := io.ReadAll(resp.Body)
187187
if err != nil {
188188
t.Fatal(err)
189189
}

connector/ldap/ldap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"crypto/x509"
88
"encoding/json"
99
"fmt"
10-
"io/ioutil"
1110
"net"
11+
"os"
1212

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

@@ -257,7 +257,7 @@ func (c *Config) openConnector(logger log.Logger) (*ldapConnector, error) {
257257
data := c.RootCAData
258258
if len(data) == 0 {
259259
var err error
260-
if data, err = ioutil.ReadFile(c.RootCA); err != nil {
260+
if data, err = os.ReadFile(c.RootCA); err != nil {
261261
return nil, fmt.Errorf("ldap: read ca file: %v", err)
262262
}
263263
}

connector/ldap/ldap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ldap
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"os"
88
"testing"
99

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

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

560560
conn, err := c.openConnector(l)
561561
if err != nil {

connector/linkedin/linkedin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"strings"
1111

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

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

211211
if resp.StatusCode != http.StatusOK {
212-
body, err := ioutil.ReadAll(resp.Body)
212+
body, err := io.ReadAll(resp.Body)
213213
if err != nil {
214214
return p, fmt.Errorf("read body: %v", err)
215215
}

connector/openshift/openshift.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"crypto/x509"
77
"encoding/json"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net"
1111
"net/http"
12+
"os"
1213
"strings"
1314
"time"
1415

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

197198
if resp.StatusCode != http.StatusOK {
198-
body, err := ioutil.ReadAll(resp.Body)
199+
body, err := io.ReadAll(resp.Body)
199200
if err != nil {
200201
return u, fmt.Errorf("read body: %v", err)
201202
}
@@ -223,7 +224,7 @@ func newHTTPClient(insecureCA bool, rootCA string) (*http.Client, error) {
223224
tlsConfig = tls.Config{InsecureSkipVerify: true}
224225
} else if rootCA != "" {
225226
tlsConfig = tls.Config{RootCAs: x509.NewCertPool()}
226-
rootCABytes, err := ioutil.ReadFile(rootCA)
227+
rootCABytes, err := os.ReadFile(rootCA)
227228
if err != nil {
228229
return nil, fmt.Errorf("failed to read root-ca: %v", err)
229230
}

connector/saml/saml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"encoding/pem"
99
"encoding/xml"
1010
"fmt"
11-
"io/ioutil"
11+
"os"
1212
"strings"
1313
"sync"
1414
"time"
@@ -194,7 +194,7 @@ func (c *Config) openConnector(logger log.Logger) (*provider, error) {
194194

195195
var caData []byte
196196
if c.CA != "" {
197-
data, err := ioutil.ReadFile(c.CA)
197+
data, err := os.ReadFile(c.CA)
198198
if err != nil {
199199
return nil, fmt.Errorf("read ca file: %v", err)
200200
}

0 commit comments

Comments
 (0)