Skip to content

Commit

Permalink
Replace deprecated package io/ioutil with io and os (#1423)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com>
  • Loading branch information
alexandear and andrewsomething authored Oct 3, 2023
1 parent 59e4c38 commit 1ba1648
Show file tree
Hide file tree
Showing 81 changed files with 214 additions and 299 deletions.
3 changes: 1 addition & 2 deletions commands/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -953,7 +952,7 @@ func readAppAlertDestination(stdin io.Reader, path string) (*godo.AlertDestinati
alertDestinations = alertDestinationsFile
}

byt, err := ioutil.ReadAll(alertDestinations)
byt, err := io.ReadAll(alertDestinations)
if err != nil {
return nil, fmt.Errorf("reading app alert destinations: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions commands/apps_dev_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -202,7 +201,7 @@ func tempAppSpec(t *testing.T, spec *godo.AppSpec) (path string) {
path = tempFile(t, "app.yaml")
specYaml, err := yaml.Marshal(spec)
require.NoError(t, err, "marshaling app spec")
err = ioutil.WriteFile(path, specYaml, 0664)
err = os.WriteFile(path, specYaml, 0664)
require.NoError(t, err, "writing app spec to disk")
return
}
24 changes: 7 additions & 17 deletions commands/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/url"
"os"
"testing"
Expand Down Expand Up @@ -130,12 +129,9 @@ var (

func TestRunAppsCreate(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
specFile, err := ioutil.TempFile("", "spec")
specFile, err := os.CreateTemp(t.TempDir(), "spec")
require.NoError(t, err)
defer func() {
os.Remove(specFile.Name())
specFile.Close()
}()
defer specFile.Close()

err = json.NewEncoder(specFile).Encode(&testAppSpec)
require.NoError(t, err)
Expand Down Expand Up @@ -195,12 +191,9 @@ func TestRunAppsList(t *testing.T) {

func TestRunAppsUpdate(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
specFile, err := ioutil.TempFile("", "spec")
specFile, err := os.CreateTemp(t.TempDir(), "spec")
require.NoError(t, err)
defer func() {
os.Remove(specFile.Name())
specFile.Close()
}()
defer specFile.Close()

err = json.NewEncoder(specFile).Encode(&testAppSpec)
require.NoError(t, err)
Expand Down Expand Up @@ -543,7 +536,7 @@ var validAppSpec = &godo.AppSpec{
func testTempFile(t *testing.T, data []byte) string {
t.Helper()
file := t.TempDir() + "/file"
err := ioutil.WriteFile(file, data, 0644)
err := os.WriteFile(file, data, 0644)
require.NoError(t, err, "writing temp file")
return file
}
Expand Down Expand Up @@ -769,12 +762,9 @@ func TestRunAppsListAlerts(t *testing.T) {

func TestRunAppsUpdateAlertDestinations(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
destinationsFile, err := ioutil.TempFile("", "dest")
destinationsFile, err := os.CreateTemp(t.TempDir(), "dest")
require.NoError(t, err)
defer func() {
os.Remove(destinationsFile.Name())
destinationsFile.Close()
}()
defer destinationsFile.Close()

err = json.NewEncoder(destinationsFile).Encode(&testAlertUpdate)
require.NoError(t, err)
Expand Down
7 changes: 3 additions & 4 deletions commands/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bufio"
"bytes"
"io"
"io/ioutil"
"path/filepath"
"testing"

Expand Down Expand Up @@ -48,7 +47,7 @@ func TestAuthInit(t *testing.T) {
return "valid-token", nil
}

cfgFileWriter = func() (io.WriteCloser, error) { return &nopWriteCloser{Writer: ioutil.Discard}, nil }
cfgFileWriter = func() (io.WriteCloser, error) { return &nopWriteCloser{Writer: io.Discard}, nil }

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.oauth.EXPECT().TokenInfo(gomock.Any()).Return(&do.OAuthTokenInfo{}, nil)
Expand Down Expand Up @@ -114,7 +113,7 @@ func TestAuthInitWithProvidedToken(t *testing.T) {
return "", errors.New("should not have called this")
}

cfgFileWriter = func() (io.WriteCloser, error) { return &nopWriteCloser{Writer: ioutil.Discard}, nil }
cfgFileWriter = func() (io.WriteCloser, error) { return &nopWriteCloser{Writer: io.Discard}, nil }

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.oauth.EXPECT().TokenInfo(gomock.Any()).Return(&do.OAuthTokenInfo{}, nil)
Expand All @@ -136,7 +135,7 @@ func TestAuthForcesLowercase(t *testing.T) {
return "", errors.New("should not have called this")
}

cfgFileWriter = func() (io.WriteCloser, error) { return &nopWriteCloser{Writer: ioutil.Discard}, nil }
cfgFileWriter = func() (io.WriteCloser, error) { return &nopWriteCloser{Writer: io.Discard}, nil }

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.oauth.EXPECT().TokenInfo(gomock.Any()).Return(&do.OAuthTokenInfo{}, nil)
Expand Down
4 changes: 2 additions & 2 deletions commands/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ limitations under the License.
package commands

import (
"io/ioutil"
"os"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
Expand Down Expand Up @@ -217,7 +217,7 @@ func RunCertificateDelete(c *CmdConfig) error {
}

func readInputFromFile(path string) (string, error) {
fileBytes, err := ioutil.ReadFile(path)
fileBytes, err := os.ReadFile(path)
if err != nil {
return "", err
}
Expand Down
7 changes: 3 additions & 4 deletions commands/certificates_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -106,21 +105,21 @@ func TestCertificatesCreate(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
if test.privateKeyPath != "" {
pkErr := ioutil.WriteFile(test.privateKeyPath, []byte("-----BEGIN PRIVATE KEY-----"), 0600)
pkErr := os.WriteFile(test.privateKeyPath, []byte("-----BEGIN PRIVATE KEY-----"), 0600)
assert.NoError(t, pkErr)

defer os.Remove(test.privateKeyPath)
}

if test.certLeafPath != "" {
certErr := ioutil.WriteFile(test.certLeafPath, []byte("-----BEGIN CERTIFICATE-----"), 0600)
certErr := os.WriteFile(test.certLeafPath, []byte("-----BEGIN CERTIFICATE-----"), 0600)
assert.NoError(t, certErr)

defer os.Remove(test.certLeafPath)
}

if test.certChainPath != "" {
certErr := ioutil.WriteFile(test.certChainPath, []byte("-----BEGIN CERTIFICATE-----"), 0600)
certErr := os.WriteFile(test.certChainPath, []byte("-----BEGIN CERTIFICATE-----"), 0600)
assert.NoError(t, certErr)

defer os.Remove(test.certChainPath)
Expand Down
4 changes: 2 additions & 2 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ limitations under the License.
package commands

import (
"io/ioutil"
"io"
"sort"
"testing"

Expand Down Expand Up @@ -241,7 +241,7 @@ func withTestClient(t *testing.T, tFn testFn) {
config := &CmdConfig{
NS: "test",
Doit: testConfig,
Out: ioutil.Discard,
Out: io.Discard,

// can stub this out, since the return is dictated by the mocks.
initServices: func(c *CmdConfig) error { return nil },
Expand Down
4 changes: 2 additions & 2 deletions commands/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -413,7 +413,7 @@ func extractSSHKeys(keys []string) []godo.DropletCreateSSHKey {

func extractUserData(userData, filename string) (string, error) {
if userData == "" && filename != "" {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
Expand Down
4 changes: 1 addition & 3 deletions commands/droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package commands

import (
"bytes"
"io/ioutil"
"os"
"strconv"
"testing"
Expand Down Expand Up @@ -163,9 +162,8 @@ coreos:
command: start
`

tmpFile, err := ioutil.TempFile(os.TempDir(), "doctlDropletsTest-*.yml")
tmpFile, err := os.CreateTemp(t.TempDir(), "doctlDropletsTest-*.yml")
assert.NoError(t, err)
defer os.Remove(tmpFile.Name())

_, err = tmpFile.WriteString(userData)
assert.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions commands/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package commands

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

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
Expand Down Expand Up @@ -145,7 +145,7 @@ func RunInvoicesGetPDF(c *CmdConfig) error {

outputFile := getOutputFileArg("pdf", c.Args)

err = ioutil.WriteFile(outputFile, pdf, 0644)
err = os.WriteFile(outputFile, pdf, 0644)
if err != nil {
return err
}
Expand All @@ -167,7 +167,7 @@ func RunInvoicesGetCSV(c *CmdConfig) error {

outputFile := getOutputFileArg("csv", c.Args)

err = ioutil.WriteFile(outputFile, csv, 0644)
err = os.WriteFile(outputFile, csv, 0644)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions commands/invoices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ limitations under the License.
package commands

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -185,7 +184,7 @@ func TestInvoicesGetPDF(t *testing.T) {
assert.NoError(t, err)

// Assert the file exists
result, err := ioutil.ReadFile(fpath)
result, err := os.ReadFile(fpath)
assert.NoError(t, err)
assert.Equal(t, content, result)

Expand All @@ -208,7 +207,7 @@ func TestInvoicesGetCSV(t *testing.T) {
assert.NoError(t, err)

// Assert the file exists
result, err := ioutil.ReadFile(fpath)
result, err := os.ReadFile(fpath)
assert.NoError(t, err)
assert.Equal(t, content, result)

Expand Down
19 changes: 4 additions & 15 deletions commands/serverless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"context"
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -688,17 +687,12 @@ func TestGetCredentialDirectory(t *testing.T) {

func TestPreserveCredsMovesExistingToStaging(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tmp, err := ioutil.TempDir("", "test-dir")
require.NoError(t, err)
defer func() {
err := os.RemoveAll(tmp)
require.NoError(t, err, "error cleaning tmp dir")
}()
tmp := t.TempDir()

// Set up "existing" creds in the "sandbox" dir
serverlessDir := filepath.Join(tmp, "sandbox")
serverlessCredsDir := filepath.Join(serverlessDir, "creds", "d5b388f2")
err = os.MkdirAll(serverlessCredsDir, os.FileMode(0755))
err := os.MkdirAll(serverlessCredsDir, os.FileMode(0755))
require.NoError(t, err)
serverlessCreds := filepath.Join(serverlessCredsDir, "credentials.json")
creds, err := os.Create(serverlessCreds)
Expand Down Expand Up @@ -726,17 +720,12 @@ func TestPreserveCredsMovesLegacyCreds(t *testing.T) {
return "a7bbe7e8af7411ec912e47a270a2ee78a7bbe7e8af7411ec912e47a270a2ee78"
}

tmp, err := ioutil.TempDir("", "test-dir")
require.NoError(t, err)
defer func() {
err := os.RemoveAll(tmp)
require.NoError(t, err, "error cleaning tmp dir")
}()
tmp := t.TempDir()

// Set up "existing" legacy creds in the "sandbox" dir
serverlessDir := filepath.Join(tmp, "sandbox")
legacyCredsDir := filepath.Join(serverlessDir, ".nimbella")
err = os.MkdirAll(legacyCredsDir, os.FileMode(0755))
err := os.MkdirAll(legacyCredsDir, os.FileMode(0755))
require.NoError(t, err)
legacyCreds := filepath.Join(legacyCredsDir, "credentials.json")
creds, err := os.Create(legacyCreds)
Expand Down
4 changes: 2 additions & 2 deletions commands/sshkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ limitations under the License.
package commands

import (
"io/ioutil"
"os"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
Expand Down Expand Up @@ -149,7 +149,7 @@ func RunKeyImport(c *CmdConfig) error {

keyName := c.Args[0]

keyFile, err := ioutil.ReadFile(keyPath)
keyFile, err := os.ReadFile(keyPath)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions commands/sshkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ limitations under the License.
package commands

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -141,7 +140,7 @@ func TestKeysUpdateByFingerprint(t *testing.T) {
func TestSSHPublicKeyImportWithName(t *testing.T) {
pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCn6eZ8ve0ha04rPRZuoPXK1AQ/h21qslWCzoDcOciXn5OcyafkZw+31k/afaBTeW62D8fXd8e/1xWbFfp/2GqmslYpNCTPrtpNhsE8I0yKjJ8FxX9FfsCOu/Sv83dWgSpiT7pNWVKarZjW9KdKKRQljq1i+H5pX3r5Q9I1v+66mYTe7qsKGas9KWy0vkGoNSqmTCl+d+Y0286chtqBqBjSCUCI8oLKPnJB86Lj344tFGmzDIsJKXMVHTL0dF8n3u6iWN4qiRU+JvkoIkI3v0JvyZXxhR2uPIS1yUAY2GC+2O5mfxydJQzBdtag5Uw8Y7H5yYR1gar/h16bAy5XzRvp testkey"
path := filepath.Join(os.TempDir(), "key.pub")
err := ioutil.WriteFile(path, []byte(pubkey), 0600)
err := os.WriteFile(path, []byte(pubkey), 0600)
assert.NoError(t, err)
defer os.Remove(path)

Expand Down
Loading

0 comments on commit 1ba1648

Please sign in to comment.