Skip to content

Commit

Permalink
Remove readFile() and reverseSlice() in favour of stdlib (#671)
Browse files Browse the repository at this point in the history
* Update updater.go

remove readFile() as it appears to essentially be a re-implementation of stdlib's os.ReadFile

Signed-off-by: udf2457 <[email protected]>

* Remove ReadFile from cli/tuf-client/cmd/root.go

Another instance of re-implementing os.ReadFile.  This one does not even appear to be used by the cli code.

Signed-off-by: udf2457 <[email protected]>

* Update init.go

Reference os.Readfile instead of the re-implementation

Signed-off-by: udf2457 <[email protected]>

* Update root.go

Remove ReadFile re-implementation

Signed-off-by: udf2457 <[email protected]>

* Update metadata.go to use os.ReadFile

Don't reinvent the wheel, use stdlib os.ReadFile 😉 

Signed-off-by: udf2457 <[email protected]>

* Update updater.go - remove reimplementation of slices.Reverse()

Remive the reimplementation of stdlib slices.Reverse()

Signed-off-by: udf2457 <[email protected]>

* Update root.go

remove un-necessary io import

Signed-off-by: udf2457 <[email protected]>

* Update root.go

Remove io import

Signed-off-by: udf2457 <[email protected]>

---------

Signed-off-by: udf2457 <[email protected]>
  • Loading branch information
udf2457 authored Jan 24, 2025
1 parent 110bec9 commit 423f3ab
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 69 deletions.
2 changes: 1 addition & 1 deletion examples/cli/tuf-client/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func InitializeCmd() error {
}

// read the content of root.json
rootBytes, err := ReadFile(rootPath)
rootBytes, err := os.ReadFile(rootPath)
if err != nil {
return err
}
Expand Down
15 changes: 0 additions & 15 deletions examples/cli/tuf-client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package cmd

import (
"io"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -57,17 +56,3 @@ func Execute() {
os.Exit(1)
}
}

// ReadFile reads the content of a file and return its bytes
func ReadFile(name string) ([]byte, error) {
in, err := os.Open(name)
if err != nil {
return nil, err
}
defer in.Close()
data, err := io.ReadAll(in)
if err != nil {
return nil, err
}
return data, nil
}
15 changes: 0 additions & 15 deletions examples/cli/tuf/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package cmd

import (
"io"
"os"

"github.com/spf13/cobra"
Expand All @@ -42,17 +41,3 @@ func Execute() {
os.Exit(1)
}
}

// ReadFile reads the content of a file and return its bytes
func ReadFile(name string) ([]byte, error) {
in, err := os.Open(name)
if err != nil {
return nil, err
}
defer in.Close()
data, err := io.ReadAll(in)
if err != nil {
return nil, err
}
return data, nil
}
15 changes: 2 additions & 13 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,7 @@ func MetaFile(version int64) *MetaFiles {

// FromFile load metadata from file
func (meta *Metadata[T]) FromFile(name string) (*Metadata[T], error) {
in, err := os.Open(name)
if err != nil {
return nil, err
}
defer in.Close()
data, err := io.ReadAll(in)
data, err := os.ReadFile(name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -474,14 +469,8 @@ func (source *TargetFiles) Equal(expected TargetFiles) bool {
// FromFile generate TargetFiles from file
func (t *TargetFiles) FromFile(localPath string, hashes ...string) (*TargetFiles, error) {
log.Info("Generating target file from file", "path", localPath)
// open file
in, err := os.Open(localPath)
if err != nil {
return nil, err
}
defer in.Close()
// read file
data, err := io.ReadAll(in)
data, err := os.ReadFile(localPath)
if err != nil {
return nil, err
}
Expand Down
29 changes: 4 additions & 25 deletions metadata/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -283,7 +283,7 @@ func (update *Updater) FindCachedTarget(targetFile *metadata.TargetFiles, filePa
targetFilePath = filePath
}
// get file content
data, err := readFile(targetFilePath)
data, err := os.ReadFile(targetFilePath)
if err != nil {
// do not want to return err, instead we say that there's no cached target available
return "", nil, nil
Expand Down Expand Up @@ -569,7 +569,7 @@ func (update *Updater) preOrderDepthFirstWalk(targetFilePath string) (*metadata.
// push childRolesToVisit in reverse order of appearance
// onto delegationsToVisit. Roles are popped from the end of
// the list
reverseSlice(childRolesToVisit)
slices.Reverse(childRolesToVisit)
delegationsToVisit = append(delegationsToVisit, childRolesToVisit...)
}
}
Expand Down Expand Up @@ -663,7 +663,7 @@ func (update *Updater) generateTargetFilePath(tf *metadata.TargetFiles) (string,

// loadLocalMetadata reads a local <roleName>.json file and returns its bytes
func (update *Updater) loadLocalMetadata(roleName string) ([]byte, error) {
return readFile(fmt.Sprintf("%s.json", roleName))
return os.ReadFile(fmt.Sprintf("%s.json", roleName))
}

// GetTopLevelTargets returns the top-level target files
Expand Down Expand Up @@ -702,24 +702,3 @@ func ensureTrailingSlash(url string) string {
}
return url + "/"
}

// reverseSlice reverses the elements in a generic type of slice
func reverseSlice[S ~[]E, E any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}

// readFile reads the content of a file and return its bytes
func readFile(name string) ([]byte, error) {
in, err := os.Open(name)
if err != nil {
return nil, err
}
defer in.Close()
data, err := io.ReadAll(in)
if err != nil {
return nil, err
}
return data, nil
}

0 comments on commit 423f3ab

Please sign in to comment.