Skip to content

Commit

Permalink
chore: replace deprecated ioutil with io and os
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslak committed Jan 15, 2025
1 parent c137f0c commit 77d9ba7
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 47 deletions.
11 changes: 3 additions & 8 deletions config/example_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package config_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -36,8 +35,7 @@ import (
func getExampleConfigs() (map[string][]byte, error) {
examplesPath := "../docs/examples"

files, err := ioutil.ReadDir(examplesPath)

files, err := os.ReadDir(examplesPath)
if err != nil {
return nil, err
}
Expand All @@ -62,14 +60,13 @@ func getExampleConfigs() (map[string][]byte, error) {
return bytes, nil
}

func readFileAndParseConfig(path string, info os.FileInfo) ([]byte, error) {
func readFileAndParseConfig(path string, info os.DirEntry) ([]byte, error) {
// Assuming we can parse all .json files in the example config directory
if filepath.Ext(info.Name()) != ".json" {
return nil, nil
}

data, err := ioutil.ReadFile(filepath.Join(path, info.Name()))

data, err := os.ReadFile(filepath.Join(path, info.Name()))
if err != nil {
return nil, err
}
Expand All @@ -79,7 +76,6 @@ func readFileAndParseConfig(path string, info os.FileInfo) ([]byte, error) {

func TestExampleConfigs(t *testing.T) {
bytebytes, err := getExampleConfigs()

if err != nil {
t.Errorf("Failed to read configuration files, %s", err)
}
Expand All @@ -91,7 +87,6 @@ func TestExampleConfigs(t *testing.T) {
// If the test passes it gets suppressed.
logrus.Debugf("Parsing %s", filename)
conf, err := config.Bytes(bytes)

if err != nil {
t.Errorf("Failed to parse config in %s %s", filename, err)
return
Expand Down
6 changes: 2 additions & 4 deletions config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -420,7 +419,7 @@ func Path(path string) (*Config, error) {
// File opens a config file and parses it, then returns the valid
// configuration, using Bytes()
func File(f string) (*Config, error) {
dat, err := ioutil.ReadFile(f)
dat, err := os.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("failed to read configuration file: %w", err)
}
Expand Down Expand Up @@ -457,8 +456,7 @@ func ReadFiles(p string) (*Config, error) {

for _, f := range files {
confLog.WithField("file", f).Debug("Reading file")
b, err := ioutil.ReadFile(f)

b, err := os.ReadFile(f)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions encoder/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package encoder_test

import (
"io/ioutil"
"os"
"testing"

"github.com/telenornms/skogul"
Expand Down Expand Up @@ -73,8 +73,7 @@ func testJSON(t *testing.T, file string, match bool) {

func parseJSON(t *testing.T, file string) (*skogul.Container, []byte) {
t.Helper()
b, err := ioutil.ReadFile(file)

b, err := os.ReadFile(file)
if err != nil {
t.Logf("Failed to read test data file: %v", err)
t.FailNow()
Expand Down
4 changes: 2 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package skogul

import (
"io/ioutil"
"io"
"strings"

"github.com/sirupsen/logrus"
Expand All @@ -36,7 +36,7 @@ var skogulLogger = logrus.New()
func ConfigureLogger(requestedLoglevel string, logtimestamp bool, logFormat string) {
// Disable output from the root logger; all logging is delegated through hooks
logrus.SetLevel(logrus.TraceLevel)
logrus.SetOutput(ioutil.Discard)
logrus.SetOutput(io.Discard)

loglevel := GetLogLevelFromString(requestedLoglevel)
skogulLogger.SetLevel(loglevel)
Expand Down
12 changes: 5 additions & 7 deletions parser/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package parser_test

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -108,8 +108,7 @@ func TestInfluxDBLineParseWithoutTimestamp(t *testing.T) {
}

func TestInfluxDBParseFile(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/influxdb.txt")

b, err := os.ReadFile("./testdata/influxdb.txt")
if err != nil {
t.Errorf("Failed to read test data file: %v", err)
return
Expand Down Expand Up @@ -259,8 +258,7 @@ func TestInfluxDBParseTelegrafCmdLine(t *testing.T) {
}

func TestInfluxDBParseTelegrafCmdLines(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/influxdb_procstat.txt")

b, err := os.ReadFile("./testdata/influxdb_procstat.txt")
if err != nil {
t.Errorf("Failed to read test data file: %v", err)
return
Expand Down Expand Up @@ -323,9 +321,9 @@ func TestInfluxDBParseDoubleBackslashEscape(t *testing.T) {
t.Errorf("expected parsed tag to equal %s, got %s", name, _container.Metrics[0].Metadata["name"])
}
}
func TestInfluxDBParseTelegrafSystemdUnitLines(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/influxdb_systemd_units.txt")

func TestInfluxDBParseTelegrafSystemdUnitLines(t *testing.T) {
b, err := os.ReadFile("./testdata/influxdb_systemd_units.txt")
if err != nil {
t.Errorf("Failed to read test data file: %v", err)
return
Expand Down
8 changes: 3 additions & 5 deletions parser/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package parser_test

import (
"io/ioutil"
"os"
"testing"

"github.com/telenornms/skogul/parser"
Expand All @@ -51,8 +51,7 @@ func BenchmarkSkogulJSONParse(b *testing.B) {
}

func TestJSONParse(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/raw.json")

b, err := os.ReadFile("./testdata/raw.json")
if err != nil {
t.Errorf("Failed to read test data file: %v", err)
return
Expand All @@ -72,8 +71,7 @@ func TestJSONParse(t *testing.T) {
}

func TestJSONArrayParse(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/raw_array.json")

b, err := os.ReadFile("./testdata/raw_array.json")
if err != nil {
t.Errorf("Failed to read test data file: %v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions parser/mnr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package parser_test

import (
"io/ioutil"
"os"
"testing"
"time"

Expand Down Expand Up @@ -170,7 +170,7 @@ func TestMNRExtractValues(t *testing.T) {
}

func TestMNROnDataset(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/mnr.txt")
b, err := os.ReadFile("./testdata/mnr.txt")
if err != nil {
t.Errorf("Failed to read test data file: %v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions parser/structured_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package parser_test

import (
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/telenornms/skogul/parser"
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestStructuredDataParseNoContentResultsInOneMetric(t *testing.T) {
}

func TestStructuredDataOnDataset(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/structured_data.txt")
b, err := os.ReadFile("./testdata/structured_data.txt")
if err != nil {
t.Errorf("Failed to read test data file: %v", err)
return
Expand Down
13 changes: 8 additions & 5 deletions receiver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"sync/atomic"

Expand Down Expand Up @@ -170,13 +170,15 @@ func (rcvr receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"code": code,
"remoteAddress": r.RemoteAddr,
"requestUri": r.RequestURI,
"ContentLength": r.ContentLength}).WithError(err).Warnf("HTTP request failed")
"ContentLength": r.ContentLength,
}).WithError(err).Warnf("HTTP request failed")
} else if rcvr.settings.Log204OK {
httpLog.WithFields(log.Fields{
"code": code,
"remoteAddress": r.RemoteAddr,
"requestUri": r.RequestURI,
"ContentLength": r.ContentLength}).Infof("HTTP request ok")
"ContentLength": r.ContentLength,
}).Infof("HTTP request ok")
}
answer(w, r, code, err)
}
Expand All @@ -193,7 +195,8 @@ func (f fallback) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"code": code,
"remoteAddress": r.RemoteAddr,
"requestUri": r.RequestURI,
"ContentLength": r.ContentLength}).WithError(err).Warnf("HTTP request failed%s", extra)
"ContentLength": r.ContentLength,
}).WithError(err).Warnf("HTTP request failed%s", extra)
if f.hasAuth {
code = 401
err = fmt.Errorf("Invalid credentials")
Expand All @@ -207,7 +210,7 @@ func loadClientCertificateCAs(paths []string) (*x509.CertPool, error) {
httpLog.Debugf("Loading Client Certificates from %d file(s)", len(paths))
pool := x509.NewCertPool()
for _, path := range paths {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read certificate file: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions sender/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package sender_test

import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"

Expand Down Expand Up @@ -82,7 +82,7 @@ func TestSnmpSenderTest(t *testing.T) {
}

func loadJsonFile(t *testing.T, file string) (*skogul.Container, []byte) {
b, _ := ioutil.ReadFile(file)
b, _ := os.ReadFile(file)

container, _ := parser.SkogulJSON{}.Parse(b)

Expand Down
12 changes: 5 additions & 7 deletions transformer/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,23 @@ package transformer_test

import (
"encoding/json"
"github.com/telenornms/skogul"
"github.com/telenornms/skogul/transformer"
"io/ioutil"
"os"
"testing"
"time"

"github.com/telenornms/skogul"
"github.com/telenornms/skogul/transformer"
)

func TestTimestampParse(t *testing.T) {
b, err := ioutil.ReadFile("./testdata/data-with-timestamp.json")

b, err := os.ReadFile("./testdata/data-with-timestamp.json")
if err != nil {
t.Errorf("Could not read json data file: %v", err)
return
}

var jsonData map[string]interface{}
err = json.Unmarshal(b, &jsonData)

if err != nil {
t.Errorf("Could not parse JSON data: %v", err)
return
Expand All @@ -56,7 +55,6 @@ func TestTimestampParse(t *testing.T) {
}

jsonTimestamp, err := time.Parse(time.RFC3339, jTimestamp)

if err != nil {
t.Errorf("Failed to parse original timestamp from JSON file: %v (%s)", err, jTimestamp)
return
Expand Down

0 comments on commit 77d9ba7

Please sign in to comment.