Skip to content

Commit

Permalink
refactor: use os.ReadDir for lightweight directory reading
Browse files Browse the repository at this point in the history
This commit also replaces the existing io/ioutil functions with their
new definitions in io and os packages, since the io/ioutil package has
been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil.

Reference: https://pkg.go.dev/io/ioutil#ReadDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored and ciarams87 committed Feb 7, 2022
1 parent 83b5fbc commit 8504d46
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
6 changes: 3 additions & 3 deletions internal/metrics/collectors/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package collectors
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strconv"

"github.com/golang/glog"
Expand Down Expand Up @@ -45,7 +45,7 @@ func getWorkerProcesses() (int, int, error) {
var workerProcesses int
var prevWorkerProcesses int

procFolders, err := ioutil.ReadDir("/proc")
procFolders, err := os.ReadDir("/proc")
if err != nil {
return 0, 0, fmt.Errorf("unable to read directory /proc : %w", err)
}
Expand All @@ -57,7 +57,7 @@ func getWorkerProcesses() (int, int, error) {
}

cmdlineFile := fmt.Sprintf("/proc/%v/cmdline", folder.Name())
content, err := ioutil.ReadFile(cmdlineFile)
content, err := os.ReadFile(cmdlineFile)
if err != nil {
return 0, 0, fmt.Errorf("unable to read file %v: %w", cmdlineFile, err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/metrics/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metrics

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -75,7 +74,7 @@ func runServer(port string, registry prometheus.Gatherer, prometheusSecret *api_
}

func writeTempFile(data []byte, name string) (*os.File, error) {
f, err := ioutil.TempFile("", name)
f, err := os.CreateTemp("", name)
if err != nil {
return nil, fmt.Errorf("failed to create temp file: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/nginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nginx

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -265,7 +264,7 @@ func (lm *LocalManager) DeleteAppProtectResourceFile(name string) {

// ClearAppProtectFolder clears contents of a config folder
func (lm *LocalManager) ClearAppProtectFolder(name string) {
files, err := ioutil.ReadDir(name)
files, err := os.ReadDir(name)
if err != nil {
glog.Fatalf("Failed to read the App Protect folder %s: %v", name, err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/nginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nginx
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -51,7 +50,7 @@ func createFileAndWrite(name string, b []byte) error {
}

func createFileAndWriteAtomically(filename string, tempPath string, mode os.FileMode, content []byte) {
file, err := ioutil.TempFile(tempPath, path.Base(filename))
file, err := os.CreateTemp(tempPath, path.Base(filename))
if err != nil {
glog.Fatalf("Couldn't create a temp file for the file %v: %v", filename, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/nginx/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"fmt"
"html/template"
"io/ioutil"
"io"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -47,7 +47,7 @@ func (c *verifyClient) GetConfigVersion() (int, error) {
return 0, fmt.Errorf("non-200 response: %v", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("failed to read the response body: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/nginx/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package nginx

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand All @@ -14,7 +14,7 @@ type Transport struct{}
func (c Transport) RoundTrip(_ *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("42")),
Body: io.NopCloser(bytes.NewBufferString("42")),
Header: make(http.Header),
}, nil
}
Expand Down

0 comments on commit 8504d46

Please sign in to comment.