Skip to content

Commit

Permalink
refactor deprecated ioutil usage
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Valdron <[email protected]>
  • Loading branch information
michael-valdron committed Mar 7, 2024
1 parent e123696 commit d7f6da1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
36 changes: 29 additions & 7 deletions index/generator/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package library
import (
"encoding/json"
"fmt"
"io/ioutil"
"io/fs"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -104,7 +104,7 @@ func CreateIndexFile(index []schema.Schema, indexFilePath string) error {
}

/* #nosec G306 -- index file does not contain any sensitive data*/
err = ioutil.WriteFile(indexFilePath, bytes, 0644)
err = os.WriteFile(indexFilePath, bytes, 0644)
if err != nil {
return fmt.Errorf("failed to write %s: %v", indexFilePath, err)
}
Expand Down Expand Up @@ -242,10 +242,20 @@ func parseDevfileRegistry(registryDirPath string, force bool) ([]schema.Schema,

var index []schema.Schema
stackDirPath := path.Join(registryDirPath, "stacks")
stackDir, err := ioutil.ReadDir(stackDirPath)
dirEntries, err := os.ReadDir(stackDirPath)
if err != nil {
return nil, fmt.Errorf("failed to read stack directory %s: %v", stackDirPath, err)
}

stackDir := make([]fs.FileInfo, 0, len(dirEntries))
for i := 0; i < len(dirEntries); i++ {
info, err := dirEntries[i].Info()
if err != nil {
return nil, fmt.Errorf("failed to read stack directory info for %s: %v", stackDirPath, err)
}
stackDir[i] = info
}

for _, stackFolderDir := range stackDir {
if !stackFolderDir.IsDir() {
continue
Expand Down Expand Up @@ -352,7 +362,7 @@ func parseStackDevfile(devfileDirPath string, stackName string, force bool, vers
}

/* #nosec G304 -- devfilePath is produced using filepath.Join which cleans the input path */
bytes, err := ioutil.ReadFile(devfilePath)
bytes, err := os.ReadFile(devfilePath)
if err != nil {
return fmt.Errorf("failed to read %s: %v", devfilePath, err)
}
Expand Down Expand Up @@ -455,10 +465,22 @@ func parseStackDevfile(devfileDirPath string, stackName string, force bool, vers
}

// Get the files in the stack folder
stackFiles, err := ioutil.ReadDir(devfileDirPath)
fileEntries, err := os.ReadDir(devfileDirPath)
if err != nil {
return err
}

stackFiles := make([]fs.FileInfo, 0, len(fileEntries))

for i := 0; i < len(fileEntries); i++ {
info, err := fileEntries[i].Info()
if err != nil {
return err
}

stackFiles[i] = info
}

for _, stackFile := range stackFiles {
// The registry build should have already packaged any folders and miscellaneous files into an archive.tar file
// But, add this check as a safeguard, as OCI doesn't support unarchived folders being pushed up.
Expand All @@ -473,7 +495,7 @@ func parseExtraDevfileEntries(registryDirPath string, force bool) ([]schema.Sche
var index []schema.Schema
extraDevfileEntriesPath := path.Join(registryDirPath, extraDevfileEntries)
/* #nosec G304 -- extraDevfileEntriesPath is produced using path.Join which cleans the input path */
bytes, err := ioutil.ReadFile(extraDevfileEntriesPath)
bytes, err := os.ReadFile(extraDevfileEntriesPath)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %v", extraDevfileEntriesPath, err)
}
Expand Down Expand Up @@ -563,7 +585,7 @@ func parseExtraDevfileEntries(registryDirPath string, force bool) ([]schema.Sche
/* #nosec G304 -- stackYamlPath is produced from file.Join which cleans the input path */
func parseStackInfo(stackYamlPath string) (schema.Schema, error) {
var index schema.Schema
bytes, err := ioutil.ReadFile(stackYamlPath)
bytes, err := os.ReadFile(stackYamlPath)
if err != nil {
return schema.Schema{}, fmt.Errorf("failed to read %s: %v", stackYamlPath, err)
}
Expand Down
8 changes: 4 additions & 4 deletions index/generator/library/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package library
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -547,7 +547,7 @@ func TestValidateIndexComponent(t *testing.T) {
func TestParseDevfileRegistry(t *testing.T) {
registryDirPath := "../tests/registry"
wantIndexFilePath := "../tests/registry/index_registry.json"
bytes, err := ioutil.ReadFile(wantIndexFilePath)
bytes, err := os.ReadFile(wantIndexFilePath)
if err != nil {
t.Errorf("Failed to read index_registry.json: %v", err)
}
Expand Down Expand Up @@ -579,7 +579,7 @@ func TestParseDevfileRegistry(t *testing.T) {
func TestParseExtraDevfileEntries(t *testing.T) {
registryDirPath := "../tests/registry"
wantIndexFilePath := "../tests/registry/index_extra.json"
bytes, err := ioutil.ReadFile(wantIndexFilePath)
bytes, err := os.ReadFile(wantIndexFilePath)
if err != nil {
t.Errorf("Failed to read index_extra.json: %v", err)
}
Expand Down Expand Up @@ -611,7 +611,7 @@ func TestParseExtraDevfileEntries(t *testing.T) {
func TestGenerateIndexStruct(t *testing.T) {
registryDirPath := "../tests/registry"
wantIndexFilePath := "../tests/registry/index_main.json"
bytes, err := ioutil.ReadFile(wantIndexFilePath)
bytes, err := os.ReadFile(wantIndexFilePath)
if err != nil {
t.Errorf("Failed to read index_main.json: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions index/generator/library/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -71,7 +70,7 @@ func CloneRemoteStack(git *schema.Git, path string, verbose bool) (err error) {
originalPath := ""
if git.SubDir != "" {
originalPath = path
path, err = ioutil.TempDir("", "")
path, err = os.MkdirTemp("", "")
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +143,7 @@ func DownloadStackFromGit(git *schema.Git, path string, verbose bool) ([]byte, e

// Read bytes from response and return, error will be nil if successful
/* #nosec G304 -- zipPath is constructed from a clean path */
return ioutil.ReadFile(zipPath)
return os.ReadFile(zipPath)
}

// DownloadStackFromZipUrl downloads the zip file containing the stack at a given url, uses default filesystem
Expand Down Expand Up @@ -195,7 +194,7 @@ func downloadStackFromZipUrl(zipUrl string, subDir string, path string, fs files

// Read bytes from response and return, error will be nil if successful
/* #nosec G304 -- zipDest is produced using a cleaned path */
return ioutil.ReadFile(zipDst)
return os.ReadFile(zipDst)
}

// GitSubDir handles subDir for git components using the default filesystem
Expand Down
3 changes: 1 addition & 2 deletions index/generator/library/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package library

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -327,7 +326,7 @@ func TestZipDir(t *testing.T) {
t.Errorf("Failed to zip directory '%s': %v", dirPath, err)
}

bytes, err := ioutil.ReadFile(zipPath)
bytes, err := os.ReadFile(zipPath)
if err != nil {
t.Errorf("Unable to read zip file '%s': %v", zipPath, err)
}
Expand Down
5 changes: 2 additions & 3 deletions registry-library/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -179,7 +178,7 @@ func GetRegistryIndex(registryURL string, options RegistryOptions, devfileTypes
if err != nil {
return nil, err
}
bytes, err := ioutil.ReadAll(resp.Body)
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -454,7 +453,7 @@ func DownloadStarterProjectAsBytes(registryURL string, stack string, starterProj
}

// Return downloaded starter project as bytes or error if unsuccessful.
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

// IsStarterProjectExists checks if starter project exists for a given stack
Expand Down

0 comments on commit d7f6da1

Please sign in to comment.