Skip to content

Commit 0fab0ae

Browse files
authored
Merge pull request #274 from roots/remove-ioutil-usage
Remove all ioutil usage
2 parents bac9d14 + 0bdf0a1 commit 0fab0ae

22 files changed

+35
-54
lines changed

cmd/ad_hoc_playbook.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
)
@@ -16,7 +15,7 @@ func (p *AdHocPlaybook) DumpFiles() func() {
1615
destination := filepath.Join(p.path, fileName)
1716
contentByte := []byte(content)
1817

19-
if err := ioutil.WriteFile(destination, contentByte, 0644); err != nil {
18+
if err := os.WriteFile(destination, contentByte, 0644); err != nil {
2019
panic("Could not write temporary file. This is probably a bug in trellis-cli; please open an issue to let us know.")
2120
}
2221
}

cmd/alias.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
_ "embed"
55
"flag"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"strings"
@@ -100,7 +99,7 @@ func (c *AliasCommand) Run(args []string) int {
10099
}
101100
}
102101

103-
tempDir, tempDirErr := ioutil.TempDir("", "trellis-alias-")
102+
tempDir, tempDirErr := os.MkdirTemp("", "trellis-alias-")
104103
if tempDirErr != nil {
105104
spinner.StopFail()
106105
c.UI.Error(tempDirErr.Error())
@@ -129,7 +128,7 @@ func (c *AliasCommand) Run(args []string) int {
129128

130129
combined := ""
131130
for _, environment := range remoteEnvironments {
132-
part, err := ioutil.ReadFile(filepath.Join(tempDir, environment+".yml.part"))
131+
part, err := os.ReadFile(filepath.Join(tempDir, environment+".yml.part"))
133132
if err != nil {
134133
spinner.StopFail()
135134
c.UI.Error(err.Error())
@@ -139,7 +138,7 @@ func (c *AliasCommand) Run(args []string) int {
139138
}
140139

141140
combinedYmlPath := filepath.Join(tempDir, "/combined.yml")
142-
writeFileErr := ioutil.WriteFile(combinedYmlPath, []byte(combined), 0644)
141+
writeFileErr := os.WriteFile(combinedYmlPath, []byte(combined), 0644)
143142
if writeFileErr != nil {
144143
spinner.StopFail()
145144
c.UI.Error(writeFileErr.Error())

cmd/alias_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"io/ioutil"
54
"os"
65
"os/exec"
76
"path"
@@ -91,10 +90,10 @@ func TestIntegrationAlias(t *testing.T) {
9190
t.Error("wp-cli.trellis-alias.yml file not generated")
9291
}
9392

94-
actualByte, _ := ioutil.ReadFile(actualPath)
93+
actualByte, _ := os.ReadFile(actualPath)
9594
actual := string(actualByte)
9695

97-
expectedByte, _ := ioutil.ReadFile("./testdata/expected/alias/wp-cli.trellis-alias.yml")
96+
expectedByte, _ := os.ReadFile("./testdata/expected/alias/wp-cli.trellis-alias.yml")
9897
expected := string(expectedByte)
9998

10099
if actual != expected {

cmd/db_open.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"flag"
77
"fmt"
8-
"io/ioutil"
98
"os"
109
"strings"
1110

@@ -105,7 +104,7 @@ func (c *DBOpenCommand) Run(args []string) int {
105104
}
106105

107106
// Prepare JSON file for db credentials
108-
dbCredentialsJson, dbCredentialsErr := ioutil.TempFile("", "*.json")
107+
dbCredentialsJson, dbCredentialsErr := os.CreateTemp("", "*.json")
109108
if dbCredentialsErr != nil {
110109
c.UI.Error(fmt.Sprintf("Error creating temporary db credentials JSON file: %s", dbCredentialsErr))
111110
}
@@ -128,7 +127,7 @@ func (c *DBOpenCommand) Run(args []string) int {
128127
}
129128

130129
// Read db credentials from JSON file.
131-
dbCredentialsByte, readErr := ioutil.ReadFile(dbCredentialsJson.Name())
130+
dbCredentialsByte, readErr := os.ReadFile(dbCredentialsJson.Name())
132131
if readErr != nil {
133132
c.UI.Error(fmt.Sprintf("Error reading db credentials JSON file: %s", readErr))
134133
return 1

cmd/db_opener_sequel_ace.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
_ "embed"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"text/template"
98
"time"
@@ -20,7 +19,7 @@ type DBOpenerSequelAce struct {
2019
var sequelAceSpfTemplate string
2120

2221
func (o *DBOpenerSequelAce) Open(c DBCredentials) (err error) {
23-
sequelAceSpf, sequelAceSpfErr := ioutil.TempFile("", "*.spf")
22+
sequelAceSpf, sequelAceSpfErr := os.CreateTemp("", "*.spf")
2423
if sequelAceSpfErr != nil {
2524
return fmt.Errorf("Error creating temporary SequelAce SPF file: %s", sequelAceSpfErr)
2625
}

cmd/dot_env_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"io/ioutil"
54
"os"
65
"os/exec"
76
"path/filepath"
@@ -141,10 +140,10 @@ func TestIntegrationDotEnv(t *testing.T) {
141140
t.Error(".env file not generated")
142141
}
143142

144-
actualByte, _ := ioutil.ReadFile(actualPath)
143+
actualByte, _ := os.ReadFile(actualPath)
145144
actual := string(actualByte)
146145

147-
expectedByte, _ := ioutil.ReadFile("./testdata/expected/dot_env/.env")
146+
expectedByte, _ := os.ReadFile("./testdata/expected/dot_env/.env")
148147
expected := string(expectedByte)
149148

150149
if actual != expected {

cmd/droplet_create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"flag"
77
"fmt"
8-
"io/ioutil"
98
"net"
109
"os"
1110
"strings"
@@ -309,7 +308,7 @@ func checkSSHKey(ui cli.Ui, keyString string, publicKey ssh.PublicKey) error {
309308

310309
func loadSSHKey(path string) (keyString string, publicKey ssh.PublicKey, err error) {
311310
path, err = homedir.Expand(path)
312-
key, err := ioutil.ReadFile(path)
311+
key, err := os.ReadFile(path)
313312
if err != nil {
314313
return "", nil, err
315314
}

cmd/key_generate_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"os/exec"
87
"path/filepath"
@@ -98,7 +97,7 @@ func TestKeyGenerateExistingPrivateKey(t *testing.T) {
9897
tmpDir := t.TempDir()
9998

10099
privateKeyPath := filepath.Join(tmpDir, "trellis_example_com_ed25519")
101-
ioutil.WriteFile(privateKeyPath, []byte{}, 0666)
100+
os.WriteFile(privateKeyPath, []byte{}, 0666)
102101

103102
ui := cli.NewMockUi()
104103
keyGenerateCommand := NewKeyGenerateCommand(ui, trellis)
@@ -125,7 +124,7 @@ func TestKeyGenerateExistingPublicKey(t *testing.T) {
125124

126125
os.Mkdir(filepath.Join(trellis.Path, "public_keys"), os.ModePerm)
127126
publicKeyPath := filepath.Join(trellis.Path, "public_keys", "trellis_example_com_ed25519.pub")
128-
err := ioutil.WriteFile(publicKeyPath, []byte{}, 0666)
127+
err := os.WriteFile(publicKeyPath, []byte{}, 0666)
129128

130129
if err != nil {
131130
t.Fatal(err)

cmd/new.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"flag"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"strings"
@@ -248,7 +247,7 @@ func (c *NewCommand) YamlHeader(doc string) string {
248247

249248
func addTrellisFile(path string) error {
250249
path = filepath.Join(path, ".trellis.yml")
251-
return ioutil.WriteFile(path, []byte{}, 0666)
250+
return os.WriteFile(path, []byte{}, 0666)
252251
}
253252

254253
func askDomain(ui cli.Ui, path string) (host string, err error) {

github/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"os"
109
"path/filepath"
@@ -87,7 +86,7 @@ func FetchLatestRelease(repo string, client *http.Client) (*Release, error) {
8786

8887
defer resp.Body.Close()
8988

90-
body, err := ioutil.ReadAll(resp.Body)
89+
body, err := io.ReadAll(resp.Body)
9190

9291
if err != nil {
9392
return nil, fmt.Errorf("Error reading API response: %v", err)

0 commit comments

Comments
 (0)