Skip to content

Commit

Permalink
refactor: remove uses of deprecated io/ioutil package (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherFry authored Oct 30, 2023
1 parent fe15a8d commit fc50fec
Show file tree
Hide file tree
Showing 28 changed files with 72 additions and 90 deletions.
9 changes: 4 additions & 5 deletions cmd/gen-core-scoper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

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

Expand All @@ -40,17 +39,17 @@ func CoreScoper() Scoper {

func main() {
// Types available on a default-created cluster for both Kubernetes 1.15 and 1.18.
apiResources115, err := ioutil.ReadFile("cmd/gen-core-scoper/api_resources_1_15.txt")
apiResources115, err := os.ReadFile("cmd/gen-core-scoper/api_resources_1_15.txt")
if err != nil {
panic(err)
}
apiResources118, err := ioutil.ReadFile("cmd/gen-core-scoper/api_resources_1_18.txt")
apiResources118, err := os.ReadFile("cmd/gen-core-scoper/api_resources_1_18.txt")
if err != nil {
panic(err)
}
apiResources := append(apiResources115, apiResources118...)
// Read Nomos-specific types which are not available on a default-created cluster.
nomosResources, err := ioutil.ReadFile("cmd/gen-core-scoper/nomos_resources.txt")
nomosResources, err := os.ReadFile("cmd/gen-core-scoper/nomos_resources.txt")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -94,7 +93,7 @@ func main() {
}
`)

err = ioutil.WriteFile("pkg/util/discovery/core_scoper.generated.go", []byte(sb.String()), os.ModePerm)
err = os.WriteFile("pkg/util/discovery/core_scoper.generated.go", []byte(sb.String()), os.ModePerm)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/nomos/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package initialize

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -117,7 +116,7 @@ func Initialize(root string, force bool) error {
}

func checkEmpty(dir cmpath.Absolute) error {
files, err := ioutil.ReadDir(dir.OSPath())
files, err := os.ReadDir(dir.OSPath())
if err != nil {
return errors.Wrapf(err, "error reading %q", dir)
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/nomos/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package migrate
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -415,7 +414,7 @@ func saveRootSyncYAML(ctx context.Context, cm *util.ConfigManagementClient, cont
}

yamlFile := filepath.Join(dir, rootSyncYamlFile)
if err := ioutil.WriteFile(yamlFile, content, 0644); err != nil {
if err := os.WriteFile(yamlFile, content, 0644); err != nil {
return rs, yamlFile, err
}
printInfo("A RootSync object is generated and saved in %q", yamlFile)
Expand All @@ -437,7 +436,7 @@ func saveConfigManagementYAML(ctx context.Context, cm *util.ConfigManagementClie
return cmMulti, "", err
}
yamlFile := filepath.Join(dir, cmOrigYAMLFile)
if err := ioutil.WriteFile(yamlFile, content, 0644); err != nil {
if err := os.WriteFile(yamlFile, content, 0644); err != nil {
return cmMulti, yamlFile, err
}
printInfo("The original ConfigManagement object is saved in %q", yamlFile)
Expand All @@ -447,7 +446,7 @@ func saveConfigManagementYAML(ctx context.Context, cm *util.ConfigManagementClie
return cmMulti, "", err
}
yamlFile = filepath.Join(dir, cmMultiYAMLFile)
if err := ioutil.WriteFile(yamlFile, content, 0644); err != nil {
if err := os.WriteFile(yamlFile, content, 0644); err != nil {
return cmMulti, yamlFile, err
}
printInfo("The ConfigManagement object is updated and saved in %q", yamlFile)
Expand Down
3 changes: 1 addition & 2 deletions cmd/nomos/parse/find_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package parse

import (
"io/ioutil"
"os"
"os/exec"
"sort"
Expand Down Expand Up @@ -51,7 +50,7 @@ func TestListPolicyFiles(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

dirP, err := ioutil.TempDir(os.TempDir(), "nomos-git-test")
dirP, err := os.MkdirTemp(os.TempDir(), "nomos-git-test")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/nomos/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"runtime"
Expand Down Expand Up @@ -63,7 +62,7 @@ func init() {
// caused by the os.Pipe buffer limit: 64k.
// This function is only used in `nomos bugreport` for the `nomos status` output.
func SaveToTempFile(ctx context.Context, contexts []string) (*os.File, error) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "nomos-status-")
tmpFile, err := os.CreateTemp(os.TempDir(), "nomos-status-")
if err != nil {
return nil, fmt.Errorf("failed to create a temporary file: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/nomos/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -62,7 +61,7 @@ func GetVersionReadCloser(ctx context.Context, contexts []string) (io.ReadCloser
return nil, errors.Wrap(err, "failed to close version file writer with error: %v")
}

return ioutil.NopCloser(r), nil
return io.NopCloser(r), nil
}

var (
Expand Down
3 changes: 1 addition & 2 deletions e2e/nomostest/artifactregistry/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package artifactregistry

import (
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -48,7 +47,7 @@ func WriteObjectYAMLFile(nt *nomostest.NT, path string, obj client.Object) error
return errors.Wrapf(err, "making parent directories: %s", dirPath)
}
}
if err := ioutil.WriteFile(path, bytes, fileMode); err != nil {
if err := os.WriteFile(path, bytes, fileMode); err != nil {
return errors.Wrapf(err, "writing file: %s", path)
}
return nil
Expand Down
9 changes: 4 additions & 5 deletions e2e/nomostest/config_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package nomostest
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -215,15 +214,15 @@ func convertToTypedObjects(nt *NT, objs []client.Object) ([]client.Object, error
}

func copyFile(src, dst string) error {
bytes, err := ioutil.ReadFile(src)
bytes, err := os.ReadFile(src)
if err != nil {
return err
}
return ioutil.WriteFile(dst, bytes, fileMode)
return os.WriteFile(dst, bytes, fileMode)
}

func copyDirContents(src, dest string) error {
files, err := ioutil.ReadDir(src)
files, err := os.ReadDir(src)
if err != nil {
return err
}
Expand Down Expand Up @@ -271,7 +270,7 @@ func parseManifestDir(dirPath string) ([]client.Object, error) {
if err != nil {
return nil, err
}
files, err := ioutil.ReadDir(dirPath)
files, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions e2e/nomostest/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package nomostest

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -112,7 +112,7 @@ func checkImage(image string) error {
defer func() {
_ = resp.Body.Close()
}()
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
if err != nil {
return errors.Errorf("Failed to read response for image %s in registry: %s", image, err)
}
Expand Down
5 changes: 2 additions & 3 deletions e2e/nomostest/gitproviders/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package gitproviders
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -441,7 +440,7 @@ func (g *Repository) AddFile(path string, bytes []byte) error {
}

// Write bytes to file.
if err := ioutil.WriteFile(absPath, bytes, fileMode); err != nil {
if err := os.WriteFile(absPath, bytes, fileMode); err != nil {
return errors.Wrapf(err, "writing file: %s", absPath)
}

Expand All @@ -463,7 +462,7 @@ func (g *Repository) AddEmptyDir(path string) error {
func (g *Repository) GetFile(path string) ([]byte, error) {
absPath := filepath.Join(g.Root, path)

bytes, err := ioutil.ReadFile(absPath)
bytes, err := os.ReadFile(absPath)
if err != nil {
return nil, errors.Wrapf(err, "reading file: %s", absPath)
}
Expand Down
3 changes: 1 addition & 2 deletions e2e/nomostest/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package nomostest

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -501,7 +500,7 @@ func TestDir(t nomostesting.NTB) string {
if err != nil {
t.Fatalf("creating nomos-e2e tmp directory: %v", err)
}
tmpDir, err := ioutil.TempDir(filepath.Join(os.TempDir(), NomosE2E), name)
tmpDir, err := os.MkdirTemp(filepath.Join(os.TempDir(), NomosE2E), name)
if err != nil {
t.Fatalf("creating nomos-e2e tmp test subdirectory %s: %v", tmpDir, err)
}
Expand Down
3 changes: 1 addition & 2 deletions e2e/nomostest/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"net"
"os"
Expand Down Expand Up @@ -249,7 +248,7 @@ func downloadSSHKey(nt *NT) string {
nt.T.Fatal("downloading SSH key:", err)
}

if err := ioutil.WriteFile(privateKeyPath(nt), []byte(out), 0600); err != nil {
if err := os.WriteFile(privateKeyPath(nt), []byte(out), 0600); err != nil {
nt.T.Fatal("saving SSH key:", err)
}

Expand Down
8 changes: 4 additions & 4 deletions e2e/testcases/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package e2e

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -98,7 +98,7 @@ metadata:
name: test-ns
`)

if err := ioutil.WriteFile(filepath.Join(nt.TmpDir, "test-ns.yaml"), ns, 0644); err != nil {
if err := os.WriteFile(filepath.Join(nt.TmpDir, "test-ns.yaml"), ns, 0644); err != nil {
nt.T.Fatalf("failed to create a tmp file %v", err)
}

Expand All @@ -125,7 +125,7 @@ metadata:
name: test-ns
`)

if err := ioutil.WriteFile(filepath.Join(nt.TmpDir, "test-ns.yaml"), ns, 0644); err != nil {
if err := os.WriteFile(filepath.Join(nt.TmpDir, "test-ns.yaml"), ns, 0644); err != nil {
nt.T.Fatalf("failed to create a tmp file %v", err)
}

Expand Down Expand Up @@ -161,7 +161,7 @@ metadata:
nt.T.Fatal(err)
}

if err := ioutil.WriteFile(filepath.Join(nt.TmpDir, "webhook.yaml"), webhook, 0644); err != nil {
if err := os.WriteFile(filepath.Join(nt.TmpDir, "webhook.yaml"), webhook, 0644); err != nil {
nt.T.Fatalf("failed to create a tmp file %v", err)
}

Expand Down
3 changes: 1 addition & 2 deletions e2e/testcases/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -981,7 +980,7 @@ func TestNomosImage(t *testing.T) {
// getBugReportZipName find and returns the zip name of bugreport under test dir
// or error if no bugreport zip found
func getBugReportZipName(dir string, nt *nomostest.NT) (string, error) {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
nt.T.Fatal(err)
}
Expand Down
10 changes: 5 additions & 5 deletions e2e/testcases/custom_resource_definitions_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package e2e

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -36,11 +36,11 @@ func TestChangeCustomResourceDefinitionSchema(t *testing.T) {
newCRFile := filepath.Join(".", "..", "testdata", "customresources", "changed_schema_crds", "new_schema_cr.yaml")

// Add a CRD and CR to the repo
crdContent, err := ioutil.ReadFile(oldCRDFile)
crdContent, err := os.ReadFile(oldCRDFile)
if err != nil {
nt.T.Fatal(err)
}
crContent, err := ioutil.ReadFile(oldCRFile)
crContent, err := os.ReadFile(oldCRFile)
if err != nil {
nt.T.Fatal(err)
}
Expand All @@ -65,11 +65,11 @@ func TestChangeCustomResourceDefinitionSchema(t *testing.T) {
}

// Add the CRD with a new schema and a CR using the new schema to the repo
crdContent, err = ioutil.ReadFile(newCRDFile)
crdContent, err = os.ReadFile(newCRDFile)
if err != nil {
nt.T.Fatal(err)
}
crContent, err = ioutil.ReadFile(newCRFile)
crContent, err = os.ReadFile(newCRFile)
if err != nil {
nt.T.Fatal(err)
}
Expand Down
Loading

0 comments on commit fc50fec

Please sign in to comment.