Skip to content

Commit

Permalink
Update deprecated use of ioutil (#271)
Browse files Browse the repository at this point in the history
> Deprecated: 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.
https://pkg.go.dev/io/ioutil
  • Loading branch information
abtreece authored Oct 11, 2024
1 parent 182e714 commit 3edafc4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 43 deletions.
3 changes: 1 addition & 2 deletions cmd/confd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -85,7 +84,7 @@ func initConfig() error {
log.Debug("Skipping confd config file.")
} else {
log.Debug("Loading " + config.ConfigFile)
configBytes, err := ioutil.ReadFile(config.ConfigFile)
configBytes, err := os.ReadFile(config.ConfigFile)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/backends/etcd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"os"
"strings"
"time"

Expand Down Expand Up @@ -124,7 +124,7 @@ func NewEtcdClient(machines []string, cert, key, caCert string, clientInsecure b
}

if caCert != "" {
certBytes, err := ioutil.ReadFile(caCert)
certBytes, err := os.ReadFile(caCert)
if err != nil {
return &Client{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/backends/file/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package file
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -33,7 +33,7 @@ func NewFileClient(filepath []string, filter string) (*Client, error) {
}

func readFile(path string, vars map[string]string) error {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -174,7 +173,7 @@ func (t *TemplateResource) createStageFile() error {
}

// create TempFile in Dest directory to avoid cross-filesystem issues
temp, err := ioutil.TempFile(filepath.Dir(t.Dest), "."+filepath.Base(t.Dest))
temp, err := os.CreateTemp(filepath.Dir(t.Dest), "."+filepath.Base(t.Dest))
if err != nil {
return err
}
Expand Down Expand Up @@ -231,11 +230,11 @@ func (t *TemplateResource) sync() error {
// try to open the file and write to it
var contents []byte
var rerr error
contents, rerr = ioutil.ReadFile(staged)
contents, rerr = os.ReadFile(staged)
if rerr != nil {
return rerr
}
err := ioutil.WriteFile(t.Dest, contents, t.FileMode)
err := os.WriteFile(t.Dest, contents, t.FileMode)
// make sure owner and group match the temp file, in case the file was created with WriteFile
os.Chown(t.Dest, t.Uid, t.Gid)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions pkg/template/resource_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package template

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -16,7 +15,7 @@ import (
// confd confDir.
// It returns an error if any.
func createTempDirs() (string, error) {
confDir, err := ioutil.TempDir("", "")
confDir, err := os.MkdirTemp("", "")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -51,13 +50,13 @@ func TestProcessTemplateResources(t *testing.T) {

// Create the src template.
srcTemplateFile := filepath.Join(tempConfDir, "templates", "foo.tmpl")
err = ioutil.WriteFile(srcTemplateFile, []byte(`foo = {{getv "/foo"}}`), 0644)
err = os.WriteFile(srcTemplateFile, []byte(`foo = {{getv "/foo"}}`), 0644)
if err != nil {
t.Error(err.Error())
}

// Create the dest.
destFile, err := ioutil.TempFile("", "")
destFile, err := os.CreateTemp("", "")
if err != nil {
t.Errorf("Failed to create destFile: %s", err.Error())
}
Expand Down Expand Up @@ -99,7 +98,7 @@ func TestProcessTemplateResources(t *testing.T) {
}
// Verify the results.
expected := "foo = bar"
results, err := ioutil.ReadFile(destFile.Name())
results, err := os.ReadFile(destFile.Name())
if err != nil {
t.Error(err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package template

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

Expand Down Expand Up @@ -569,7 +568,7 @@ func ExecuteTestTemplate(tt templateTest, t *testing.T) {
t.Errorf(tt.desc + ": failed createStageFile: " + err.Error())
}

actual, err := ioutil.ReadFile(tr.StageFile.Name())
actual, err := os.ReadFile(tr.StageFile.Name())
if err != nil {
t.Errorf(tt.desc + ": failed to read StageFile: " + err.Error())
}
Expand All @@ -595,14 +594,14 @@ func setupDirectoriesAndFiles(tt templateTest, t *testing.T) {
if err := os.MkdirAll("./test/confd", os.ModePerm); err != nil {
t.Errorf(tt.desc + ": failed to created confd directory: " + err.Error())
}
if err := ioutil.WriteFile(tomlFilePath, []byte(tt.toml), os.ModePerm); err != nil {
if err := os.WriteFile(tomlFilePath, []byte(tt.toml), os.ModePerm); err != nil {
t.Errorf(tt.desc + ": failed to write toml file: " + err.Error())
}
// create templates directory and tmpl file
if err := os.MkdirAll("./test/templates", os.ModePerm); err != nil {
t.Errorf(tt.desc + ": failed to create template directory: " + err.Error())
}
if err := ioutil.WriteFile(tmplFilePath, []byte(tt.tmpl), os.ModePerm); err != nil {
if err := os.WriteFile(tmplFilePath, []byte(tt.tmpl), os.ModePerm); err != nil {
t.Errorf(tt.desc + ": failed to write toml file: " + err.Error())
}
// create tmp directory for output
Expand Down
44 changes: 22 additions & 22 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package util

import (
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand All @@ -16,26 +15,27 @@ import (
// │   ├── sym1.toml
// │   └── sym2.toml
// └── root
// ├── root.other1
// ├── root.toml
// ├── subDir1
// │   ├── sub1.other
// │   ├── sub1.toml
// │   └── sub12.toml
// ├── subDir2
// │   ├── sub2.other
// │   ├── sub2.toml
// │   ├── sub22.toml
// │   └── subSubDir
// │   ├── subsub.other
// │   ├── subsub.toml
// │   ├── subsub2.toml
// │   └── sym2.toml -> ../../../other/sym2.toml
// └── sym1.toml -> ../other/sym1.toml
//
// ├── root.other1
// ├── root.toml
// ├── subDir1
// │   ├── sub1.other
// │   ├── sub1.toml
// │   └── sub12.toml
// ├── subDir2
// │   ├── sub2.other
// │   ├── sub2.toml
// │   ├── sub22.toml
// │   └── subSubDir
// │   ├── subsub.other
// │   ├── subsub.toml
// │   ├── subsub2.toml
// │   └── sym2.toml -> ../../../other/sym2.toml
// └── sym1.toml -> ../other/sym1.toml
func createDirStructure() (string, error) {
mod := os.FileMode(0755)
flag := os.O_RDWR | os.O_CREATE | os.O_EXCL
tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestRecursiveFilesLookup(t *testing.T) {

func TestIsConfigChangedTrue(t *testing.T) {
log.SetLevel("warn")
src, err := ioutil.TempFile("", "src")
src, err := os.CreateTemp("", "src")
defer os.Remove(src.Name())
if err != nil {
t.Errorf(err.Error())
Expand All @@ -187,7 +187,7 @@ func TestIsConfigChangedTrue(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
dest, err := ioutil.TempFile("", "dest")
dest, err := os.CreateTemp("", "dest")
defer os.Remove(dest.Name())
if err != nil {
t.Errorf(err.Error())
Expand All @@ -207,7 +207,7 @@ func TestIsConfigChangedTrue(t *testing.T) {

func TestIsConfigChangedFalse(t *testing.T) {
log.SetLevel("warn")
src, err := ioutil.TempFile("", "src")
src, err := os.CreateTemp("", "src")
defer os.Remove(src.Name())
if err != nil {
t.Errorf(err.Error())
Expand All @@ -216,7 +216,7 @@ func TestIsConfigChangedFalse(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
dest, err := ioutil.TempFile("", "dest")
dest, err := os.CreateTemp("", "dest")
defer os.Remove(dest.Name())
if err != nil {
t.Errorf(err.Error())
Expand Down
3 changes: 1 addition & 2 deletions test/integration/zookeeper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -55,7 +54,7 @@ func parsejson(prefix string, x interface{}, c *zk.Conn) {

func main() {
var pj interface{}
dat, err := ioutil.ReadFile("test.json")
dat, err := os.ReadFile("test.json")
check(err)
err = json.Unmarshal(dat, &pj)
check(err)
Expand Down

0 comments on commit 3edafc4

Please sign in to comment.