Skip to content

Commit

Permalink
removes ioutil usage everywhere which was deprecated in go1.16 (hashi…
Browse files Browse the repository at this point in the history
…corp#15297)

* update go version to 1.18 for api and sdk, go mod tidy
* removes ioutil usage everywhere which was deprecated in go1.16 in favour of io and os packages. Also introduces a lint rule which forbids use of ioutil going forward.
Co-authored-by: R.B. Boyer <[email protected]>
  • Loading branch information
kschoche authored Nov 10, 2022
1 parent d981fb8 commit bf0f61a
Show file tree
Hide file tree
Showing 109 changed files with 363 additions and 397 deletions.
7 changes: 7 additions & 0 deletions .changelog/15297.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:improvement
api: updated the go module directive to 1.18.
```

```release-note:improvement
sdk: updated the go module directive to 1.18.
```
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ linters-settings:
forbidigo:
# Forbid the following identifiers (list of regexp).
forbid:
- '\bioutil\b(# Use io and os packages instead of ioutil)?'
- '\brequire\.New\b(# Use package-level functions with explicit TestingT)?'
- '\bassert\.New\b(# Use package-level functions with explicit TestingT)?'
# Exclude godoc examples from forbidigo checks.
Expand Down
19 changes: 9 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -2141,7 +2140,7 @@ func (a *Agent) readPersistedServiceConfigs() (map[structs.ServiceID]*structs.Se
out := make(map[structs.ServiceID]*structs.ServiceConfigResponse)

configDir := filepath.Join(a.config.DataDir, serviceConfigDir)
files, err := ioutil.ReadDir(configDir)
files, err := os.ReadDir(configDir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
Expand All @@ -2163,7 +2162,7 @@ func (a *Agent) readPersistedServiceConfigs() (map[structs.ServiceID]*structs.Se

// Read the contents into a buffer
file := filepath.Join(configDir, fi.Name())
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed reading service config file %q: %w", file, err)
}
Expand Down Expand Up @@ -3374,7 +3373,7 @@ func (a *Agent) persistCheckState(check *checks.CheckTTL, status, output string)
tempFile := file + ".tmp"

// persistCheckState is called frequently, so don't use writeFileAtomic to avoid calling fsync here
if err := ioutil.WriteFile(tempFile, buf, 0600); err != nil {
if err := os.WriteFile(tempFile, buf, 0600); err != nil {
return fmt.Errorf("failed writing temp file %q: %s", tempFile, err)
}
if err := os.Rename(tempFile, file); err != nil {
Expand All @@ -3389,12 +3388,12 @@ func (a *Agent) loadCheckState(check *structs.HealthCheck) error {
cid := check.CompoundCheckID()
// Try to read the persisted state for this check
file := filepath.Join(a.config.DataDir, checkStateDir, cid.StringHashSHA256())
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
// try the md5 based name. This can be removed once we no longer support upgrades from versions that use MD5 hashing
oldFile := filepath.Join(a.config.DataDir, checkStateDir, cid.StringHashMD5())
buf, err = ioutil.ReadFile(oldFile)
buf, err = os.ReadFile(oldFile)
if err != nil {
if os.IsNotExist(err) {
return nil
Expand Down Expand Up @@ -3596,7 +3595,7 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI

// Load any persisted services
svcDir := filepath.Join(a.config.DataDir, servicesDir)
files, err := ioutil.ReadDir(svcDir)
files, err := os.ReadDir(svcDir)
if err != nil {
if os.IsNotExist(err) {
return nil
Expand All @@ -3617,7 +3616,7 @@ func (a *Agent) loadServices(conf *config.RuntimeConfig, snap map[structs.CheckI

// Read the contents into a buffer
file := filepath.Join(svcDir, fi.Name())
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed reading service file %q: %w", file, err)
}
Expand Down Expand Up @@ -3760,7 +3759,7 @@ func (a *Agent) loadChecks(conf *config.RuntimeConfig, snap map[structs.CheckID]

// Load any persisted checks
checkDir := filepath.Join(a.config.DataDir, checksDir)
files, err := ioutil.ReadDir(checkDir)
files, err := os.ReadDir(checkDir)
if err != nil {
if os.IsNotExist(err) {
return nil
Expand All @@ -3775,7 +3774,7 @@ func (a *Agent) loadChecks(conf *config.RuntimeConfig, snap map[structs.CheckID]

// Read the contents into a buffer
file := filepath.Join(checkDir, fi.Name())
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed reading check file %q: %w", file, err)
}
Expand Down
5 changes: 2 additions & 3 deletions agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -1763,7 +1762,7 @@ func TestAgent_ReloadDoesNotTriggerWatch(t *testing.T) {
}

dc1 := "dc1"
tmpFileRaw, err := ioutil.TempFile("", "rexec")
tmpFileRaw, err := os.CreateTemp("", "rexec")
require.NoError(t, err)
tmpFile := tmpFileRaw.Name()
defer os.Remove(tmpFile)
Expand Down Expand Up @@ -1802,7 +1801,7 @@ func TestAgent_ReloadDoesNotTriggerWatch(t *testing.T) {
contentsStr := ""
// Wait for watch to be populated
for i := 1; i < 7; i++ {
contents, err := ioutil.ReadFile(tmpFile)
contents, err := os.ReadFile(tmpFile)
if err != nil {
t.Fatalf("should be able to read file, but had: %#v", err)
}
Expand Down
Loading

0 comments on commit bf0f61a

Please sign in to comment.