Skip to content

Commit

Permalink
🎨 Move from io/ioutil to io and os packages (kairos-io#470)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Juneezee authored Nov 21, 2022
1 parent 39a83ca commit 50d9143
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 62 deletions.
3 changes: 1 addition & 2 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package agent

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -46,7 +45,7 @@ func Run(opts ...Option) error {

// Create if not exist
if _, err := os.Stat(fileName); err != nil {
err = ioutil.WriteFile(fileName, []byte{}, os.ModePerm)
err = os.WriteFile(fileName, []byte{}, os.ModePerm)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions internal/agent/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package agent

import (
"io/ioutil"
"os"

"github.com/kairos-io/kairos/internal/kairos"

Expand Down Expand Up @@ -29,35 +29,35 @@ func LoadConfig(path ...string) (*Config, error) {
cfg := &Config{}

for _, p := range path {
f, err := ioutil.ReadFile(p)
f, err := os.ReadFile(p)
if err == nil {
yaml.Unmarshal(f, cfg) //nolint:errcheck
}
}

if cfg.Branding.InteractiveInstall == "" {
f, err := ioutil.ReadFile(kairos.BrandingFile("interactive_install_text"))
f, err := os.ReadFile(kairos.BrandingFile("interactive_install_text"))
if err == nil {
cfg.Branding.InteractiveInstall = string(f)
}
}

if cfg.Branding.Install == "" {
f, err := ioutil.ReadFile(kairos.BrandingFile("install_text"))
f, err := os.ReadFile(kairos.BrandingFile("install_text"))
if err == nil {
cfg.Branding.Install = string(f)
}
}

if cfg.Branding.Recovery == "" {
f, err := ioutil.ReadFile(kairos.BrandingFile("recovery_text"))
f, err := os.ReadFile(kairos.BrandingFile("recovery_text"))
if err == nil {
cfg.Branding.Recovery = string(f)
}
}

if cfg.Branding.Reset == "" {
f, err := ioutil.ReadFile(kairos.BrandingFile("reset_text"))
f, err := os.ReadFile(kairos.BrandingFile("reset_text"))
if err == nil {
cfg.Branding.Reset = string(f)
}
Expand Down
7 changes: 3 additions & 4 deletions internal/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"syscall"
Expand Down Expand Up @@ -40,7 +39,7 @@ func optsToArgs(options map[string]string) (res []string) {
}

func ManualInstall(config string, options map[string]string) error {
dat, err := ioutil.ReadFile(config)
dat, err := os.ReadFile(config)
if err != nil {
return err
}
Expand Down Expand Up @@ -195,7 +194,7 @@ func RunInstall(options map[string]string) error {
utils.SH("elemental run-stage kairos-install.pre") //nolint:errcheck
events.RunHookScript("/usr/bin/kairos-agent.install.pre.hook") //nolint:errcheck

f, _ := ioutil.TempFile("", "xxxx")
f, _ := os.CreateTemp("", "xxxx")
defer os.RemoveAll(f.Name())

device, ok := options["device"]
Expand Down Expand Up @@ -232,7 +231,7 @@ func RunInstall(options map[string]string) error {
env := append(c.Install.Env, c.Env...)
utils.SetEnv(env)

err := ioutil.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
err := os.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
if err != nil {
fmt.Printf("could not write cloud init: %s\n", err.Error())
os.Exit(1)
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

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

"github.com/kairos-io/kairos/internal/kairos"
Expand All @@ -19,7 +18,7 @@ func PrintText(f string, banner string) {
func PrintBranding(b []byte) {
brandingFile := kairos.BrandingFile("banner")
if _, err := os.Stat(brandingFile); err == nil {
f, err := ioutil.ReadFile(brandingFile)
f, err := os.ReadFile(brandingFile)
if err == nil {
fmt.Println(string(f))
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 8 additions & 9 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package config_test

import (
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -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())
Expand All @@ -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")))
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/bootcmdline.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package machine

import (
"io/ioutil"
"os"
"strings"

"github.com/google/shlex"
Expand All @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/machine/bootcmdline_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package machine_test

import (
"io/ioutil"
"os"

. "github.com/kairos-io/kairos/pkg/machine"
Expand All @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package machine

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/openrc/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package openrc

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/machine/systemd/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package systemd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/utils/sh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"bytes"
"io/ioutil"
"os"
"os/exec"

Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions sdk/bundles/bundle_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bundles_test

import (
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -13,7 +12,7 @@ import (
var _ = Describe("Bundle", func() {
Context("install", func() {
PIt("installs packages from luet repos", func() {
dir, err := ioutil.TempDir("", "test")
dir, err := os.MkdirTemp("", "test")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
os.MkdirAll(filepath.Join(dir, "var", "tmp", "luet"), os.ModePerm)
Expand All @@ -23,7 +22,7 @@ var _ = Describe("Bundle", func() {
})

It("installs from container images", func() {
dir, err := ioutil.TempDir("", "test")
dir, err := os.MkdirTemp("", "test")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
err = RunBundles([]BundleOption{WithDBPath(dir), WithRootFS(dir), WithTarget("container://quay.io/mocaccino/extra:edgevpn-utils-0.15.0")})
Expand Down
3 changes: 1 addition & 2 deletions sdk/bundles/bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bundles

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -143,7 +142,7 @@ type ContainerRunner struct{}

func (l *ContainerRunner) Install(config *BundleConfig) error {

tempDir, err := ioutil.TempDir("", "containerrunner")
tempDir, err := os.MkdirTemp("", "containerrunner")
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 50d9143

Please sign in to comment.