Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: remove usage of ioutil #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ linters-settings:
- codegenComment
# - commentedOutCode
- commentedOutImport
# - commentFormatting
- commentFormatting
- defaultCaseOrder
- deferUnlambda
# - deprecatedComment
Expand All @@ -54,7 +54,7 @@ linters-settings:
# - importShadow
- indexAlloc
- initClause
# - ioutilDeprecated
- ioutilDeprecated
- mapKey
- methodExprCall
# - nestingReduce
Expand Down
4 changes: 2 additions & 2 deletions cli/config/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"io/ioutil"
"io"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -67,5 +67,5 @@ func read(path string) ([]byte, error) {
return nil, err
}
defer fi.Close()
return ioutil.ReadAll(fi)
return io.ReadAll(fi)
}
9 changes: 5 additions & 4 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cli
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/url"
"os"
"os/exec"
"os/user"
"runtime"
Expand All @@ -30,8 +31,8 @@ func init() {
// when in SSH or another type of headless session
// NOTE: This needs to be in `init` to prevent data races
// (multiple threads trying to set the global browser.Std* variables)
browser.Stderr = ioutil.Discard
browser.Stdout = ioutil.Discard
browser.Stderr = io.Discard
browser.Stdout = io.Discard
}

func login() *cobra.Command {
Expand Down Expand Up @@ -205,7 +206,7 @@ func isWSL() (bool, error) {
if runtime.GOOS == goosDarwin || runtime.GOOS == goosWindows {
return false, nil
}
data, err := ioutil.ReadFile("/proc/version")
data, err := os.ReadFile("/proc/version")
if err != nil {
return false, xerrors.Errorf("read /proc/version: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"database/sql"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -383,10 +382,12 @@ func newProvisionerDaemon(ctx context.Context, client *codersdk.Client, logger s
panic(err)
}
}()
tempDir, err := ioutil.TempDir("", "provisionerd")

tempDir, err := os.MkdirTemp("", "provisionerd")
if err != nil {
return nil, err
}

return provisionerd.New(client.ListenProvisionerDaemon, &provisionerd.Options{
Logger: logger,
PollInterval: 50 * time.Millisecond,
Expand Down Expand Up @@ -473,7 +474,7 @@ func configureTLS(listener net.Listener, tlsMinVersion, tlsClientAuth, tlsCertFi

if tlsClientCAFile != "" {
caPool := x509.NewCertPool()
data, err := ioutil.ReadFile(tlsClientCAFile)
data, err := os.ReadFile(tlsClientCAFile)
if err != nil {
return nil, xerrors.Errorf("read %q: %w", tlsClientCAFile, err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/templater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -263,10 +262,12 @@ func newProvisionerDaemon(ctx context.Context, client *codersdk.Client, logger s
panic(err)
}
}()
tempDir, err := ioutil.TempDir("", "provisionerd")

tempDir, err := os.MkdirTemp("", "provisionerd")
if err != nil {
return nil, err
return nil, xerrors.Errorf("mkdir temp: %w", err)
}

return provisionerd.New(client.ListenProvisionerDaemon, &provisionerd.Options{
Logger: logger,
PollInterval: 50 * time.Millisecond,
Expand Down
11 changes: 5 additions & 6 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"encoding/json"
"encoding/pem"
"io"
"io/ioutil"
"math/big"
"net"
"net/http"
Expand Down Expand Up @@ -323,7 +322,7 @@ func NewGoogleInstanceIdentity(t *testing.T, instanceID string, expired bool) (*
require.NoError(t, err)
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(data)),
Body: io.NopCloser(bytes.NewReader(data)),
Header: make(http.Header),
}, nil
}),
Expand All @@ -334,7 +333,7 @@ func NewGoogleInstanceIdentity(t *testing.T, instanceID string, expired bool) (*
Transport: roundTripper(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(signedKey))),
Body: io.NopCloser(bytes.NewReader([]byte(signedKey))),
Header: make(http.Header),
}, nil
}),
Expand Down Expand Up @@ -379,19 +378,19 @@ func NewAWSInstanceIdentity(t *testing.T, instanceID string) (awsidentity.Certif
case "/latest/api/token":
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("faketoken"))),
Body: io.NopCloser(bytes.NewReader([]byte("faketoken"))),
Header: make(http.Header),
}, nil
case "/latest/dynamic/instance-identity/signature":
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(signature)),
Body: io.NopCloser(bytes.NewReader(signature)),
Header: make(http.Header),
}, nil
case "/latest/dynamic/instance-identity/document":
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(document)),
Body: io.NopCloser(bytes.NewReader(document)),
Header: make(http.Header),
}, nil
default:
Expand Down
3 changes: 1 addition & 2 deletions coderd/database/dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -81,7 +80,7 @@ func main() {
if !ok {
panic("couldn't get caller path")
}
err = ioutil.WriteFile(filepath.Join(mainPath, "..", "..", "dump.sql"), []byte(dump), 0600)
err = os.WriteFile(filepath.Join(mainPath, "..", "..", "dump.sql"), []byte(dump), 0600)
if err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions coderd/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package postgres
import (
"database/sql"
"fmt"
"io/ioutil"
"net"
"os"
"sync"
Expand Down Expand Up @@ -48,10 +47,12 @@ func Open() (string, func(), error) {
if err != nil {
return "", nil, xerrors.Errorf("create pool: %w", err)
}
tempDir, err := ioutil.TempDir(os.TempDir(), "postgres")

tempDir, err := os.MkdirTemp(os.TempDir(), "postgres")
if err != nil {
return "", nil, xerrors.Errorf("create tempdir: %w", err)
}

openPortMutex.Lock()
// Pick an explicit port on the host to connect to 5432.
// This is necessary so we can configure the port to only use ipv4.
Expand Down
5 changes: 2 additions & 3 deletions provisionersdk/archive_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provisionersdk_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -14,7 +13,7 @@ import (
func TestTar(t *testing.T) {
t.Parallel()
dir := t.TempDir()
file, err := ioutil.TempFile(dir, "")
file, err := os.CreateTemp(dir, "")
require.NoError(t, err)
_ = file.Close()
_, err = provisionersdk.Tar(dir)
Expand All @@ -24,7 +23,7 @@ func TestTar(t *testing.T) {
func TestUntar(t *testing.T) {
t.Parallel()
dir := t.TempDir()
file, err := ioutil.TempFile(dir, "")
file, err := os.CreateTemp(dir, "")
require.NoError(t, err)
_ = file.Close()
archive, err := provisionersdk.Tar(dir)
Expand Down