Skip to content

chore: stop using deprecated io/ioutil package #180

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
Jan 10, 2024
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
6 changes: 3 additions & 3 deletions cmd/cdi/cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cmd

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

oci "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -66,9 +66,9 @@ func readOCISpec(path string) (*oci.Spec, error) {
)

if path == "-" {
data, err = ioutil.ReadAll(os.Stdin)
data, err = io.ReadAll(os.Stdin)
} else {
data, err = ioutil.ReadFile(path)
data, err = os.ReadFile(path)
}

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"os"

"tags.cncf.io/container-device-interface/schema"
Expand Down Expand Up @@ -80,7 +80,7 @@ func main() {
for _, docFile = range docs {
if docFile == "" || docFile == "-" {
docFile = "<stdin>"
docData, err = ioutil.ReadAll(os.Stdin)
docData, err = io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read document data from stdin: %v\n", err)
os.Exit(1)
Expand Down
5 changes: 2 additions & 3 deletions pkg/cdi/spec-dirs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cdi

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -212,7 +211,7 @@ devices:

// Create an automatically cleaned up temporary directory, with optional content.
func mkTestDir(t *testing.T, dirs map[string]map[string]string) (string, error) {
tmp, err := ioutil.TempDir("", ".cache-test*")
tmp, err := os.MkdirTemp("", ".cache-test*")
if err != nil {
return "", fmt.Errorf("failed to create test directory: %w", err)
}
Expand All @@ -237,7 +236,7 @@ func updateTestDir(t *testing.T, tmp string, dirs map[string]map[string]string)
for file, data := range content {
file := filepath.Join(dir, file)
tmp := file + ".tmp"
if err := ioutil.WriteFile(tmp, []byte(data), 0644); err != nil {
if err := os.WriteFile(tmp, []byte(data), 0644); err != nil {
return fmt.Errorf("failed to write file %q: %w", tmp, err)
}
if err := os.Rename(tmp, file); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/cdi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cdi
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -62,7 +61,7 @@ type Spec struct {
// assigned the given priority. If reading or parsing the Spec
// data fails ReadSpec returns a nil Spec and an error.
func ReadSpec(path string, priority int) (*Spec, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
switch {
case os.IsNotExist(err):
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions pkg/cdi/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cdi

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -496,7 +495,7 @@ devices:

// Create an automatically cleaned up temporary file for a test.
func mkTestSpec(t *testing.T, data []byte) (string, error) {
tmp, err := ioutil.TempFile("", ".cdi-test.*."+specType(data))
tmp, err := os.CreateTemp("", ".cdi-test.*."+specType(data))
if err != nil {
return "", fmt.Errorf("failed to create test file: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -158,7 +158,7 @@ func Load(source string) (*Schema, error) {
// ReadAndValidate all data from the given reader, using the schema for validation.
func (s *Schema) ReadAndValidate(r io.Reader) ([]byte, error) {
loader, reader := schema.NewReaderLoader(r)
data, err := ioutil.ReadAll(reader)
data, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("failed to read data for validation: %w", err)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func (s *Schema) ValidateFile(path string) error {
return s.validate(schema.NewReferenceLoader("file://" + path))
}

data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -269,7 +268,7 @@ func validateFile(t *testing.T, scm *schema.Schema, path string, shouldLoad, isV
}

func validateData(t *testing.T, scm *schema.Schema, path string, shouldLoad, isValid bool) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
require.NoError(t, err)

if scm != nil {
Expand Down