Skip to content
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

Remove readFile() and reverseSlice() in favour of stdlib #671

Merged
merged 8 commits into from
Jan 24, 2025
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
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
}
Loading