Skip to content

Commit

Permalink
chore: Update deprecated io/ioutil to io or os packages
Browse files Browse the repository at this point in the history
  • Loading branch information
driskell committed Feb 10, 2023
1 parent ef11492 commit 4f28efb
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lc-lib/admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -177,7 +177,7 @@ func (c *Client) Call(path string, values url.Values) (string, error) {
}

func (c *Client) handleResponse(resp *http.Response) (string, error) {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
51 changes: 25 additions & 26 deletions lc-lib/config/generate/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -66,7 +65,7 @@ func main() {
platformFile += generateInit(config)

platformFileBytes := []byte(os.ExpandEnv(platformFile))
if err := ioutil.WriteFile(targetFile, platformFileBytes, 0644); err != nil {
if err := os.WriteFile(targetFile, platformFileBytes, 0644); err != nil {
log.Fatalf("Failed to write %s: %s", targetFile, err)
}
}
Expand Down Expand Up @@ -152,37 +151,37 @@ func generateInitSegment(pkg, name string) string {
//
// Examples
//
// "" => [""]
// "lowercase" => ["lowercase"]
// "Class" => ["Class"]
// "MyClass" => ["My", "Class"]
// "MyC" => ["My", "C"]
// "HTML" => ["HTML"]
// "PDFLoader" => ["PDF", "Loader"]
// "AString" => ["A", "String"]
// "SimpleXMLParser" => ["Simple", "XML", "Parser"]
// "vimRPCPlugin" => ["vim", "RPC", "Plugin"]
// "GL11Version" => ["GL", "11", "Version"]
// "99Bottles" => ["99", "Bottles"]
// "May5" => ["May", "5"]
// "BFG9000" => ["BFG", "9000"]
// "BöseÜberraschung" => ["Böse", "Überraschung"]
// "Two spaces" => ["Two", " ", "spaces"]
// "BadUTF8\xe2\xe2\xa1" => ["BadUTF8\xe2\xe2\xa1"]
// "" => [""]
// "lowercase" => ["lowercase"]
// "Class" => ["Class"]
// "MyClass" => ["My", "Class"]
// "MyC" => ["My", "C"]
// "HTML" => ["HTML"]
// "PDFLoader" => ["PDF", "Loader"]
// "AString" => ["A", "String"]
// "SimpleXMLParser" => ["Simple", "XML", "Parser"]
// "vimRPCPlugin" => ["vim", "RPC", "Plugin"]
// "GL11Version" => ["GL", "11", "Version"]
// "99Bottles" => ["99", "Bottles"]
// "May5" => ["May", "5"]
// "BFG9000" => ["BFG", "9000"]
// "BöseÜberraschung" => ["Böse", "Überraschung"]
// "Two spaces" => ["Two", " ", "spaces"]
// "BadUTF8\xe2\xe2\xa1" => ["BadUTF8\xe2\xe2\xa1"]
//
// Splitting rules
//
// 1) If string is not valid UTF-8, return it without splitting as
// 1. If string is not valid UTF-8, return it without splitting as
// single item array.
// 2) Assign all unicode characters into one of 4 sets: lower case
// 2. Assign all unicode characters into one of 4 sets: lower case
// letters, upper case letters, numbers, and all other characters.
// 3) Iterate through characters of string, introducing splits
// 3. Iterate through characters of string, introducing splits
// between adjacent characters that belong to different sets.
// 4) Iterate through array of split strings, and if a given string
// 4. Iterate through array of split strings, and if a given string
// is upper case:
// if subsequent string is lower case:
// move last character of upper case string to beginning of
// lower case string
// if subsequent string is lower case:
// move last character of upper case string to beginning of
// lower case string
func split(src string) (entries []string) {
// don't split invalid utf8
if !utf8.ValidString(src) {
Expand Down
4 changes: 2 additions & 2 deletions lc-lib/config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package config

import (
"io/ioutil"
"os"

"gopkg.in/yaml.v2"
)
Expand All @@ -27,7 +27,7 @@ func loadYAMLFile(path string, rawConfig interface{}) (err error) {
var data []byte

// Read the entire file
if data, err = ioutil.ReadFile(path); err != nil {
if data, err = os.ReadFile(path); err != nil {
return
}

Expand Down
4 changes: 2 additions & 2 deletions lc-lib/core/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package core

import (
"io/ioutil"
"io"
golog "log"
"os"

Expand Down Expand Up @@ -81,7 +81,7 @@ func (f *defaultLogBackend) Reopen() (err error) {

func (f *defaultLogBackend) Close() {
// Discard logs before closing
golog.SetOutput(ioutil.Discard)
golog.SetOutput(io.Discard)

if f.file != nil {
f.file.Close()
Expand Down
4 changes: 2 additions & 2 deletions lc-lib/transports/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/driskell/log-courier/lc-lib/config"
)
Expand Down Expand Up @@ -308,7 +308,7 @@ func GetTlsVersionAsString(version uint16) string {

// AddCertificates returns a new slice containing the given certificate list and the contents of the given file added
func AddCertificates(certificateList []*x509.Certificate, file string) ([]*x509.Certificate, error) {
pemdata, err := ioutil.ReadFile(file)
pemdata, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions lc-lib/transports/es/bulkrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"testing"
"time"

Expand Down Expand Up @@ -59,7 +59,7 @@ func TestRequestCreate(t *testing.T) {

func TestRequestReadFull(t *testing.T) {
request := createTestBulkRequest(3, "2020-03-07", "2020-03-07")
result, err := ioutil.ReadAll(request)
result, err := io.ReadAll(request)
if err != nil {
t.Errorf("Failed to encode: %s", err)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestRequestReadFull(t *testing.T) {

func TestRequestReadMultiple(t *testing.T) {
request := createTestBulkRequest(3, "2020-03-07", "2020-03-14")
result, err := ioutil.ReadAll(request)
result, err := io.ReadAll(request)
if err != nil {
t.Errorf("Failed to encode: %s", err)
}
Expand Down Expand Up @@ -115,18 +115,18 @@ func TestRequestReadMultiple(t *testing.T) {

func TestRequestReadReset(t *testing.T) {
request := createTestBulkRequest(3, "2020-03-07", "2020-03-14")
if _, err := ioutil.ReadAll(request); err != nil {
if _, err := io.ReadAll(request); err != nil {
t.Errorf("Failed to encode: %s", err)
}
result, err := ioutil.ReadAll(request)
result, err := io.ReadAll(request)
if err != nil {
t.Errorf("Failed to encode: %s", err)
}
if len(result) != 0 {
t.Errorf("Unexpected result: %s", string(result))
}
request.Reset()
result, err = ioutil.ReadAll(request)
result, err = io.ReadAll(request)
if err != nil {
t.Errorf("Failed to encode: %s", err)
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestRequestMark(t *testing.T) {
t.Errorf("Unexpected ack sequence: %d", request.AckSequence())
}
request.Reset()
result, err := ioutil.ReadAll(request)
result, err := io.ReadAll(request)
if err != nil {
t.Errorf("Failed to encode: %s", err)
}
Expand Down
15 changes: 7 additions & 8 deletions lc-lib/transports/es/transportes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -158,11 +157,11 @@ func (t *transportES) populateNodeInfo() error {
return err
}
defer func() {
bufio.NewReader(httpResponse.Body).WriteTo(ioutil.Discard)
bufio.NewReader(httpResponse.Body).WriteTo(io.Discard)
httpResponse.Body.Close()
}()
if httpResponse.StatusCode != 200 {
body, _ := ioutil.ReadAll(httpResponse.Body)
body, _ := io.ReadAll(httpResponse.Body)
return fmt.Errorf("unexpected status: %s [Body: %s]", httpResponse.Status, body)
}

Expand Down Expand Up @@ -239,11 +238,11 @@ func (t *transportES) installTemplate() error {
return err
}
defer func() {
bufio.NewReader(httpResponse.Body).WriteTo(ioutil.Discard)
bufio.NewReader(httpResponse.Body).WriteTo(io.Discard)
httpResponse.Body.Close()
}()
if httpResponse.StatusCode != 200 {
body, _ := ioutil.ReadAll(httpResponse.Body)
body, _ := io.ReadAll(httpResponse.Body)
return fmt.Errorf("unexpected status: %s [Body: %s]", httpResponse.Status, body)
}

Expand All @@ -264,14 +263,14 @@ func (t *transportES) checkTemplate(server *net.TCPAddr, name string) (bool, err
return false, err
}
defer func() {
bufio.NewReader(httpResponse.Body).WriteTo(ioutil.Discard)
bufio.NewReader(httpResponse.Body).WriteTo(io.Discard)
httpResponse.Body.Close()
}()
if httpResponse.StatusCode == 200 {
return true, nil
}
if httpResponse.StatusCode != 404 {
body, _ := ioutil.ReadAll(httpResponse.Body)
body, _ := io.ReadAll(httpResponse.Body)
return false, fmt.Errorf("unexpected status: %s [Body: %s]", httpResponse.Status, body)
}

Expand Down Expand Up @@ -381,7 +380,7 @@ func (t *transportES) performBulkRequest(id int, request *bulkRequest) error {
if err != nil {
return err
}
body, _ := ioutil.ReadAll(httpResponse.Body)
body, _ := io.ReadAll(httpResponse.Body)
httpResponse.Body.Close()
if httpResponse.StatusCode != 200 {
return fmt.Errorf("unexpected status: %s [Body: %s]", httpResponse.Status, body)
Expand Down
4 changes: 2 additions & 2 deletions lc-lib/transports/es/transportesfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -108,7 +108,7 @@ func (f *TransportESFactory) Validate(p *config.Parser, configPath string) (err
defer func() {
file.Close()
}()
f.template, err = ioutil.ReadAll(file)
f.template, err = io.ReadAll(file)
if err != nil {
return
}
Expand Down

0 comments on commit 4f28efb

Please sign in to comment.