art: Move from io/ioutil to io and os packages (#470)

refactor: move from io/ioutil to io and os packages

The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

[1]: https://golang.org/doc/go1.16#ioutil
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-11-22 01:11:03 +08:00
committed by Itxaka
parent ad8fc74813
commit c080dc6220
8 changed files with 23 additions and 28 deletions

View File

@@ -2,7 +2,7 @@ package config
import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
@@ -141,7 +141,7 @@ func Scan(opts ...Option) (c *Config, err error) {
//fmt.Println("warning: Skipping file ", f, "as exceeds 1 MB in size")
continue
}
b, err := ioutil.ReadFile(f)
b, err := os.ReadFile(f)
if err == nil {
// best effort. skip lint checks
yaml.Unmarshal(b, c) //nolint:errcheck
@@ -164,7 +164,7 @@ func Scan(opts ...Option) (c *Config, err error) {
// use last recorded if no config is found valid
if !configFound && lastYamlFileFound != "" {
b, err := ioutil.ReadFile(lastYamlFileFound)
b, err := os.ReadFile(lastYamlFileFound)
if err == nil {
yaml.Unmarshal(b, c) //nolint:errcheck
c.location = lastYamlFileFound
@@ -196,7 +196,7 @@ func Scan(opts ...Option) (c *Config, err error) {
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
body, err = io.ReadAll(resp.Body)
if err != nil {
return err
}
@@ -266,7 +266,7 @@ func SaveCloudConfig(name Stage, yc yip.YipConfig) error {
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join("usr", "local", "cloud-config", fmt.Sprintf("100_%s.yaml", name)), dnsYAML, 0700)
return os.WriteFile(filepath.Join("usr", "local", "cloud-config", fmt.Sprintf("100_%s.yaml", name)), dnsYAML, 0700)
}
func FromString(s string, o interface{}) error {

View File

@@ -16,7 +16,6 @@
package config_test
import (
"io/ioutil"
"os"
"path/filepath"
@@ -40,12 +39,12 @@ baz: bar
kairos:
network_token: foo
`
d, _ := ioutil.TempDir("", "xxxx")
d, _ := os.MkdirTemp("", "xxxx")
defer os.RemoveAll(d)
err := ioutil.WriteFile(filepath.Join(d, "test"), []byte(cc), os.ModePerm)
err := os.WriteFile(filepath.Join(d, "test"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(d, "b"), []byte(`
err = os.WriteFile(filepath.Join(d, "b"), []byte(`
fooz:
`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
@@ -70,12 +69,12 @@ kairos:
bb:
nothing: "foo"
`
d, _ := ioutil.TempDir("", "xxxx")
d, _ := os.MkdirTemp("", "xxxx")
defer os.RemoveAll(d)
err := ioutil.WriteFile(filepath.Join(d, "test"), []byte(cc), os.ModePerm)
err := os.WriteFile(filepath.Join(d, "test"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(d, "b"), []byte(`zz.foo="baa" options.foo=bar`), os.ModePerm)
err = os.WriteFile(filepath.Join(d, "b"), []byte(`zz.foo="baa" options.foo=bar`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
c, err := Scan(Directories(d), MergeBootLine, WithBootCMDLineFile(filepath.Join(d, "b")))
@@ -96,10 +95,10 @@ bb:
var cc string = `
config_url: "https://gist.githubusercontent.com/mudler/ab26e8dd65c69c32ab292685741ca09c/raw/bafae390eae4e6382fb1b68293568696823b3103/test.yaml"
`
d, _ := ioutil.TempDir("", "xxxx")
d, _ := os.MkdirTemp("", "xxxx")
defer os.RemoveAll(d)
err := ioutil.WriteFile(filepath.Join(d, "test"), []byte(cc), os.ModePerm)
err := os.WriteFile(filepath.Join(d, "test"), []byte(cc), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
c, err := Scan(Directories(d))

View File

@@ -1,7 +1,7 @@
package machine
import (
"io/ioutil"
"os"
"strings"
"github.com/google/shlex"
@@ -12,7 +12,7 @@ func DotToYAML(file string) ([]byte, error) {
if file == "" {
file = "/proc/cmdline"
}
dat, err := ioutil.ReadFile(file)
dat, err := os.ReadFile(file)
if err != nil {
return []byte{}, err
}

View File

@@ -1,7 +1,6 @@
package machine_test
import (
"io/ioutil"
"os"
. "github.com/kairos-io/kairos/pkg/machine"
@@ -13,11 +12,11 @@ var _ = Describe("BootCMDLine", func() {
Context("parses data", func() {
It("returns cmdline if provided", func() {
f, err := ioutil.TempFile("", "test")
f, err := os.CreateTemp("", "test")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(f.Name())
err = ioutil.WriteFile(f.Name(), []byte(`config_url="foo bar" baz.bar=""`), os.ModePerm)
err = os.WriteFile(f.Name(), []byte(`config_url="foo bar" baz.bar=""`), os.ModePerm)
Expect(err).ToNot(HaveOccurred())
b, err := DotToYAML(f.Name())

View File

@@ -2,7 +2,6 @@ package machine
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
@@ -118,7 +117,7 @@ func UUID() string {
}
func CreateSentinel(f string) error {
return ioutil.WriteFile(fmt.Sprintf("/usr/local/.kairos/sentinel_%s", f), []byte{}, os.ModePerm)
return os.WriteFile(fmt.Sprintf("/usr/local/.kairos/sentinel_%s", f), []byte{}, os.ModePerm)
}
func SentinelExist(f string) bool {

View File

@@ -2,7 +2,7 @@ package openrc
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -51,7 +51,7 @@ func NewService(opts ...ServiceOpts) (ServiceUnit, error) {
func (s ServiceUnit) WriteUnit() error {
uname := s.name
if err := ioutil.WriteFile(filepath.Join(s.rootdir, fmt.Sprintf("/etc/init.d/%s", uname)), []byte(s.content), 0755); err != nil {
if err := os.WriteFile(filepath.Join(s.rootdir, fmt.Sprintf("/etc/init.d/%s", uname)), []byte(s.content), 0755); err != nil {
return err
}

View File

@@ -2,7 +2,6 @@ package systemd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -68,7 +67,7 @@ func (s ServiceUnit) WriteUnit() error {
uname = fmt.Sprintf("%s@", s.name)
}
if err := ioutil.WriteFile(filepath.Join(s.rootdir, fmt.Sprintf("/etc/systemd/system/%s.service", uname)), []byte(s.content), 0600); err != nil {
if err := os.WriteFile(filepath.Join(s.rootdir, fmt.Sprintf("/etc/systemd/system/%s.service", uname)), []byte(s.content), 0600); err != nil {
return err
}
@@ -80,7 +79,7 @@ func (s ServiceUnit) OverrideCmd(cmd string) error {
svcDir := filepath.Join(s.rootdir, fmt.Sprintf("/etc/systemd/system/%s.service.d/", s.name))
os.MkdirAll(svcDir, 0600) //nolint:errcheck
return ioutil.WriteFile(filepath.Join(svcDir, "override.conf"), []byte(fmt.Sprintf(overrideCmdTemplate, cmd)), 0600)
return os.WriteFile(filepath.Join(svcDir, "override.conf"), []byte(fmt.Sprintf(overrideCmdTemplate, cmd)), 0600)
}
func (s ServiceUnit) Start() error {

View File

@@ -2,7 +2,6 @@ package utils
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
@@ -17,7 +16,7 @@ func SH(c string) (string, error) {
}
func WriteEnv(envFile string, config map[string]string) error {
content, _ := ioutil.ReadFile(envFile)
content, _ := os.ReadFile(envFile)
env, _ := godotenv.Unmarshal(string(content))
for key, val := range config {