Skip to content

Commit

Permalink
io/ioutil is deprecated
Browse files Browse the repository at this point in the history
As of Go 1.16, the same functionality is now provided by package io
or package os, and those implementations should be preferred in new
code.

Since we are bombarded with Yetus complains about ioutil in all our
PRs, it is worth getting rid of it.

Signed-off-by: Milan Lenco <milan@zededa.com>
  • Loading branch information
milan-zededa committed Feb 3, 2023
1 parent 378812c commit 8980521
Show file tree
Hide file tree
Showing 94 changed files with 367 additions and 411 deletions.
15 changes: 7 additions & 8 deletions pkg/edgeview/src/basics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -201,7 +200,7 @@ func getAddrFromJWT(token string, isServer bool, instID int) (string, string, er

var uuidStr string
if isServer {
retbytes, err := ioutil.ReadFile("/persist/status/uuid")
retbytes, err := os.ReadFile("/persist/status/uuid")
if err == nil {
uuidStr = strings.TrimSuffix(string(retbytes), "\n")
}
Expand Down Expand Up @@ -322,7 +321,7 @@ func getBasics() {
}

if basics.uuid == "" {
retbytes, err := ioutil.ReadFile("/persist/status/uuid")
retbytes, err := os.ReadFile("/persist/status/uuid")
if err == nil {
basics.uuid = string(retbytes)
}
Expand All @@ -335,7 +334,7 @@ func getBasics() {
}

if basics.server == "" {
retbytes, err := ioutil.ReadFile("/config/server")
retbytes, err := os.ReadFile("/config/server")
if err == nil {
server := string(retbytes)
basics.server = strings.TrimSuffix(server, "\n")
Expand All @@ -361,14 +360,14 @@ func getBasics() {
}

if basics.release == "" {
retbytes, err := ioutil.ReadFile("/run/eve-release")
retbytes, err := os.ReadFile("/run/eve-release")
if err == nil {
basics.release = string(retbytes)
}
}

if basics.partition == "" {
retbytes, err := ioutil.ReadFile("/run/eve.id")
retbytes, err := os.ReadFile("/run/eve.id")
if err == nil {
basics.partition = string(retbytes)
}
Expand Down Expand Up @@ -459,7 +458,7 @@ func runPipeCmds(prog1 string, arg1 []string, prog2 string, arg2 []string) (stri
c1.Wait()
}()
c2.Wait()
out, err := ioutil.ReadAll(&b2)
out, err := io.ReadAll(&b2)
if err != nil {
reOpenPipe(true)
return "", err
Expand Down Expand Up @@ -517,7 +516,7 @@ func getFileTimeStr(t1 time.Time) string {
}

func listJSONFiles(path string) ([]string, error) {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/edgeview/src/copyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -323,13 +322,15 @@ func untarLogfile(downloadedFile string) {
}
logSaveDir := fileCopyDir + fileStr[0]
fmt.Printf("\n log files saved at %s\n\n", logSaveDir)
files, err := ioutil.ReadDir(logSaveDir)
files, err := os.ReadDir(logSaveDir)
if err != nil {
fmt.Printf("read %s error, %v\n", logSaveDir, err)
return
}
for _, f := range files {
fmt.Printf("file: %s, size %d\n", f.Name(), f.Size())
if info, err := f.Info(); err == nil {
fmt.Printf("file: %s, size %d\n", f.Name(), info.Size())
}
}

unpackLogfiles(fileCopyDir+fileStr[0], files)
Expand Down
17 changes: 10 additions & 7 deletions pkg/edgeview/src/log-search.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -110,7 +109,7 @@ func runLogSearch(cmds cmdOpt) {
func walkLogDirs(t1, t2, now int64) []logfiletime {
var getfiles []logfiletime

files, err := ioutil.ReadDir("/persist/newlog")
files, err := os.ReadDir("/persist/newlog")
if err != nil {
fmt.Printf("read /persist/newlog error %v\n", err)
return getfiles
Expand All @@ -130,13 +129,17 @@ func walkLogDirs(t1, t2, now int64) []logfiletime {
if strings.Contains(dir.Name(), "appUpload") && querytype == "dev" {
continue
}
files1, err := ioutil.ReadDir("/persist/newlog/" + dir.Name())
files1, err := os.ReadDir("/persist/newlog/" + dir.Name())
if err != nil {
continue
}
var groupfiles []string
for _, f := range files1 {
if f.ModTime().Unix() > t1 || f.ModTime().Unix() < t2 {
info, err := f.Info()
if err != nil {
continue
}
if info.ModTime().Unix() > t1 || info.ModTime().Unix() < t2 {
continue
}
groupfiles = append(groupfiles, f.Name())
Expand Down Expand Up @@ -178,7 +181,7 @@ func walkLogDirs(t1, t2, now int64) []logfiletime {
}

func searchLiveLogs(pattern string, now int64, typeStr string, idx *int, logjson bool) {
files, err := ioutil.ReadDir("/persist/newlog/collect")
files, err := os.ReadDir("/persist/newlog/collect")
if err != nil {
fmt.Printf("read /persist/newlog/collect error %v\n", err)
return
Expand All @@ -194,7 +197,7 @@ func searchLiveLogs(pattern string, now int64, typeStr string, idx *int, logjson
}

func searchCurrentLogs(pattern, path, typeStr string, now int64, idx *int, logjson bool) {
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
fmt.Printf("read %s file error: %v\n", path, err)
}
Expand Down Expand Up @@ -301,7 +304,7 @@ func getTimeSec(timeline string, now int64) (int64, int64) {

// uncompress the gzip log files and pack them into a single
// json text file for device and each app logs
func unpackLogfiles(path string, files []os.FileInfo) {
func unpackLogfiles(path string, files []os.DirEntry) {
sfnames := make(map[string][]string)
for _, f := range files {
if strings.HasPrefix(f.Name(), "dev.log.") {
Expand Down
4 changes: 2 additions & 2 deletions pkg/edgeview/src/multiinst.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"

"github.com/lf-edge/eve/pkg/pillar/pubsub"
Expand Down Expand Up @@ -68,7 +68,7 @@ func evStatsHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
var localStats evLocalStats
content, err := ioutil.ReadAll(r.Body)
content, err := io.ReadAll(r.Body)
if err != nil {
log.Errorf("stats server read error: %v", err)
return
Expand Down
40 changes: 20 additions & 20 deletions pkg/edgeview/src/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -78,7 +78,7 @@ func runNetwork(netw string) {
intfStat = getAllIntfs()
}
if opt == "ping" || opt == "trace" {
retbytes, err := ioutil.ReadFile("/config/server")
retbytes, err := os.ReadFile("/config/server")
if err != nil {
continue
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func doAppNet(status, appstr string, isSummary bool) string {

for _, item := range appStatus.UnderlayNetworkList {
niUUID := item.Network.String()
retbytes, err := ioutil.ReadFile("/run/zedrouter/NetworkInstanceStatus/" + niUUID + ".json")
retbytes, err := os.ReadFile("/run/zedrouter/NetworkInstanceStatus/" + niUUID + ".json")
if err != nil {
continue
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func doAppNet(status, appstr string, isSummary bool) string {
if !strings.HasPrefix(l, "dhcp-hosts.") {
continue
}
retbytes, err := ioutil.ReadFile(l)
retbytes, err := os.ReadFile(l)
if err == nil {
if strings.Contains(string(retbytes), item.Mac) {
fmt.Printf("%s\n", l)
Expand All @@ -208,7 +208,7 @@ func doAppNet(status, appstr string, isSummary bool) string {
}
}

retbytes, err := ioutil.ReadFile("/run/zedrouter/dnsmasq.leases/" + item.Bridge)
retbytes, err := os.ReadFile("/run/zedrouter/dnsmasq.leases/" + item.Bridge)
if err == nil {
printColor("\n - dnsmasq lease files\n", colorGREEN)
lines := strings.Split(string(retbytes), "\n")
Expand Down Expand Up @@ -251,7 +251,7 @@ func doAppNet(status, appstr string, isSummary bool) string {
}

appUUIDStr := appStatus.UUIDandVersion.UUID.String()
retbytes, err := ioutil.ReadFile("/run/domainmgr/DomainStatus/" + appUUIDStr + ".json")
retbytes, err := os.ReadFile("/run/domainmgr/DomainStatus/" + appUUIDStr + ".json")
if err == nil {
printColor("\n - domain status:", colorGREEN)
var domainS types.DomainStatus
Expand Down Expand Up @@ -297,7 +297,7 @@ func getAppNetTable(ipaddr string, niStatus *types.NetworkInstanceStatus) {

// getVifStats - in 'doAppNet'
func getVifStats(vifStr string) {
retbytes, err := ioutil.ReadFile("/run/zedrouter/NetworkMetrics/global.json")
retbytes, err := os.ReadFile("/run/zedrouter/NetworkMetrics/global.json")
if err != nil {
return
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func getPortCfg(opt string, isPrint bool) []string {
if isPrint {
fmt.Printf("\n - device port configure:\n")
}
outbytes, err := ioutil.ReadFile("/run/zedagent/DevicePortConfig/zedagent.json")
outbytes, err := os.ReadFile("/run/zedagent/DevicePortConfig/zedagent.json")
if err != nil {
return mgmtIntf
}
Expand Down Expand Up @@ -447,15 +447,15 @@ func getAllAppIPs() []appIPvnc {

var allAppIPs []appIPvnc
for _, s := range jfiles {
retbytes1, err := ioutil.ReadFile(s)
retbytes1, err := os.ReadFile(s)
if err != nil {
continue
}
status := strings.TrimSuffix(string(retbytes1), "\n")
appIPs, appUUID := getAppIPs(status)
var oneAppIPs []appIPvnc
if len(appIPs) > 0 {
retbytes1, err := ioutil.ReadFile("/run/zedagent/AppInstanceConfig/" + appUUID.String() + ".json")
retbytes1, err := os.ReadFile("/run/zedagent/AppInstanceConfig/" + appUUID.String() + ".json")
if err != nil {
log.Errorf("getAllAppIPs: run appinstcfg %v", err)
continue
Expand Down Expand Up @@ -502,7 +502,7 @@ func getConnectivity() {
fmt.Printf(" override.json:\n")
}
for _, f := range jfiles {
retbytes1, err := ioutil.ReadFile(f)
retbytes1, err := os.ReadFile(f)
if err != nil {
fmt.Printf("error: %v\n", err)
} else {
Expand All @@ -511,7 +511,7 @@ func getConnectivity() {
}
}

retbytes, err := ioutil.ReadFile("/persist/status/nim/DevicePortConfigList/global.json")
retbytes, err := os.ReadFile("/persist/status/nim/DevicePortConfigList/global.json")
if err != nil {
return
}
Expand Down Expand Up @@ -651,7 +651,7 @@ func getTables(rules string) []string {
}

func getMetricsMap(path string, stats *urlStats, isPrint bool) {
retbytes, err := ioutil.ReadFile(path + "global.json")
retbytes, err := os.ReadFile(path + "global.json")
if err != nil {
return
}
Expand Down Expand Up @@ -745,7 +745,7 @@ func showAppDetail(substring string) {
return
}
for _, s := range jfiles {
retbytes1, err := ioutil.ReadFile(s)
retbytes1, err := os.ReadFile(s)
if err != nil {
continue
}
Expand Down Expand Up @@ -834,7 +834,7 @@ func getProxy(needPrint bool) (string, int, [][]byte) {
proxyIP := ""
proxyPort := 0
proxyPEM := [][]byte{}
retbytes, err := ioutil.ReadFile("/run/zedagent/DevicePortConfig/zedagent.json")
retbytes, err := os.ReadFile("/run/zedagent/DevicePortConfig/zedagent.json")
if err != nil {
return proxyIP, proxyPort, proxyPEM
}
Expand Down Expand Up @@ -886,7 +886,7 @@ func httpsclient(server string, ipaddr net.IP) {
}
defer resp.Body.Close()

htmlData, err := ioutil.ReadAll(resp.Body)
htmlData, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("%v\n", err)
return
Expand Down Expand Up @@ -1169,7 +1169,7 @@ func runWireless() {
args := []string{"wlan0"}
_, _ = runCmd(prog, args, true)

retbytes, err := ioutil.ReadFile("/run/wlan/wpa_supplicant.conf")
retbytes, err := os.ReadFile("/run/wlan/wpa_supplicant.conf")
if err == nil {
printTitle(" wpa_supplicant.conf:", colorCYAN, false)
lines := strings.Split(string(retbytes), "\n")
Expand All @@ -1189,7 +1189,7 @@ func runWireless() {
}

printTitle("\n wwan config", colorCYAN, false)
retbytes, err = ioutil.ReadFile("/run/wwan/config.json")
retbytes, err = os.ReadFile("/run/wwan/config.json")
if err != nil {
return
}
Expand All @@ -1201,7 +1201,7 @@ func runWireless() {
fmt.Printf("%+v\n", wwancfg)

printTitle("\n wwan metrics", colorCYAN, false)
retbytes, err = ioutil.ReadFile("/run/wwan/metrics.json")
retbytes, err = os.ReadFile("/run/wwan/metrics.json")
if err == nil {
prettyJSON, err := formatJSON(retbytes)
if err == nil {
Expand All @@ -1210,7 +1210,7 @@ func runWireless() {
}

printTitle("\n wwan status", colorCYAN, false)
retbytes, err = ioutil.ReadFile("/run/wwan/status.json")
retbytes, err = os.ReadFile("/run/wwan/status.json")
if err == nil {
prettyJSON, err := formatJSON(retbytes)
if err == nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/edgeview/src/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand All @@ -34,7 +33,7 @@ var (
func initPolicy() error {
_, err := os.Stat(types.EdgeviewCfgFile)
if err == nil {
data, err := ioutil.ReadFile(types.EdgeviewCfgFile)
data, err := os.ReadFile(types.EdgeviewCfgFile)
if err != nil {
log.Errorf("can not read policy file: %v", err)
return err
Expand Down
Loading

0 comments on commit 8980521

Please sign in to comment.