diff --git a/command/acl_bootstrap.go b/command/acl_bootstrap.go index f8970f93849..85955611601 100644 --- a/command/acl_bootstrap.go +++ b/command/acl_bootstrap.go @@ -2,7 +2,7 @@ package command import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -86,10 +86,10 @@ func (c *ACLBootstrapCommand) Run(args []string) int { case "": terminalToken = []byte{} case "-": - terminalToken, err = ioutil.ReadAll(os.Stdin) + terminalToken, err = io.ReadAll(os.Stdin) default: file = args[0] - terminalToken, err = ioutil.ReadFile(file) + terminalToken, err = os.ReadFile(file) } if err != nil { c.Ui.Error(fmt.Sprintf("Error reading provided token: %v", err)) diff --git a/command/acl_policy_apply.go b/command/acl_policy_apply.go index 02c5ef45618..1210d0b88c1 100644 --- a/command/acl_policy_apply.go +++ b/command/acl_policy_apply.go @@ -2,7 +2,7 @@ package command import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -76,13 +76,13 @@ func (c *ACLPolicyApplyCommand) Run(args []string) int { var rawPolicy []byte var err error if file == "-" { - rawPolicy, err = ioutil.ReadAll(os.Stdin) + rawPolicy, err = io.ReadAll(os.Stdin) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err)) return 1 } } else { - rawPolicy, err = ioutil.ReadFile(file) + rawPolicy, err = os.ReadFile(file) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err)) return 1 diff --git a/command/agent/agent.go b/command/agent/agent.go index 6c41574ec1d..a7c6b12124d 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" golog "log" "net" "os" @@ -850,7 +849,7 @@ func (a *Agent) setupNodeID(config *nomad.Config) error { // validate it. Saved state overwrites any configured node id fileID := filepath.Join(config.DataDir, "node-id") if _, err := os.Stat(fileID); err == nil { - rawID, err := ioutil.ReadFile(fileID) + rawID, err := os.ReadFile(fileID) if err != nil { return err } @@ -875,7 +874,7 @@ func (a *Agent) setupNodeID(config *nomad.Config) error { if err := escapingfs.EnsurePath(fileID, false); err != nil { return err } - if err := ioutil.WriteFile(fileID, []byte(config.NodeID), 0600); err != nil { + if err := os.WriteFile(fileID, []byte(config.NodeID), 0600); err != nil { return err } return nil @@ -887,7 +886,7 @@ func (a *Agent) setupNodeID(config *nomad.Config) error { if err := escapingfs.EnsurePath(fileID, false); err != nil { return err } - if err := ioutil.WriteFile(fileID, []byte(id), 0600); err != nil { + if err := os.WriteFile(fileID, []byte(id), 0600); err != nil { return err } diff --git a/command/agent/agent_endpoint_test.go b/command/agent/agent_endpoint_test.go index 0572b84ac02..b8c40753104 100644 --- a/command/agent/agent_endpoint_test.go +++ b/command/agent/agent_endpoint_test.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/http/httptest" @@ -379,7 +379,7 @@ func TestHTTP_AgentMonitor(t *testing.T) { s.Server.logger.Warn("log that should be sent") tried++ } - output, err := ioutil.ReadAll(resp.Body) + output, err := io.ReadAll(resp.Body) if err != nil { return false, err } @@ -420,7 +420,7 @@ func TestHTTP_AgentMonitor(t *testing.T) { s.Agent.logger.Warn("log that should be sent") tried++ } - output, err := ioutil.ReadAll(resp.Body) + output, err := io.ReadAll(resp.Body) if err != nil { return false, err } diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 1767f0c90e8..37c5877223e 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -2,7 +2,6 @@ package agent import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -1273,7 +1272,7 @@ func TestServer_ShouldReload_ReturnTrueForFileChanges(t *testing.T) { dir := t.TempDir() tmpfn := filepath.Join(dir, "testcert") - err := ioutil.WriteFile(tmpfn, content, 0666) + err := os.WriteFile(tmpfn, content, 0666) require.Nil(err) const ( @@ -1325,7 +1324,7 @@ func TestServer_ShouldReload_ReturnTrueForFileChanges(t *testing.T) { ` os.Remove(tmpfn) - err = ioutil.WriteFile(tmpfn, []byte(newCertificate), 0666) + err = os.WriteFile(tmpfn, []byte(newCertificate), 0666) require.Nil(err) newAgentConfig := &Config{ diff --git a/command/agent/alloc_endpoint_test.go b/command/agent/alloc_endpoint_test.go index f85cf296fb7..0f6ef029320 100644 --- a/command/agent/alloc_endpoint_test.go +++ b/command/agent/alloc_endpoint_test.go @@ -4,7 +4,6 @@ import ( "archive/tar" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -766,7 +765,7 @@ func TestHTTP_AllocSnapshot_Atomic(t *testing.T) { os.RemoveAll(allocDir.TaskDirs["web"].LocalDir) // require Snapshot fails - if err := allocDir.Snapshot(ioutil.Discard); err != nil { + if err := allocDir.Snapshot(io.Discard); err != nil { t.Logf("[DEBUG] agent.test: snapshot returned error: %v", err) } else { t.Errorf("expected Snapshot() to fail but it did not") diff --git a/command/agent/bindata_assetfs.go b/command/agent/bindata_assetfs.go index a232e2d07e0..5bd2a4255b6 100644 --- a/command/agent/bindata_assetfs.go +++ b/command/agent/bindata_assetfs.go @@ -38,7 +38,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -786,7 +785,7 @@ func RestoreAsset(dir, name string) error { if err != nil { return err } - err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + err = os.WriteFile(_filePath(dir, name), data, info.Mode()) if err != nil { return err } diff --git a/command/agent/command_test.go b/command/agent/command_test.go index 1e313518b95..2ae127368d3 100644 --- a/command/agent/command_test.go +++ b/command/agent/command_test.go @@ -1,8 +1,8 @@ package agent import ( - "io/ioutil" "math" + "os" "path/filepath" "strings" "testing" @@ -109,7 +109,7 @@ func TestCommand_MetaConfigValidation(t *testing.T) { } for _, tc := range tcases { configFile := filepath.Join(tmpDir, "conf1.hcl") - err := ioutil.WriteFile(configFile, []byte(`client{ + err := os.WriteFile(configFile, []byte(`client{ enabled = true meta = { "valid" = "yes" @@ -160,7 +160,7 @@ func TestCommand_NullCharInDatacenter(t *testing.T) { } for _, tc := range tcases { configFile := filepath.Join(tmpDir, "conf1.hcl") - err := ioutil.WriteFile(configFile, []byte(` + err := os.WriteFile(configFile, []byte(` datacenter = "`+tc+`" client{ enabled = true @@ -207,7 +207,7 @@ func TestCommand_NullCharInRegion(t *testing.T) { } for _, tc := range tcases { configFile := filepath.Join(tmpDir, "conf1.hcl") - err := ioutil.WriteFile(configFile, []byte(` + err := os.WriteFile(configFile, []byte(` region = "`+tc+`" client{ enabled = true diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 4477cedb88a..e9d1a1836fc 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -2,7 +2,6 @@ package agent import ( "fmt" - "io/ioutil" "net" "os" "path/filepath" @@ -460,7 +459,7 @@ func TestConfig_ParseConfigFile(t *testing.T) { t.Fatalf("expected error, got nothing") } - fh, err := ioutil.TempFile("", "nomad") + fh, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -514,19 +513,19 @@ func TestConfig_LoadConfigDir(t *testing.T) { } file1 := filepath.Join(dir, "conf1.hcl") - err = ioutil.WriteFile(file1, []byte(`{"region":"west"}`), 0600) + err = os.WriteFile(file1, []byte(`{"region":"west"}`), 0600) if err != nil { t.Fatalf("err: %s", err) } file2 := filepath.Join(dir, "conf2.hcl") - err = ioutil.WriteFile(file2, []byte(`{"datacenter":"sfo"}`), 0600) + err = os.WriteFile(file2, []byte(`{"datacenter":"sfo"}`), 0600) if err != nil { t.Fatalf("err: %s", err) } file3 := filepath.Join(dir, "conf3.hcl") - err = ioutil.WriteFile(file3, []byte(`nope;!!!`), 0600) + err = os.WriteFile(file3, []byte(`nope;!!!`), 0600) if err != nil { t.Fatalf("err: %s", err) } @@ -558,7 +557,7 @@ func TestConfig_LoadConfig(t *testing.T) { t.Fatalf("expected error, got nothing") } - fh, err := ioutil.TempFile("", "nomad") + fh, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -586,7 +585,7 @@ func TestConfig_LoadConfig(t *testing.T) { dir := t.TempDir() file1 := filepath.Join(dir, "config1.hcl") - err = ioutil.WriteFile(file1, []byte(`{"datacenter":"sfo"}`), 0600) + err = os.WriteFile(file1, []byte(`{"datacenter":"sfo"}`), 0600) if err != nil { t.Fatalf("err: %s", err) } @@ -1342,7 +1341,7 @@ func TestTelemetry_Parse(t *testing.T) { dir := t.TempDir() file1 := filepath.Join(dir, "config1.hcl") - err := ioutil.WriteFile(file1, []byte(`telemetry{ + err := os.WriteFile(file1, []byte(`telemetry{ prefix_filter = ["+nomad.raft"] filter_default = false disable_dispatched_job_summary_metrics = true diff --git a/command/agent/consul/group_test.go b/command/agent/consul/group_test.go index 1af9a113dd4..969b524211e 100644 --- a/command/agent/consul/group_test.go +++ b/command/agent/consul/group_test.go @@ -1,7 +1,7 @@ package consul import ( - "io/ioutil" + "io" "testing" "time" @@ -22,8 +22,8 @@ func TestConsul_Connect(t *testing.T) { testconsul, err := testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) { // If -v wasn't specified squelch consul logging if !testing.Verbose() { - c.Stdout = ioutil.Discard - c.Stderr = ioutil.Discard + c.Stdout = io.Discard + c.Stderr = io.Discard } }) if err != nil { diff --git a/command/agent/consul/int_test.go b/command/agent/consul/int_test.go index 3e1d31aeb07..bc124ee4622 100644 --- a/command/agent/consul/int_test.go +++ b/command/agent/consul/int_test.go @@ -2,7 +2,7 @@ package consul_test import ( "context" - "io/ioutil" + "io" "testing" "time" @@ -48,8 +48,8 @@ func TestConsul_Integration(t *testing.T) { testconsul, err := testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) { // If -v wasn't specified squelch consul logging if !testing.Verbose() { - c.Stdout = ioutil.Discard - c.Stderr = ioutil.Discard + c.Stdout = io.Discard + c.Stderr = io.Discard } }) if err != nil { diff --git a/command/agent/fs_endpoint_test.go b/command/agent/fs_endpoint_test.go index 1acb468a23e..90b3d01cf44 100644 --- a/command/agent/fs_endpoint_test.go +++ b/command/agent/fs_endpoint_test.go @@ -3,7 +3,7 @@ package agent import ( "encoding/base64" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strings" @@ -322,7 +322,7 @@ func TestHTTP_FS_ReadAt(t *testing.T) { _, err = s.Server.FileReadAtRequest(respW, req) require.Nil(err) - output, err := ioutil.ReadAll(respW.Result().Body) + output, err := io.ReadAll(respW.Result().Body) require.Nil(err) require.EqualValues(expectation, output) }) @@ -341,7 +341,7 @@ func TestHTTP_FS_ReadAt_XSS(t *testing.T) { require.NoError(t, err) defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, xssLoggerMockDriverStdout, string(buf)) @@ -368,7 +368,7 @@ func TestHTTP_FS_Cat(t *testing.T) { _, err = s.Server.FileCatRequest(respW, req) require.Nil(err) - output, err := ioutil.ReadAll(respW.Result().Body) + output, err := io.ReadAll(respW.Result().Body) require.Nil(err) require.EqualValues(defaultLoggerMockDriverStdout, output) }) @@ -386,7 +386,7 @@ func TestHTTP_FS_Cat_XSS(t *testing.T) { require.NoError(t, err) defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, xssLoggerMockDriverStdout, string(buf)) @@ -423,7 +423,7 @@ func TestHTTP_FS_Stream_NoFollow(t *testing.T) { out := "" testutil.WaitForResult(func() (bool, error) { - output, err := ioutil.ReadAll(respW) + output, err := io.ReadAll(respW) if err != nil { return false, err } @@ -455,7 +455,7 @@ func TestHTTP_FS_Stream_NoFollow_XSS(t *testing.T) { require.NoError(t, err) defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) require.NoError(t, err) expected := `{"Data":"PHNjcmlwdD5hbGVydChkb2N1bWVudC5kb21haW4pOzwvc2NyaXB0Pg==","File":"alloc/logs/web.stdout.0","Offset":40}` require.Equal(t, expected, string(buf)) @@ -487,7 +487,7 @@ func TestHTTP_FS_Stream_Follow(t *testing.T) { out := "" testutil.WaitForResult(func() (bool, error) { - output, err := ioutil.ReadAll(respW.Body) + output, err := io.ReadAll(respW.Body) if err != nil { return false, err } @@ -528,7 +528,7 @@ func TestHTTP_FS_Logs(t *testing.T) { out := "" testutil.WaitForResult(func() (bool, error) { - output, err := ioutil.ReadAll(respW) + output, err := io.ReadAll(respW) if err != nil { return false, err } @@ -557,7 +557,7 @@ func TestHTTP_FS_Logs_XSS(t *testing.T) { require.NoError(t, err) defer resp.Body.Close() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, xssLoggerMockDriverStdout, string(buf)) @@ -588,7 +588,7 @@ func TestHTTP_FS_Logs_Follow(t *testing.T) { out := "" testutil.WaitForResult(func() (bool, error) { - output, err := ioutil.ReadAll(respW) + output, err := io.ReadAll(respW) if err != nil { return false, err } diff --git a/command/agent/host/host.go b/command/agent/host/host.go index 9119f7b96cc..08aeb53bdfb 100644 --- a/command/agent/host/host.go +++ b/command/agent/host/host.go @@ -1,7 +1,7 @@ package host import ( - "io/ioutil" + "io" "os" "strings" ) @@ -119,7 +119,7 @@ func slurp(path string) string { return err.Error() } - bs, err := ioutil.ReadAll(fh) + bs, err := io.ReadAll(fh) if err != nil { return err.Error() } diff --git a/command/agent/http_test.go b/command/agent/http_test.go index aea6095e4d9..c610fbab9b2 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -271,7 +270,7 @@ func TestWrapNonJSON(t *testing.T) { req, _ := http.NewRequest("GET", "/v1/kv/key", nil) s.Server.wrapNonJSON(handler)(resp, req) - respBody, _ := ioutil.ReadAll(resp.Body) + respBody, _ := io.ReadAll(resp.Body) require.Equal(t, respBody, []byte("test response")) } @@ -294,7 +293,7 @@ func TestWrapNonJSON_Error(t *testing.T) { resp := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/v1/kv/key", nil) s.Server.wrapNonJSON(handlerRPCErr)(resp, req) - respBody, _ := ioutil.ReadAll(resp.Body) + respBody, _ := io.ReadAll(resp.Body) require.Equal(t, []byte("not found"), respBody) require.Equal(t, 404, resp.Code) } @@ -304,7 +303,7 @@ func TestWrapNonJSON_Error(t *testing.T) { resp := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/v1/kv/key", nil) s.Server.wrapNonJSON(handlerCodedErr)(resp, req) - respBody, _ := ioutil.ReadAll(resp.Body) + respBody, _ := io.ReadAll(resp.Body) require.Equal(t, []byte("unprocessable"), respBody) require.Equal(t, 422, resp.Code) } @@ -354,7 +353,7 @@ func testPrettyPrint(pretty string, prettyFmt bool, t *testing.T) { if err != nil { t.Fatalf("failed to encode: %v", err) } - actual, err := ioutil.ReadAll(resp.Body) + actual, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("err: %s", err) } @@ -798,7 +797,7 @@ func TestHTTP_VerifyHTTPSClient(t *testing.T) { // FAIL: Requests that specify a valid hostname and CA cert but lack a // client certificate should fail - cacertBytes, err := ioutil.ReadFile(cafile) + cacertBytes, err := os.ReadFile(cafile) if err != nil { t.Fatalf("error reading cacert: %v", err) } @@ -908,7 +907,7 @@ func TestHTTP_VerifyHTTPSClient_AfterConfigReload(t *testing.T) { // HTTPS request should succeed httpsReqURL := fmt.Sprintf("https://%s/v1/agent/self", s.Agent.config.AdvertiseAddrs.HTTP) - cacertBytes, err := ioutil.ReadFile(cafile) + cacertBytes, err := os.ReadFile(cafile) assert.Nil(err) tlsConf.RootCAs.AppendCertsFromPEM(cacertBytes) @@ -939,7 +938,7 @@ func TestHTTP_VerifyHTTPSClient_AfterConfigReload(t *testing.T) { }, } - cacertBytes, err = ioutil.ReadFile(cafile) + cacertBytes, err = os.ReadFile(cafile) assert.Nil(err) tlsConf.RootCAs.AppendCertsFromPEM(cacertBytes) @@ -1419,7 +1418,7 @@ func Test_decodeBody(t *testing.T) { name: "empty input request body", }, { - inputReq: &http.Request{Body: ioutil.NopCloser(strings.NewReader(`{"foo":"bar"}`))}, + inputReq: &http.Request{Body: io.NopCloser(strings.NewReader(`{"foo":"bar"}`))}, inputOut: &struct { Foo string `json:"foo"` }{}, @@ -1497,5 +1496,5 @@ func encodeReq(obj interface{}) io.ReadCloser { buf := bytes.NewBuffer(nil) enc := json.NewEncoder(buf) enc.Encode(obj) - return ioutil.NopCloser(buf) + return io.NopCloser(buf) } diff --git a/command/agent/keyring.go b/command/agent/keyring.go index a8537b9caf2..7a58c30b526 100644 --- a/command/agent/keyring.go +++ b/command/agent/keyring.go @@ -4,7 +4,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" @@ -81,7 +80,7 @@ func loadKeyringFile(c *serf.Config) error { } // Read in the keyring file data - keyringData, err := ioutil.ReadFile(c.KeyringFile) + keyringData, err := os.ReadFile(c.KeyringFile) if err != nil { return err } diff --git a/command/agent/keyring_test.go b/command/agent/keyring_test.go index bdf7564d511..d234b8a557e 100644 --- a/command/agent/keyring_test.go +++ b/command/agent/keyring_test.go @@ -2,7 +2,7 @@ package agent import ( "fmt" - "io/ioutil" + "os" "path/filepath" "testing" @@ -63,7 +63,7 @@ func TestAgent_InitKeyring(t *testing.T) { t.Fatalf("err: %s", err) } - content, err := ioutil.ReadFile(file) + content, err := os.ReadFile(file) if err != nil { t.Fatalf("err: %s", err) } @@ -77,7 +77,7 @@ func TestAgent_InitKeyring(t *testing.T) { } // Content should still be the same - content, err = ioutil.ReadFile(file) + content, err = os.ReadFile(file) if err != nil { t.Fatalf("err: %s", err) } diff --git a/command/agent/log_file_test.go b/command/agent/log_file_test.go index 5d3c3b9629a..26864ec2cbd 100644 --- a/command/agent/log_file_test.go +++ b/command/agent/log_file_test.go @@ -1,7 +1,7 @@ package agent import ( - "io/ioutil" + "os" "path/filepath" "testing" "time" @@ -33,7 +33,7 @@ func TestLogFile_timeRotation(t *testing.T) { time.Sleep(2 * time.Second) logFile.Write([]byte("Second File")) want := 2 - if got, _ := ioutil.ReadDir(tempDir); len(got) != want { + if got, _ := os.ReadDir(tempDir); len(got) != want { t.Errorf("Expected %d files, got %v file(s)", want, len(got)) } } @@ -55,7 +55,7 @@ func TestLogFile_openNew(t *testing.T) { } require.NoError(logFile.openNew()) - _, err := ioutil.ReadFile(logFile.FileInfo.Name()) + _, err := os.ReadFile(logFile.FileInfo.Name()) require.NoError(err) require.Equal(logFile.FileInfo.Name(), filepath.Join(tempDir, testFileName)) @@ -91,7 +91,7 @@ func TestLogFile_byteRotation(t *testing.T) { logFile.Write([]byte("Hello World")) logFile.Write([]byte("Second File")) want := 2 - tempFiles, _ := ioutil.ReadDir(tempDir) + tempFiles, _ := os.ReadDir(tempDir) require.Equal(want, len(tempFiles)) } @@ -112,7 +112,7 @@ func TestLogFile_logLevelFiltering(t *testing.T) { logFile.Write([]byte("[DEBUG] This is a debug message")) logFile.Write([]byte("[ERR] This is an error message")) want := 2 - tempFiles, _ := ioutil.ReadDir(tempDir) + tempFiles, _ := os.ReadDir(tempDir) require.Equal(want, len(tempFiles)) } @@ -136,7 +136,7 @@ func TestLogFile_deleteArchives(t *testing.T) { logFile.Write([]byte("[INFO] Second File")) logFile.Write([]byte("[INFO] Third File")) want := 2 - tempFiles, _ := ioutil.ReadDir(tempDir) + tempFiles, _ := os.ReadDir(tempDir) require.Equal(want, len(tempFiles)) @@ -144,7 +144,7 @@ func TestLogFile_deleteArchives(t *testing.T) { var bytes []byte var err error path := filepath.Join(tempDir, tempFile.Name()) - if bytes, err = ioutil.ReadFile(path); err != nil { + if bytes, err = os.ReadFile(path); err != nil { t.Errorf(err.Error()) return } @@ -174,6 +174,6 @@ func TestLogFile_deleteArchivesDisabled(t *testing.T) { logFile.Write([]byte("[INFO] Second File")) logFile.Write([]byte("[INFO] Third File")) want := 3 - tempFiles, _ := ioutil.ReadDir(tempDir) + tempFiles, _ := os.ReadDir(tempDir) require.Equal(want, len(tempFiles)) } diff --git a/command/agent/log_levels.go b/command/agent/log_levels.go index 8b4e3523650..a91e75959fe 100644 --- a/command/agent/log_levels.go +++ b/command/agent/log_levels.go @@ -1,7 +1,7 @@ package agent import ( - "io/ioutil" + "io" "github.com/hashicorp/logutils" ) @@ -12,7 +12,7 @@ func LevelFilter() *logutils.LevelFilter { return &logutils.LevelFilter{ Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"}, MinLevel: "INFO", - Writer: ioutil.Discard, + Writer: io.Discard, } } diff --git a/command/agent/testagent.go b/command/agent/testagent.go index 25f555c2039..092ffbc4ce5 100644 --- a/command/agent/testagent.go +++ b/command/agent/testagent.go @@ -2,7 +2,6 @@ package agent import ( "fmt" - "io/ioutil" "math/rand" "net/http" "net/http/httptest" @@ -123,7 +122,7 @@ func (a *TestAgent) Start() *TestAgent { name = a.Name + "-agent" } name = strings.ReplaceAll(name, "/", "_") - d, err := ioutil.TempDir(TempDir, name) + d, err := os.MkdirTemp(TempDir, name) if err != nil { a.T.Fatalf("Error creating data dir %s: %s", filepath.Join(TempDir, name), err) } diff --git a/command/helpers_test.go b/command/helpers_test.go index 187afee2287..127e3953f62 100644 --- a/command/helpers_test.go +++ b/command/helpers_test.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "os" "reflect" @@ -130,9 +129,9 @@ test`, } for i, c := range cases { - in := ioutil.NopCloser(strings.NewReader(c.Input)) + in := io.NopCloser(strings.NewReader(c.Input)) limit := NewLineLimitReader(in, c.Lines, c.SearchLimit, 0) - outBytes, err := ioutil.ReadAll(limit) + outBytes, err := io.ReadAll(limit) if err != nil { t.Fatalf("case %d failed: %v", i, err) } @@ -182,7 +181,7 @@ func TestHelpers_LineLimitReader_TimeLimit(t *testing.T) { go func() { defer close(resultCh) defer close(errCh) - outBytes, err := ioutil.ReadAll(limit) + outBytes, err := io.ReadAll(limit) if err != nil { errCh <- fmt.Errorf("ReadAll failed: %v", err) return @@ -258,7 +257,7 @@ var ( // Test APIJob with local jobfile func TestJobGetter_LocalFile(t *testing.T) { ci.Parallel(t) - fh, err := ioutil.TempFile("", "nomad") + fh, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -307,7 +306,7 @@ func TestJobGetter_LocalFile_InvalidHCL2(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - fh, err := ioutil.TempFile("", "nomad") + fh, err := os.CreateTemp("", "nomad") require.NoError(t, err) defer os.Remove(fh.Name()) defer fh.Close() @@ -353,7 +352,7 @@ job "example" { fileVars := `var3 = "from-varfile"` expected := []string{"default-val", "from-cli", "from-varfile", "from-envvar"} - hclf, err := ioutil.TempFile("", "hcl") + hclf, err := os.CreateTemp("", "hcl") require.NoError(t, err) defer os.Remove(hclf.Name()) defer hclf.Close() @@ -361,7 +360,7 @@ job "example" { _, err = hclf.WriteString(hcl) require.NoError(t, err) - vf, err := ioutil.TempFile("", "var.hcl") + vf, err := os.CreateTemp("", "var.hcl") require.NoError(t, err) defer os.Remove(vf.Name()) defer vf.Close() @@ -404,7 +403,7 @@ unsedVar2 = "from-varfile" ` expected := []string{"default-val", "from-cli", "from-varfile", "from-envvar"} - hclf, err := ioutil.TempFile("", "hcl") + hclf, err := os.CreateTemp("", "hcl") require.NoError(t, err) defer os.Remove(hclf.Name()) defer hclf.Close() @@ -412,7 +411,7 @@ unsedVar2 = "from-varfile" _, err = hclf.WriteString(hcl) require.NoError(t, err) - vf, err := ioutil.TempFile("", "var.hcl") + vf, err := os.CreateTemp("", "var.hcl") require.NoError(t, err) defer os.Remove(vf.Name()) defer vf.Close() diff --git a/command/job_dispatch.go b/command/job_dispatch.go index b7ce752128f..fda8c381ee1 100644 --- a/command/job_dispatch.go +++ b/command/job_dispatch.go @@ -2,7 +2,7 @@ package command import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -141,9 +141,9 @@ func (c *JobDispatchCommand) Run(args []string) int { if len(args) == 2 { switch args[1] { case "-": - payload, readErr = ioutil.ReadAll(os.Stdin) + payload, readErr = io.ReadAll(os.Stdin) default: - payload, readErr = ioutil.ReadFile(args[1]) + payload, readErr = os.ReadFile(args[1]) } if readErr != nil { c.Ui.Error(fmt.Sprintf("Error reading input data: %v", readErr)) diff --git a/command/job_init.go b/command/job_init.go index 94522ae7e07..1d6c89b5189 100644 --- a/command/job_init.go +++ b/command/job_init.go @@ -2,7 +2,6 @@ package command import ( "fmt" - "io/ioutil" "os" "strings" @@ -114,7 +113,7 @@ func (c *JobInitCommand) Run(args []string) int { } // Write out the example - err = ioutil.WriteFile(filename, jobSpec, 0660) + err = os.WriteFile(filename, jobSpec, 0660) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to write '%s': %v", filename, err)) return 1 diff --git a/command/job_init_test.go b/command/job_init_test.go index 4c474e2b46f..5fdb2fa7040 100644 --- a/command/job_init_test.go +++ b/command/job_init_test.go @@ -1,7 +1,6 @@ package command import ( - "io/ioutil" "os" "strings" "testing" @@ -47,7 +46,7 @@ func TestInitCommand_Run(t *testing.T) { if code := cmd.Run([]string{}); code != 0 { t.Fatalf("expect exit code 0, got: %d", code) } - content, err := ioutil.ReadFile(DefaultInitName) + content, err := os.ReadFile(DefaultInitName) if err != nil { t.Fatalf("err: %s", err) } @@ -61,7 +60,7 @@ func TestInitCommand_Run(t *testing.T) { if code := cmd.Run([]string{"-short"}); code != 0 { require.Zero(t, code, "unexpected exit code: %d", code) } - content, err = ioutil.ReadFile(DefaultInitName) + content, err = os.ReadFile(DefaultInitName) require.NoError(t, err) shortJob, _ := Asset("command/assets/example-short.nomad") require.Equal(t, string(content), string(shortJob)) @@ -109,7 +108,7 @@ func TestInitCommand_customFilename(t *testing.T) { if code := cmd.Run([]string{filename}); code != 0 { t.Fatalf("expect exit code 0, got: %d", code) } - content, err := ioutil.ReadFile(filename) + content, err := os.ReadFile(filename) if err != nil { t.Fatalf("err: %s", err) } @@ -123,7 +122,7 @@ func TestInitCommand_customFilename(t *testing.T) { if code := cmd.Run([]string{"-short", filename}); code != 0 { require.Zero(t, code, "unexpected exit code: %d", code) } - content, err = ioutil.ReadFile(filename) + content, err = os.ReadFile(filename) require.NoError(t, err) shortJob, _ := Asset("command/assets/example-short.nomad") require.Equal(t, string(content), string(shortJob)) diff --git a/command/job_plan_test.go b/command/job_plan_test.go index 33ec6c2cff9..5225d1ef756 100644 --- a/command/job_plan_test.go +++ b/command/job_plan_test.go @@ -1,7 +1,6 @@ package command import ( - "io/ioutil" "os" "strconv" "strings" @@ -49,7 +48,7 @@ func TestPlanCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on invalid HCL - fh1, err := ioutil.TempFile("", "nomad") + fh1, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -66,7 +65,7 @@ func TestPlanCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on invalid job spec - fh2, err := ioutil.TempFile("", "nomad") + fh2, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -83,7 +82,7 @@ func TestPlanCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on connection failure (requires a valid job) - fh3, err := ioutil.TempFile("", "nomad") + fh3, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } diff --git a/command/job_run_test.go b/command/job_run_test.go index 0df73495ff3..1af17ccc198 100644 --- a/command/job_run_test.go +++ b/command/job_run_test.go @@ -2,7 +2,6 @@ package command import ( "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -23,7 +22,7 @@ func TestRunCommand_Output_Json(t *testing.T) { ui := cli.NewMockUi() cmd := &JobRunCommand{Meta: Meta{Ui: ui}} - fh, err := ioutil.TempFile("", "nomad") + fh, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -101,7 +100,7 @@ func TestRunCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on invalid HCL - fh1, err := ioutil.TempFile("", "nomad") + fh1, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -118,7 +117,7 @@ func TestRunCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on invalid job spec - fh2, err := ioutil.TempFile("", "nomad") + fh2, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -135,7 +134,7 @@ func TestRunCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on connection failure (requires a valid job) - fh3, err := ioutil.TempFile("", "nomad") + fh3, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } diff --git a/command/job_validate_test.go b/command/job_validate_test.go index 1f74cb65345..678571bba97 100644 --- a/command/job_validate_test.go +++ b/command/job_validate_test.go @@ -1,7 +1,6 @@ package command import ( - "io/ioutil" "os" "strings" "testing" @@ -110,7 +109,7 @@ func TestValidateCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on invalid HCL - fh1, err := ioutil.TempFile("", "nomad") + fh1, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } @@ -127,7 +126,7 @@ func TestValidateCommand_Fails(t *testing.T) { ui.ErrorWriter.Reset() // Fails on invalid job spec - fh2, err := ioutil.TempFile("", "nomad") + fh2, err := os.CreateTemp("", "nomad") if err != nil { t.Fatalf("err: %s", err) } diff --git a/command/namespace_apply.go b/command/namespace_apply.go index 357b1f112f3..9093ea6e36d 100644 --- a/command/namespace_apply.go +++ b/command/namespace_apply.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "os" "strings" @@ -120,13 +120,13 @@ func (c *NamespaceApplyCommand) Run(args []string) int { } if file == "-" { - rawNamespace, err = ioutil.ReadAll(os.Stdin) + rawNamespace, err = io.ReadAll(os.Stdin) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err)) return 1 } } else { - rawNamespace, err = ioutil.ReadFile(file) + rawNamespace, err = os.ReadFile(file) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err)) return 1 diff --git a/command/operator_api.go b/command/operator_api.go index 36dd375b241..20bad25605a 100644 --- a/command/operator_api.go +++ b/command/operator_api.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "fmt" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -151,7 +150,7 @@ func (c *OperatorAPICommand) Run(args []string) int { // Load stdin into a *bytes.Reader so that http.NewRequest can set the // correct Content-Length value. - b, err := ioutil.ReadAll(Stdin) + b, err := io.ReadAll(Stdin) if err != nil { c.Ui.Error(fmt.Sprintf("Error reading stdin: %v", err)) return 1 diff --git a/command/operator_debug.go b/command/operator_debug.go index 8916517e6eb..5a778bd1bfb 100644 --- a/command/operator_debug.go +++ b/command/operator_debug.go @@ -11,7 +11,6 @@ import ( "fmt" "html/template" "io" - "io/ioutil" "net/http" "os" "os/signal" @@ -482,7 +481,7 @@ func (c *OperatorDebugCommand) Run(args []string) int { } } else { // Generate temp directory - tmp, err = ioutil.TempDir(os.TempDir(), stamped) + tmp, err = os.MkdirTemp(os.TempDir(), stamped) if err != nil { c.Ui.Error(fmt.Sprintf("Error creating tmp directory: %s", err.Error())) return 2 @@ -1338,7 +1337,7 @@ func (c *OperatorDebugCommand) writeBody(dir, file string, resp *http.Response, defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { c.writeError(dir, file, err) return @@ -1678,7 +1677,7 @@ func (e *external) token() string { } if e.tokenFile != "" { - bs, err := ioutil.ReadFile(e.tokenFile) + bs, err := os.ReadFile(e.tokenFile) if err == nil { return strings.TrimSpace(string(bs)) } diff --git a/command/operator_debug_test.go b/command/operator_debug_test.go index 25d4378d29d..82152c27e3d 100644 --- a/command/operator_debug_test.go +++ b/command/operator_debug_test.go @@ -2,7 +2,7 @@ package command import ( "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "os" @@ -729,8 +729,8 @@ func TestDebug_CollectConsul(t *testing.T) { testconsul, err := consultest.NewTestServerConfigT(t, func(c *consultest.TestServerConfig) { // If -v wasn't specified squelch consul logging if !testing.Verbose() { - c.Stdout = ioutil.Discard - c.Stderr = ioutil.Discard + c.Stdout = io.Discard + c.Stderr = io.Discard } }) require.NoError(t, err) diff --git a/command/operator_snapshot_inspect_test.go b/command/operator_snapshot_inspect_test.go index f60856c0186..7a0e9e7d036 100644 --- a/command/operator_snapshot_inspect_test.go +++ b/command/operator_snapshot_inspect_test.go @@ -1,7 +1,7 @@ package command import ( - "io/ioutil" + "os" "path/filepath" "testing" @@ -40,7 +40,7 @@ func TestOperatorSnapshotInspect_HandlesFailure(t *testing.T) { tmpDir := t.TempDir() - err := ioutil.WriteFile( + err := os.WriteFile( filepath.Join(tmpDir, "invalid.snap"), []byte("invalid data"), 0600) diff --git a/command/quota_apply.go b/command/quota_apply.go index 90bedbdfd3d..b347606b3f7 100644 --- a/command/quota_apply.go +++ b/command/quota_apply.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "os" "strings" @@ -85,13 +85,13 @@ func (c *QuotaApplyCommand) Run(args []string) int { var rawQuota []byte var err error if file == "-" { - rawQuota, err = ioutil.ReadAll(os.Stdin) + rawQuota, err = io.ReadAll(os.Stdin) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err)) return 1 } } else { - rawQuota, err = ioutil.ReadFile(file) + rawQuota, err = os.ReadFile(file) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err)) return 1 diff --git a/command/quota_init.go b/command/quota_init.go index 01b420d2629..8c0c92133d1 100644 --- a/command/quota_init.go +++ b/command/quota_init.go @@ -2,7 +2,6 @@ package command import ( "fmt" - "io/ioutil" "os" "strings" @@ -97,7 +96,7 @@ func (c *QuotaInitCommand) Run(args []string) int { } // Write out the example - err = ioutil.WriteFile(fileName, []byte(fileContent), 0660) + err = os.WriteFile(fileName, []byte(fileContent), 0660) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to write %q: %v", fileName, err)) return 1 diff --git a/command/quota_init_test.go b/command/quota_init_test.go index 980c29af0cb..f669fb02329 100644 --- a/command/quota_init_test.go +++ b/command/quota_init_test.go @@ -1,7 +1,6 @@ package command import ( - "io/ioutil" "os" "testing" @@ -42,7 +41,7 @@ func TestQuotaInitCommand_Run_HCL(t *testing.T) { require.Empty(t, ui.ErrorWriter.String()) require.Zero(t, code) - content, err := ioutil.ReadFile(DefaultHclQuotaInitName) + content, err := os.ReadFile(DefaultHclQuotaInitName) require.NoError(t, err) require.Equal(t, defaultHclQuotaSpec, string(content)) @@ -57,7 +56,7 @@ func TestQuotaInitCommand_Run_HCL(t *testing.T) { require.Empty(t, ui.ErrorWriter.String()) require.Zero(t, code) - content, err = ioutil.ReadFile("mytest.hcl") + content, err = os.ReadFile("mytest.hcl") require.NoError(t, err) require.Equal(t, defaultHclQuotaSpec, string(content)) } @@ -89,7 +88,7 @@ func TestQuotaInitCommand_Run_JSON(t *testing.T) { require.Empty(t, ui.ErrorWriter.String()) require.Zero(t, code) - content, err := ioutil.ReadFile(DefaultJsonQuotaInitName) + content, err := os.ReadFile(DefaultJsonQuotaInitName) require.NoError(t, err) require.Equal(t, defaultJsonQuotaSpec, string(content)) @@ -104,7 +103,7 @@ func TestQuotaInitCommand_Run_JSON(t *testing.T) { require.Empty(t, ui.ErrorWriter.String()) require.Zero(t, code) - content, err = ioutil.ReadFile("mytest.json") + content, err = os.ReadFile("mytest.json") require.NoError(t, err) require.Equal(t, defaultJsonQuotaSpec, string(content)) } diff --git a/command/sentinel_apply.go b/command/sentinel_apply.go index acfd9ccd7fc..f00a40e1a9e 100644 --- a/command/sentinel_apply.go +++ b/command/sentinel_apply.go @@ -2,7 +2,7 @@ package command import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -91,13 +91,13 @@ func (c *SentinelApplyCommand) Run(args []string) int { file := args[1] var rawPolicy []byte if file == "-" { - rawPolicy, err = ioutil.ReadAll(os.Stdin) + rawPolicy, err = io.ReadAll(os.Stdin) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err)) return 1 } } else { - rawPolicy, err = ioutil.ReadFile(file) + rawPolicy, err = os.ReadFile(file) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err)) return 1 diff --git a/command/volume_create.go b/command/volume_create.go index 9eaeff5cb86..91b4dd13928 100644 --- a/command/volume_create.go +++ b/command/volume_create.go @@ -2,7 +2,7 @@ package command import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -68,13 +68,13 @@ func (c *VolumeCreateCommand) Run(args []string) int { var rawVolume []byte var err error if file == "-" { - rawVolume, err = ioutil.ReadAll(os.Stdin) + rawVolume, err = io.ReadAll(os.Stdin) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err)) return 1 } } else { - rawVolume, err = ioutil.ReadFile(file) + rawVolume, err = os.ReadFile(file) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err)) return 1 diff --git a/command/volume_init.go b/command/volume_init.go index c2901e384eb..cb23b44df25 100644 --- a/command/volume_init.go +++ b/command/volume_init.go @@ -2,7 +2,6 @@ package command import ( "fmt" - "io/ioutil" "os" "strings" @@ -97,7 +96,7 @@ func (c *VolumeInitCommand) Run(args []string) int { } // Write out the example - err = ioutil.WriteFile(fileName, []byte(fileContent), 0660) + err = os.WriteFile(fileName, []byte(fileContent), 0660) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to write %q: %v", fileName, err)) return 1 diff --git a/command/volume_register.go b/command/volume_register.go index 76af00fabc1..f877c97229e 100644 --- a/command/volume_register.go +++ b/command/volume_register.go @@ -2,7 +2,7 @@ package command import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -71,13 +71,13 @@ func (c *VolumeRegisterCommand) Run(args []string) int { var rawVolume []byte var err error if file == "-" { - rawVolume, err = ioutil.ReadAll(os.Stdin) + rawVolume, err = io.ReadAll(os.Stdin) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err)) return 1 } } else { - rawVolume, err = ioutil.ReadFile(file) + rawVolume, err = os.ReadFile(file) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err)) return 1