Skip to content

Commit

Permalink
refactor: Replace ioutil.ReadFile and ioutil.ReadAll with os.ReadFile…
Browse files Browse the repository at this point in the history
… and io.ReadAll.
  • Loading branch information
enc committed Apr 26, 2024
1 parent 4f4ee8d commit d7ce42b
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 24 deletions.
6 changes: 3 additions & 3 deletions internal/provider/data_source_docker_registry_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -171,7 +171,7 @@ func getDigestFromResponse(response *http.Response) (string, error) {
header := response.Header.Get("Docker-Content-Digest")

if header == "" {
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil || len(body) == 0 {
return "", fmt.Errorf("Error reading registry response body: %s", err)
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func getAuthToken(authHeader string, username string, password string, client *h
return "", fmt.Errorf("Got bad response from registry: " + tokenResponse.Status)
}

body, err := ioutil.ReadAll(tokenResponse.Body)
body, err := io.ReadAll(tokenResponse.Body)
if err != nil {
return "", fmt.Errorf("Error reading response body: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/data_source_docker_registry_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"regexp"
"testing"
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestGetDigestFromResponse(t *testing.T) {
Header: http.Header{
"Docker-Content-Digest": []string{headerContent},
},
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
Body: io.NopCloser(bytes.NewReader([]byte("foo"))),
}

if digest, _ := getDigestFromResponse(respWithHeaders); digest != headerContent {
Expand All @@ -103,7 +103,7 @@ func TestGetDigestFromResponse(t *testing.T) {
bodyDigest := "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"
respWithoutHeaders := &http.Response{
Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewReader([]byte("bar"))),
Body: io.NopCloser(bytes.NewReader([]byte("bar"))),
}

if digest, _ := getDigestFromResponse(respWithoutHeaders); digest != bodyDigest {
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/resource_docker_container_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -449,7 +448,7 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
contentToUpload = string(decoded)
}
if source != "" {
sourceContent, err := ioutil.ReadFile(source)
sourceContent, err := os.ReadFile(source)
if err != nil {
return diag.Errorf("could not read file: %s", err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/provider/resource_docker_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -739,7 +738,7 @@ func TestAccDockerContainer_uploadSource(t *testing.T) {

wd, _ := os.Getwd()
testFile := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "testingFile"), "\\", "\\\\")
testFileContent, _ := ioutil.ReadFile(testFile)
testFileContent, _ := os.ReadFile(testFile)

testCheck := func(*terraform.State) error {
client := testAccProvider.Meta().(*ProviderConfig).DockerClient
Expand Down Expand Up @@ -816,7 +815,7 @@ func TestAccDockerContainer_uploadSourceHash(t *testing.T) {

wd, _ := os.Getwd()
testFile := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "testingFile"), "\\", "\\\\")
hash, _ := ioutil.ReadFile(testFile + ".base64")
hash, _ := os.ReadFile(testFile + ".base64")
grabFirstCheck := func(*terraform.State) error {
firstRunId = c.ID
return nil
Expand Down
5 changes: 2 additions & 3 deletions internal/provider/resource_docker_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -429,7 +428,7 @@ func TestAccDockerImage_build(t *testing.T) {
ctx := context.Background()
wd, _ := os.Getwd()
dfPath := filepath.Join(wd, "Dockerfile")
if err := ioutil.WriteFile(dfPath, []byte(testDockerFileExample), 0o644); err != nil {
if err := os.WriteFile(dfPath, []byte(testDockerFileExample), 0o644); err != nil {
t.Fatalf("failed to create a Dockerfile %s for test: %+v", dfPath, err)
}
defer os.Remove(dfPath)
Expand Down Expand Up @@ -467,7 +466,7 @@ func TestAccDockerImage_buildOutsideContext(t *testing.T) {
ctx := context.Background()
wd, _ := os.Getwd()
dfPath := filepath.Join(wd, "..", "Dockerfile")
if err := ioutil.WriteFile(dfPath, []byte(testDockerFileExample), 0o644); err != nil {
if err := os.WriteFile(dfPath, []byte(testDockerFileExample), 0o644); err != nil {
t.Fatalf("failed to create a Dockerfile %s for test: %+v", dfPath, err)
}
defer os.Remove(dfPath)
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_docker_plugin_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"io"
"strings"

"github.com/docker/distribution/reference"
Expand Down Expand Up @@ -34,7 +34,7 @@ func resourceDockerPluginCreate(d *schema.ResourceData, meta interface{}) error
if err != nil {
return fmt.Errorf("install a Docker plugin "+pluginName+": %w", err)
}
_, _ = ioutil.ReadAll(body)
_, _ = io.ReadAll(body)
key := pluginName
if alias != "" {
key = alias
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,7 +45,7 @@ func loadTestConfiguration(t *testing.T, resourceType resourceType, resourceName

testConfig := strings.ReplaceAll(filepath.Join(wd, "..", "..", TEST_CONFIG_BASE_DIR, resourceType.String(), resourceName, fmt.Sprintf("%s.tf", testName)), "\\", "\\\\")

testConfigContent, err := ioutil.ReadFile(testConfig)
testConfigContent, err := os.ReadFile(testConfig)
if err != nil {
t.Errorf("failed to read test configuration at '%s': %v", testConfig, err)
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/testing/v1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)

const listenAddr = ":8080"
Expand All @@ -15,7 +15,7 @@ type config struct {
}

func main() {
configsContent, err := ioutil.ReadFile("configs.json")
configsContent, err := os.ReadFile("configs.json")
if err != nil {
log.Fatalf("cannot open 'configs.json': %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/testing/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)

const listenAddr = ":8080"
Expand All @@ -15,7 +15,7 @@ type config struct {
}

func main() {
configsContent, err := ioutil.ReadFile("configs.json")
configsContent, err := os.ReadFile("configs.json")
if err != nil {
log.Fatalf("cannot open 'configs.json': %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/testing/v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)

const listenAddr = ":8085" // changed here on purpose
Expand All @@ -15,7 +15,7 @@ type config struct {
}

func main() {
configsContent, err := ioutil.ReadFile("configs.json")
configsContent, err := os.ReadFile("configs.json")
if err != nil {
log.Fatalf("cannot open 'configs.json': %s", err)
}
Expand Down

0 comments on commit d7ce42b

Please sign in to comment.