Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil (#1167)
Browse files Browse the repository at this point in the history
Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
  • Loading branch information
testwill authored Nov 15, 2023
1 parent 6cb25c2 commit 1554be3
Show file tree
Hide file tree
Showing 37 changed files with 86 additions and 112 deletions.
5 changes: 2 additions & 3 deletions alpha/action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package action
import (
"fmt"
"io"
"io/ioutil"

"github.com/h2non/filetype"

Expand All @@ -25,15 +24,15 @@ func (i Init) Run() (*declcfg.Package, error) {
DefaultChannel: i.DefaultChannel,
}
if i.DescriptionReader != nil {
descriptionData, err := ioutil.ReadAll(i.DescriptionReader)
descriptionData, err := io.ReadAll(i.DescriptionReader)
if err != nil {
return nil, fmt.Errorf("read description: %v", err)
}
pkg.Description = string(descriptionData)
}

if i.IconReader != nil {
iconData, err := ioutil.ReadAll(i.IconReader)
iconData, err := io.ReadAll(i.IconReader)
if err != nil {
return nil, fmt.Errorf("read icon: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions alpha/action/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package action
import (
"context"
"fmt"
"io/ioutil"
"os"

"github.com/operator-framework/operator-registry/alpha/declcfg"
Expand All @@ -20,7 +19,7 @@ type Migrate struct {
}

func (m Migrate) Run(ctx context.Context) error {
entries, err := ioutil.ReadDir(m.OutputDir)
entries, err := os.ReadDir(m.OutputDir)
if err != nil && !os.IsNotExist(err) {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions alpha/action/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -60,7 +60,7 @@ type Render struct {

func nullLogger() *logrus.Entry {
logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
return logrus.NewEntry(logger)
}

Expand Down Expand Up @@ -155,7 +155,7 @@ func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.D
if err != nil {
return nil, err
}
tmpDir, err := ioutil.TempDir("", "render-unpack-")
tmpDir, err := os.MkdirTemp("", "render-unpack-")
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/opm/alpha/bundle/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -99,7 +98,7 @@ func unpackBundle(cmd *cobra.Command, args []string) error {
}
if rootCA != "" {
rootCAs := x509.NewCertPool()
certs, err := ioutil.ReadFile(rootCA)
certs, err := os.ReadFile(rootCA)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,7 +128,7 @@ func unpackBundle(cmd *cobra.Command, args []string) error {
return err
}

dir, err := ioutil.TempDir("", "bundle-")
dir, err := os.MkdirTemp("", "bundle-")
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/opm/alpha/bundle/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundle

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

Expand Down Expand Up @@ -80,7 +79,7 @@ func validateFunc(cmd *cobra.Command, _ []string) error {
}
imageValidator := bundle.NewImageValidator(registry, logger, optional)

dir, err := ioutil.TempDir("", "bundle-")
dir, err := os.MkdirTemp("", "bundle-")
logger.Infof("Create a temp directory at %s", dir)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions cmd/opm/alpha/template/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package template

import (
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -50,7 +49,7 @@ When FILE is '-' or not provided, the template is read from standard input`,
// The bundle loading impl is somewhat verbose, even on the happy path,
// so discard all logrus default logger logs. Any important failures will be
// returned from template.Render and logged as fatal errors.
logrus.SetOutput(ioutil.Discard)
logrus.SetOutput(io.Discard)

reg, err := util.CreateCLIRegistry(cmd)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/opm/alpha/template/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package template
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -53,7 +52,7 @@ When FILE is '-' or not provided, the template is read from standard input`,
// The bundle loading impl is somewhat verbose, even on the happy path,
// so discard all logrus default logger logs. Any important failures will be
// returned from template.Render and logged as fatal errors.
logrus.SetOutput(ioutil.Discard)
logrus.SetOutput(io.Discard)

reg, err := util.CreateCLIRegistry(cmd)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/opm/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package util

import (
"errors"
"io/ioutil"
"io"
"os"

"github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
Expand Down Expand Up @@ -69,6 +69,6 @@ func CreateCLIRegistry(cmd *cobra.Command) (*containerdregistry.Registry, error)

func nullLogger() *logrus.Entry {
logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
return logrus.NewEntry(logger)
}
5 changes: 2 additions & 3 deletions cmd/opm/render/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package render

import (
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -45,7 +44,7 @@ database files.
// The bundle loading impl is somewhat verbose, even on the happy path,
// so discard all logrus default logger logs. Any important failures will be
// returned from render.Run and logged as fatal errors.
logrus.SetOutput(ioutil.Discard)
logrus.SetOutput(io.Discard)

reg, err := util.CreateCLIRegistry(cmd)
if err != nil {
Expand All @@ -72,6 +71,6 @@ database files.

func nullLogger() *logrus.Entry {
logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
return logrus.NewEntry(logger)
}
5 changes: 2 additions & 3 deletions pkg/configmap/configmap_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package configmap
import (
"context"
"fmt"
"io/ioutil"
"os"
"regexp"

Expand Down Expand Up @@ -85,7 +84,7 @@ func (c *ConfigMapWriter) Populate(maxDataSizeLimit uint64) error {
var totalSize uint64
for _, dir := range subDirs {
completePath := c.manifestsDir + dir
files, err := ioutil.ReadDir(completePath)
files, err := os.ReadDir(completePath)
if err != nil {
logrus.Errorf("read dir failed: %v", err)
return err
Expand All @@ -95,7 +94,7 @@ func (c *ConfigMapWriter) Populate(maxDataSizeLimit uint64) error {
log := logrus.WithField("file", completePath+file.Name())
log.Info("Reading file")

content, err := ioutil.ReadFile(completePath + file.Name())
content, err := os.ReadFile(completePath + file.Name())
if err != nil {
log.Errorf("read failed: %v", err)
return err
Expand Down
3 changes: 1 addition & 2 deletions pkg/image/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -40,7 +39,7 @@ func (i *MockImage) unpack(dir string) error {
if err := os.MkdirAll(pathDir, 0777); err != nil {
return err
}
return ioutil.WriteFile(path, data, 0666)
return os.WriteFile(path, data, 0666)
})
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/image/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"math"
"math/rand"
"net/http"
Expand Down Expand Up @@ -35,7 +34,7 @@ type newRegistryFunc func(t *testing.T, cafile string) (image.Registry, cleanupF

func poolForCertFile(t *testing.T, file string) *x509.CertPool {
rootCAs := x509.NewCertPool()
certs, err := ioutil.ReadFile(file)
certs, err := os.ReadFile(file)
require.NoError(t, err)
require.True(t, rootCAs.AppendCertsFromPEM(certs))
return rootCAs
Expand Down
3 changes: 1 addition & 2 deletions pkg/lib/bundle/chartutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
package bundle

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

Expand Down Expand Up @@ -125,7 +124,7 @@ func IsChartDir(dirName string) (bool, error) {
return false, errors.Errorf("no %s exists in directory %q", ChartfileName, dirName)
}

chartYamlContent, err := ioutil.ReadFile(chartYaml)
chartYamlContent, err := os.ReadFile(chartYaml)
if err != nil {
return false, errors.Errorf("cannot read %s in directory %q", ChartfileName, dirName)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/lib/bundle/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundle

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

Expand Down Expand Up @@ -34,7 +33,7 @@ func (i *BundleExporter) Export(skipTLSVerify, plainHTTP bool) error {

log := logrus.WithField("img", i.image)

tmpDir, err := ioutil.TempDir("./", "bundle_tmp")
tmpDir, err := os.MkdirTemp("./", "bundle_tmp")
if err != nil {
return err
}
Expand Down
17 changes: 10 additions & 7 deletions pkg/lib/bundle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bundle
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -180,7 +179,7 @@ func CopyYamlOutput(annotationsContent []byte, manifestDir, outputDir, workingDi
}

// Now, generate the `metadata/` dir and write the annotations
file, err := ioutil.ReadFile(filepath.Join(copyDir, MetadataDir, AnnotationsFile))
file, err := os.ReadFile(filepath.Join(copyDir, MetadataDir, AnnotationsFile))
if os.IsNotExist(err) || overwrite {
writeDir := filepath.Join(copyDir, MetadataDir)
err = WriteFile(AnnotationsFile, writeDir, annotationsContent)
Expand Down Expand Up @@ -209,7 +208,7 @@ func GetMediaType(directory string) (string, error) {
k8sFiles := make(map[string]*unstructured.Unstructured)

// Read all file names in directory
items, _ := ioutil.ReadDir(directory)
items, _ := os.ReadDir(directory)
for _, item := range items {
if item.IsDir() {
continue
Expand All @@ -218,7 +217,7 @@ func GetMediaType(directory string) (string, error) {
files = append(files, item.Name())

fileWithPath := filepath.Join(directory, item.Name())
fileBlob, err := ioutil.ReadFile(fileWithPath)
fileBlob, err := os.ReadFile(fileWithPath)
if err != nil {
return "", fmt.Errorf("Unable to read file %s in bundle", fileWithPath)
}
Expand Down Expand Up @@ -367,7 +366,7 @@ func WriteFile(fileName, directory string, content []byte) error {
}
}
log.Infof("Writing %s in %s", fileName, directory)
err := ioutil.WriteFile(filepath.Join(directory, fileName), content, DefaultPermission)
err := os.WriteFile(filepath.Join(directory, fileName), content, DefaultPermission)
if err != nil {
return err
}
Expand All @@ -376,7 +375,7 @@ func WriteFile(fileName, directory string, content []byte) error {

// copy the contents of a potentially nested manifest dir into an output dir.
func copyManifestDir(from, to string, overwrite bool) error {
fromFiles, err := ioutil.ReadDir(from)
fromFiles, err := os.ReadDir(from)
if err != nil {
return err
}
Expand Down Expand Up @@ -431,7 +430,11 @@ func copyManifestDir(from, to string, overwrite bool) error {
return err
}

err = os.Chmod(toFilePath, fromFile.Mode())
info, err := fromFile.Info()
if err != nil {
return err
}
err = os.Chmod(toFilePath, info.Mode())
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 1554be3

Please sign in to comment.