Skip to content

Commit

Permalink
chore: convert ioutil to io and os (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
wusphinx authored Sep 25, 2023
1 parent 286e281 commit 8bfce9b
Show file tree
Hide file tree
Showing 41 changed files with 104 additions and 120 deletions.
6 changes: 3 additions & 3 deletions external/github.com/elastic/beats/v7/libbeat/logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package logp

import (
"flag"
"io/ioutil"
"io"
golog "log"
"os"
"path/filepath"
Expand Down Expand Up @@ -100,7 +100,7 @@ func Configure(cfg Config) error {
if _, enabled := selectors["stdlog"]; !enabled {
// Disable standard logging by default (this is sometimes used by
// libraries and we don't want their spam).
golog.SetOutput(ioutil.Discard)
golog.SetOutput(io.Discard)
}

sink = selectiveWrapper(sink, selectors)
Expand Down Expand Up @@ -172,7 +172,7 @@ func makeStderrOutput(cfg Config) (zapcore.Core, error) {
}

func makeDiscardOutput(cfg Config) (zapcore.Core, error) {
discard := zapcore.AddSync(ioutil.Discard)
discard := zapcore.AddSync(io.Discard)
return zapcore.NewCore(buildEncoder(cfg), discard, cfg.Level.zapLevel()), nil
}

Expand Down
8 changes: 4 additions & 4 deletions helper/envconfig/aliyun_ecs_meta_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -76,7 +76,7 @@ func getToken() (result []byte, err error) {
}
defer respList.Body.Close()
var body []byte
body, err = ioutil.ReadAll(respList.Body)
body, err = io.ReadAll(respList.Body)
if err != nil {
logger.Warning(context.Background(), "UPDATE_STS_ALARM", "parse role list error", err)
return nil, err
Expand All @@ -96,7 +96,7 @@ func getToken() (result []byte, err error) {
return nil, err
}
defer respGet.Body.Close()
body, err = ioutil.ReadAll(respGet.Body)
body, err = io.ReadAll(respGet.Body)
if err != nil {
logger.Warning(context.Background(), "UPDATE_STS_ALARM", "parse token error", err, "role", role)
return nil, err
Expand Down Expand Up @@ -156,7 +156,7 @@ func getAKFromLocalFile() (accessKeyID, accessKeySecret, securityToken string, e
if err == nil {
var akInfo AKInfo
// 获取token config json
encodeTokenCfg, err := ioutil.ReadFile(filepath.Clean(addonTokenConfigPath))
encodeTokenCfg, err := os.ReadFile(filepath.Clean(addonTokenConfigPath))
if err != nil {
return accessKeyID, accessKeySecret, securityToken, expireTime, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/doc/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package doc
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -66,7 +65,7 @@ func Generate(path string) {
str += " - [" + name + "](" + relativeFile + ")" + lf
}
}
_ = ioutil.WriteFile(fileName, []byte(str), 0600)
_ = os.WriteFile(fileName, []byte(str), 0600)
}

func generatePluginDoc(fileName, pluginName string, doc Doc) {
Expand All @@ -81,7 +80,7 @@ func generatePluginDoc(fileName, pluginName string, doc Doc) {
str += lf + tableSplitor + config.Name + tableSplitor + config.Type + tableSplitor + config.Comment + tableSplitor + config.Default + tableSplitor
}
}
_ = ioutil.WriteFile(fileName, []byte(str), 0600)
_ = os.WriteFile(fileName, []byte(str), 0600)
}

func extractDocConfig(doc Doc) (configs []*FieldConfig) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/doc/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package doc

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

Expand Down Expand Up @@ -95,7 +94,7 @@ this is a test doc demo
"k": "v",
}
generatePluginDoc("test-plugin.md", "test-plugin", t2)
bytes, _ := ioutil.ReadFile("test-plugin.md")
bytes, _ := os.ReadFile("test-plugin.md")
assert.Equal(t, str, string(bytes))
_ = os.Remove("test-plugin.md")
}
5 changes: 2 additions & 3 deletions pkg/helper/docker_center_file_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -170,7 +169,7 @@ func scanContainerdFilesAndReLink(filePath string) {
}
for {
time.Sleep(time.Millisecond * time.Duration(containerdScanIntervalMs))
dir, err := ioutil.ReadDir(dirPath)
dir, err := os.ReadDir(dirPath)
if err != nil {
continue
}
Expand Down Expand Up @@ -204,7 +203,7 @@ func scanContainerdFilesAndReLink(filePath string) {
}

func innerReadStatisContainerInfo(file string, lastContainerInfo []types.ContainerJSON, stat fs.FileInfo) (containers []types.ContainerJSON, removed []string, changed bool, err error) {
body, err := ioutil.ReadFile(filepath.Clean(file))
body, err := os.ReadFile(filepath.Clean(file))
if err != nil {
return nil, nil, false, err
}
Expand Down
19 changes: 9 additions & 10 deletions pkg/helper/docker_center_file_discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package helper

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -698,7 +697,7 @@ var staticECIConfig2 = `[
func TestTryReadStaticContainerInfo(t *testing.T) {
defer os.Remove("./static_container.json")
defer os.Unsetenv(staticContainerInfoPathEnvKey)
ioutil.WriteFile("./static_container.json", []byte(staticDockerConfig), os.ModePerm)
os.WriteFile("./static_container.json", []byte(staticDockerConfig), os.ModePerm)
os.Setenv(staticContainerInfoPathEnvKey, "./static_container.json")
containerInfo, removedIDs, changed, err := tryReadStaticContainerInfo()
require.Nil(t, err)
Expand All @@ -724,7 +723,7 @@ func TestTryReadStaticContainerInfo(t *testing.T) {
}
require.Equal(t, 999999999908, info.State.Pid)

ioutil.WriteFile("./static_container.json", []byte(staticDockerConfig2), os.ModePerm)
os.WriteFile("./static_container.json", []byte(staticDockerConfig2), os.ModePerm)

containerInfo, removedIDs, changed, err = tryReadStaticContainerInfo()
require.Nil(t, err)
Expand All @@ -751,7 +750,7 @@ func TestLoadStaticContainerConfig(t *testing.T) {
resetDockerCenter()
defer os.Remove("./static_container.json")
defer os.Unsetenv(staticContainerInfoPathEnvKey)
ioutil.WriteFile("./static_container.json", []byte(staticDockerConfig), os.ModePerm)
os.WriteFile("./static_container.json", []byte(staticDockerConfig), os.ModePerm)
os.Setenv(staticContainerInfoPathEnvKey, "./static_container.json")
instance := getDockerCenterInstance()
allInfo := instance.containerMap
Expand All @@ -766,7 +765,7 @@ func TestLoadStaticContainerConfigTwice(t *testing.T) {
resetDockerCenter()
defer os.Remove("./static_container.json")
defer os.Unsetenv(staticContainerInfoPathEnvKey)
ioutil.WriteFile("./static_container.json", []byte(staticECIConfig), os.ModePerm)
os.WriteFile("./static_container.json", []byte(staticECIConfig), os.ModePerm)
os.Setenv(staticContainerInfoPathEnvKey, "./static_container.json")
instance := getDockerCenterInstance()
allInfo := instance.containerMap
Expand All @@ -776,7 +775,7 @@ func TestLoadStaticContainerConfigTwice(t *testing.T) {
}

os.Remove("./static_container.json")
ioutil.WriteFile("./static_container.json", []byte(staticECIConfig2), os.ModePerm)
os.WriteFile("./static_container.json", []byte(staticECIConfig2), os.ModePerm)

time.Sleep(time.Second * 10)

Expand Down Expand Up @@ -814,18 +813,18 @@ func TestScanContainerdFilesAndReLink(t *testing.T) {
go scanContainerdFilesAndReLink(logName)

time.Sleep(time.Second)
ioutil.WriteFile(filepath.Join(dir, "0.log"), []byte("abc"), os.ModePerm)
os.WriteFile(filepath.Join(dir, "0.log"), []byte("abc"), os.ModePerm)
checkSameDevInode(t, logName, filepath.Join(dir, "0.log"))

ioutil.WriteFile(filepath.Join(dir, "99.log"), []byte("abcdef"), os.ModePerm)
os.WriteFile(filepath.Join(dir, "99.log"), []byte("abcdef"), os.ModePerm)
time.Sleep(time.Second * 2)
checkSameDevInode(t, logName, filepath.Join(dir, "99.log"))

ioutil.WriteFile(filepath.Join(dir, "100.log"), []byte("abcde"), os.ModePerm)
os.WriteFile(filepath.Join(dir, "100.log"), []byte("abcde"), os.ModePerm)
time.Sleep(time.Second * 2)
checkSameDevInode(t, logName, filepath.Join(dir, "100.log"))

ioutil.WriteFile(filepath.Join(dir, "101.log"), []byte("abcdefg"), os.ModePerm)
os.WriteFile(filepath.Join(dir, "101.log"), []byte("abcdefg"), os.ModePerm)
time.Sleep(time.Second * 2)
checkSameDevInode(t, logName, filepath.Join(dir, "101.log"))
}
5 changes: 2 additions & 3 deletions pkg/helper/docker_center_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package helper

import (
"io/ioutil"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -55,7 +54,7 @@ func resetDockerCenter() {

func TestGetIpByHost_1(t *testing.T) {
hostFileName := "./tmp_TestGetIpByHost.txt"
ioutil.WriteFile(hostFileName, []byte(hostFileContent1), 0x777)
os.WriteFile(hostFileName, []byte(hostFileContent1), 0x777)
ip := getIPByHosts(hostFileName, "8be13ee0dd9e")
if ip != "192.168.5.3" {
t.Errorf("GetIpByHosts = %v, want %v", ip, "192.168.5.3")
Expand All @@ -65,7 +64,7 @@ func TestGetIpByHost_1(t *testing.T) {

func TestGetIpByHost_2(t *testing.T) {
hostFileName := "./tmp_TestGetIpByHost.txt"
ioutil.WriteFile(hostFileName, []byte(hostFileContent2), 0x777)
os.WriteFile(hostFileName, []byte(hostFileContent2), 0x777)
ip := getIPByHosts(hostFileName, "nginx-5fd7568b67-4sh8c")
if ip != "172.20.4.5" {
t.Errorf("GetIpByHosts = %v, want %v", ip, "172.20.4.5")
Expand Down
3 changes: 1 addition & 2 deletions pkg/helper/dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestServiceHTTP_doDumpFile(t *testing.T) {
}
}
readFunc := func(file string, expectLen int) {
data, rerr := ioutil.ReadFile(file)
data, rerr := os.ReadFile(file)
require.NoError(t, rerr)
offset := 0
num := 0
Expand Down
4 changes: 2 additions & 2 deletions pkg/helper/platformmeta/aliyun_ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package platformmeta
import (
"context"
"errors"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -50,7 +50,7 @@ func AlibabaCloudEcsPlatformRequest(api string, method string, f func(header *ht
if resp.StatusCode == 404 {
return "", error404
}
bytes, err := ioutil.ReadAll(resp.Body)
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/helper/profile/pyroscope/jfr/jfr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"compress/gzip"
"context"
"io"
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -106,7 +105,7 @@ func readGzipFile(fname string) ([]byte, error) {
return nil, err
}
defer r.Close()
return ioutil.ReadAll(r)
return io.ReadAll(r)
}

func readRawFile(fname string) ([]byte, error) {
Expand All @@ -115,5 +114,5 @@ func readRawFile(fname string) ([]byte, error) {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
return io.ReadAll(f)
}
5 changes: 2 additions & 3 deletions pkg/protocol/decoder/common/comon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"sync"
Expand Down Expand Up @@ -89,7 +88,7 @@ func CollectBody(res http.ResponseWriter, req *http.Request, maxBodySize int64)
return data, http.StatusOK, nil
}

bytes, err := ioutil.ReadAll(body)
bytes, err := io.ReadAll(body)
if err != nil {
return nil, http.StatusRequestEntityTooLarge, err
}
Expand All @@ -112,7 +111,7 @@ func CollectBody(res http.ResponseWriter, req *http.Request, maxBodySize int64)
func CollectRawBody(res http.ResponseWriter, req *http.Request, maxBodySize int64) ([]byte, int, error) {
body := req.Body
body = http.MaxBytesReader(res, body, maxBodySize)
bytes, err := ioutil.ReadAll(body)
bytes, err := io.ReadAll(body)
if err != nil {
return nil, http.StatusRequestEntityTooLarge, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/decoder/pyroscope/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"bytes"
"encoding/binary"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"sort"
"testing"

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

func TestDecoder_DecodePprofCumulative(t *testing.T) {
data, err := ioutil.ReadFile("test/dump_pprof_mem_data")
data, err := os.ReadFile("test/dump_pprof_mem_data")
require.NoError(t, err)
var length uint32
buffer := bytes.NewBuffer(data)
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"errors"
"fmt"
"hash/fnv"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -122,7 +121,7 @@ func GetTLSConfig(sslCert, sslKey, sslCA string, insecureSkipVerify bool) (*tls.
t := &tls.Config{InsecureSkipVerify: insecureSkipVerify} //nolint:gosec

if sslCA != "" {
caCert, err := ioutil.ReadFile(filepath.Clean(sslCA))
caCert, err := os.ReadFile(filepath.Clean(sslCA))
if err != nil {
return nil, fmt.Errorf("Could not load TLS CA: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions plugin_main/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"sync"

"github.com/alibaba/ilogtail/pkg/logger"
Expand Down Expand Up @@ -62,7 +62,7 @@ var (

// LoadConfig read the plugin content.
func LoadConfig() (globalCfg string, pluginCfgs []string, err error) {
if gCfg, errRead := ioutil.ReadFile(*GlobalConfig); errRead != nil {
if gCfg, errRead := os.ReadFile(*GlobalConfig); errRead != nil {
globalCfg = defaultGlobalConfig
} else {
globalCfg = string(gCfg)
Expand All @@ -74,7 +74,7 @@ func LoadConfig() (globalCfg string, pluginCfgs []string, err error) {
}

var pluginCfg string
if pCfg, errRead := ioutil.ReadFile(*PluginConfig); errRead == nil {
if pCfg, errRead := os.ReadFile(*PluginConfig); errRead == nil {
pluginCfg = string(pCfg)
} else {
pluginCfg = defaultPluginConfig
Expand Down Expand Up @@ -118,7 +118,7 @@ func GetFlusherConfiguration() (flusherCategory string, flusherOptions map[strin
}
return c, options, true
}
if fCfg, err := ioutil.ReadFile(*FlusherConfig); err == nil {
if fCfg, err := os.ReadFile(*FlusherConfig); err == nil {
category, options, ok := extract(fCfg)
if ok {
flusherType = category
Expand Down
Loading

0 comments on commit 8bfce9b

Please sign in to comment.