Skip to content

Commit

Permalink
Remove ioutil.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Apr 5, 2024
1 parent 1c0e1fe commit 11790a9
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 29 deletions.
5 changes: 2 additions & 3 deletions cmd/mixtool/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -143,15 +142,15 @@ func generateAlerts(filename string, options mixer.GenerateOptions) error {
return err
}

return ioutil.WriteFile(options.AlertsFilename, out, 0644)
return os.WriteFile(options.AlertsFilename, out, 0644)
}

func generateRules(filename string, options mixer.GenerateOptions) error {
out, err := mixer.GenerateRules(filename, options)
if err != nil {
return err
}
return ioutil.WriteFile(options.RulesFilename, out, 0644)
return os.WriteFile(options.RulesFilename, out, 0644)
}

func generateDashboards(filename string, opts mixer.GenerateOptions) error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/mixtool/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -145,7 +145,7 @@ func putMixin(content []byte, bindAddress string) error {
if resp.StatusCode == 200 {
fmt.Println("PUT alerts OK")
} else {
responseData, err := ioutil.ReadAll(resp.Body)
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to response body in putMixin, %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/mixtool/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
Expand Down Expand Up @@ -48,7 +47,7 @@ func TestInstallMixin(t *testing.T) {
}

func testInstallMixin(t *testing.T, m mixin) {
tmpdir, err := ioutil.TempDir("", "mixtool-install")
tmpdir, err := os.CreateTemp("", "mixtool-install")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)

Check failure on line 52 in cmd/mixtool/install_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use tmpdir (variable of type *os.File) as string value in argument to os.RemoveAll

Check failure on line 52 in cmd/mixtool/install_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use tmpdir (variable of type *os.File) as string value in argument to os.RemoveAll

Expand Down
8 changes: 4 additions & 4 deletions cmd/mixtool/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -68,7 +68,7 @@ func queryWebsite(mixinsWebsite string) ([]byte, error) {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func listAction(c *cli.Context) error {
if err != nil {
return err
}
body, err = ioutil.ReadFile(path)
body, err = os.ReadFile(path)
if err != nil {
return err
}
Expand All @@ -149,6 +149,6 @@ func listAction(c *cli.Context) error {
if err != nil {
return err
}

return printMixins(mixins)
}
7 changes: 3 additions & 4 deletions cmd/mixtool/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

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

Expand Down Expand Up @@ -47,14 +46,14 @@ const exampleMixins = `
`

func TestList(t *testing.T) {
tempFile, err := ioutil.TempFile("", "exampleMixinsTest.json")
tempFile, err := os.CreateTemp("", "exampleMixinsTest.json")
assert.NoError(t, err)
defer os.Remove(tempFile.Name())

err = ioutil.WriteFile(tempFile.Name(), []byte(exampleMixins), 0644)
err = os.WriteFile(tempFile.Name(), []byte(exampleMixins), 0644)
assert.NoError(t, err)

body, err := ioutil.ReadFile(tempFile.Name())
body, err := os.ReadFile(tempFile.Name())
assert.NoError(t, err)

mixins, err := parseMixinJSON([]byte(body))
Expand Down
7 changes: 3 additions & 4 deletions cmd/mixtool/server.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"
"os"
"path/filepath"
Expand Down Expand Up @@ -99,12 +98,12 @@ type ruleProvisioner struct {
// to existing, does not provision them. It returns whether Prometheus should
// be reloaded and if an error has occurred.
func (p *ruleProvisioner) provision(r io.Reader) (bool, error) {
newData, err := ioutil.ReadAll(r)
newData, err := io.ReadAll(r)
if err != nil {
return false, fmt.Errorf("unable to read new rules: %w", err)
}

tempfile, err := ioutil.TempFile(filepath.Dir(p.ruleFile), "temp-mixtool")
tempfile, err := os.CreateTemp(filepath.Dir(p.ruleFile), "temp-mixtool")
if err != nil {
return false, fmt.Errorf("unable to create temp file: %w", err)
}
Expand Down Expand Up @@ -184,7 +183,7 @@ func (r *prometheusReloader) triggerReload(ctx context.Context) error {
return fmt.Errorf("reload request: %w", err)
}

if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
return fmt.Errorf("exhausting request body: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/jsonnetbundler/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package jsonnetbundler
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
Expand Down Expand Up @@ -47,7 +47,7 @@ func InitCommand(dir string) error {

filename := filepath.Join(dir, jsonnetfile.File)

err = ioutil.WriteFile(filename, contents, 0644)
err = os.WriteFile(filename, contents, 0644)
if err != nil {
return fmt.Errorf("Failed to write new jsonnetfile.json, %s", err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/jsonnetbundler/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package jsonnetbundler
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -35,7 +34,7 @@ func InstallCommand(dir, jsonnetHome string, uris []string, single bool) error {
dir = "."
}

jbfilebytes, err := ioutil.ReadFile(filepath.Join(dir, jsonnetfile.File))
jbfilebytes, err := os.ReadFile(filepath.Join(dir, jsonnetfile.File))
if err != nil {
return fmt.Errorf("failed to load jsonnetfile %s", err.Error())
}
Expand All @@ -45,7 +44,7 @@ func InstallCommand(dir, jsonnetHome string, uris []string, single bool) error {
return err
}

jblockfilebytes, err := ioutil.ReadFile(filepath.Join(dir, jsonnetfile.LockFile))
jblockfilebytes, err := os.ReadFile(filepath.Join(dir, jsonnetfile.LockFile))
if !os.IsNotExist(err) {
if err != nil {
return fmt.Errorf("failed to load lockfile %s", err.Error())
Expand Down Expand Up @@ -117,7 +116,7 @@ func writeJSONFile(name string, d interface{}) error {
}
b = append(b, []byte("\n")...)

return ioutil.WriteFile(name, b, 0644)
return os.WriteFile(name, b, 0644)
}

func writeChangedJsonnetFile(originalBytes []byte, modified *v1.JsonnetFile, path string) error {
Expand Down
3 changes: 1 addition & 2 deletions pkg/mixer/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package mixer

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

Expand Down Expand Up @@ -63,7 +62,7 @@ const expectedYaml = `groups:
`

func TestEvalAlerts(t *testing.T) {
inFile, err := ioutil.TempFile(os.TempDir(), "mixtool-")
inFile, err := os.CreateTemp(os.TempDir(), "mixtool-")
assert.NoError(t, err)
defer os.Remove(inFile.Name())

Expand Down
3 changes: 1 addition & 2 deletions pkg/mixer/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package mixer

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

Expand Down Expand Up @@ -288,7 +287,7 @@ func TestLintGrafana(t *testing.T) {
}

func writeTempFile(t *testing.T, pattern string, contents string) (filename string, delete func()) {
f, err := ioutil.TempFile("", pattern)
f, err := os.CreateTemp("", pattern)
if err != nil {
t.Errorf("failed to create temp file: %v", err)
}
Expand Down

0 comments on commit 11790a9

Please sign in to comment.