Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os packages (DataDog#14108
Browse files Browse the repository at this point in the history
)

* cmd/agent: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* cmd/cluster-agent: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* cmd/process-agent: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* cmd/py-launcher: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* cmd/secrethelper: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* cmd/serverless-init: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* cmd/trace-agent: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* internal: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/aggregator: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/api: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/autodiscovery: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/cli: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/collector: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/config: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/dogstatsd: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/ebpf: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/flare: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/forwarder: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/logs: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/metadata: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/network: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/otlp: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/persistencecache: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/pidfile: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/process: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/remoteconfig: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/secrets: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/serializer: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/serverless: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/snmp: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/trace: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* pkg/util: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* rtloader/test: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* tools/retry_file_dump: move from `io/ioutil` to `io` and `os` packages

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* Update pkg/forwarder/internal/retry/on_disk_retry_queue.go

Co-authored-by: Rémy Mathieu <remeh@remeh.fr>

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Co-authored-by: Rémy Mathieu <remeh@remeh.fr>
Juneezee and remeh authored Nov 18, 2022
1 parent 8654a1d commit afff598
Showing 191 changed files with 558 additions and 625 deletions.
6 changes: 3 additions & 3 deletions cmd/agent/api/internal/agent/agent.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"sort"
@@ -107,7 +107,7 @@ func makeFlare(w http.ResponseWriter, r *http.Request) {
var profile flare.ProfileData

if r.Body != http.NoBody {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, log.Errorf("Error while reading HTTP request body: %s", err).Error(), 500)
return
@@ -225,7 +225,7 @@ func streamLogs(w http.ResponseWriter, r *http.Request) {
var filters diagnostic.Filters

if r.Body != http.NoBody {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, log.Errorf("Error while reading HTTP request body: %s", err).Error(), 500)
return
15 changes: 7 additions & 8 deletions cmd/agent/common/import.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ package common

import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
@@ -72,7 +71,7 @@ func ImportConfig(oldConfigDir string, newConfigDir string, force bool) error {
}

// move existing config files to the new configuration directory
files, err := ioutil.ReadDir(filepath.Join(oldConfigDir, "conf.d"))
files, err := os.ReadDir(filepath.Join(oldConfigDir, "conf.d"))
if err != nil {
if os.IsNotExist(err) {
fmt.Fprintln(color.Output,
@@ -146,7 +145,7 @@ func ImportConfig(oldConfigDir string, newConfigDir string, force bool) error {
// dump the current configuration to datadog.yaml
// file permissions will be used only to create the file if doesn't exist,
// please note on Windows such permissions have no effect.
if err = ioutil.WriteFile(datadogYamlPath, b, 0640); err != nil {
if err = os.WriteFile(datadogYamlPath, b, 0640); err != nil {
return fmt.Errorf("unable to write config to %s: %v", datadogYamlPath, err)
}

@@ -160,7 +159,7 @@ func ImportConfig(oldConfigDir string, newConfigDir string, force bool) error {
)

// move existing config templates to the new auto_conf directory
autoConfFiles, err := ioutil.ReadDir(filepath.Join(oldConfigDir, "conf.d", "auto_conf"))
autoConfFiles, err := os.ReadDir(filepath.Join(oldConfigDir, "conf.d", "auto_conf"))
if err != nil {
if os.IsNotExist(err) {
fmt.Fprintln(color.Output,
@@ -187,13 +186,13 @@ func ImportConfig(oldConfigDir string, newConfigDir string, force bool) error {
}

// Transform if needed AD configuration
input, err := ioutil.ReadFile(dst)
input, err := os.ReadFile(dst)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to open %s", dst)
continue
}
output := strings.Replace(string(input), "docker_images:", "ad_identifiers:", 1)
err = ioutil.WriteFile(dst, []byte(output), 0640)
err = os.WriteFile(dst, []byte(output), 0640)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to write %s", dst)
continue
@@ -241,7 +240,7 @@ func copyFile(src, dst string, overwrite bool, transformations []TransformationF
return err
}

data, err := ioutil.ReadFile(src)
data, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("unable to read file %s : %s", src, err)
}
@@ -253,7 +252,7 @@ func copyFile(src, dst string, overwrite bool, transformations []TransformationF
}
}

ioutil.WriteFile(dst, data, 0640) //nolint:errcheck
os.WriteFile(dst, data, 0640) //nolint:errcheck

ddGroup, errGroup := user.LookupGroup("dd-agent")
ddUser, errUser := user.LookupId("dd-agent")
8 changes: 4 additions & 4 deletions cmd/agent/common/import_test.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
package common

import (
"io/ioutil"
"os"
"path"
"strconv"
"strings"
@@ -109,7 +109,7 @@ type paramMatcher struct {
}

func validateSelectedParameters(t *testing.T, migratedConfigFile, oldConfigFile string) {
migratedBytes, err := ioutil.ReadFile(migratedConfigFile)
migratedBytes, err := os.ReadFile(migratedConfigFile)
require.NoError(t, err, "Failed to read"+migratedConfigFile)
migratedConf := make(map[string]interface{})
yaml.Unmarshal(migratedBytes, migratedConf)
@@ -172,12 +172,12 @@ func toInt(val interface{}) interface{} {
}

func assertYAMLEquality(t *testing.T, f1, f2 string) {
f1Bytes, err := ioutil.ReadFile(f1)
f1Bytes, err := os.ReadFile(f1)
require.NoError(t, err, "Failed to read "+f1)
migratedContent := make(map[string]interface{})
yaml.Unmarshal(f1Bytes, migratedContent)

f2Bytes, err := ioutil.ReadFile(f2)
f2Bytes, err := os.ReadFile(f2)
require.NoError(t, err, "Failed to read "+f2)
expectedContent := make(map[string]interface{})
yaml.Unmarshal(f2Bytes, expectedContent)
8 changes: 4 additions & 4 deletions cmd/agent/gui/agent.go
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
@@ -105,7 +105,7 @@ func getLog(w http.ResponseWriter, r *http.Request) {
logFile = common.DefaultLogFile
}

logFileContents, e := ioutil.ReadFile(logFile)
logFileContents, e := os.ReadFile(logFile)
if e != nil {
w.Write([]byte("Error: " + e.Error()))
return
@@ -203,7 +203,7 @@ func getConfigSetting(w http.ResponseWriter, r *http.Request) {
// Sends the configuration (aka datadog.yaml) file
func getConfigFile(w http.ResponseWriter, r *http.Request) {
path := config.Datadog.ConfigFileUsed()
settings, e := ioutil.ReadFile(path)
settings, e := os.ReadFile(path)
if e != nil {
w.Write([]byte("Error: " + e.Error()))
return
@@ -230,7 +230,7 @@ func setConfigFile(w http.ResponseWriter, r *http.Request) {
}

path := config.Datadog.ConfigFileUsed()
e = ioutil.WriteFile(path, data, 0644)
e = os.WriteFile(path, data, 0644)
if e != nil {
w.Write([]byte("Error: " + e.Error()))
return
19 changes: 9 additions & 10 deletions cmd/agent/gui/checks.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"html"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@@ -200,7 +199,7 @@ func getCheckConfigFile(w http.ResponseWriter, r *http.Request) {
log.Errorf("Error: Unable to join config path with the file name: %s", fileName)
continue
}
file, e = ioutil.ReadFile(filePath)
file, e = os.ReadFile(filePath)
if e == nil {
break
}
@@ -267,7 +266,7 @@ func setCheckConfigFile(w http.ResponseWriter, r *http.Request) {
return
}
os.MkdirAll(checkConfFolderPath, os.FileMode(0755)) //nolint:errcheck
e = ioutil.WriteFile(path, data, 0600)
e = os.WriteFile(path, data, 0600)

// If the write didn't work, try writing to the default checks directory
if e != nil && strings.Contains(e.Error(), "no such file or directory") {
@@ -277,7 +276,7 @@ func setCheckConfigFile(w http.ResponseWriter, r *http.Request) {
return
}
os.MkdirAll(defaultCheckConfFolderPath, os.FileMode(0755)) //nolint:errcheck
e = ioutil.WriteFile(path, data, 0600)
e = os.WriteFile(path, data, 0600)
}

if e != nil {
@@ -341,13 +340,13 @@ func getWheelsChecks() ([]string, error) {
func listChecks(w http.ResponseWriter, r *http.Request) {
integrations := []string{}
for _, path := range checkPaths {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
continue
}

for _, file := range files {
if ext := filepath.Ext(file.Name()); ext == ".py" && file.Mode().IsRegular() {
if ext := filepath.Ext(file.Name()); ext == ".py" && file.Type().IsRegular() {
integrations = append(integrations, file.Name())
}
}
@@ -442,7 +441,7 @@ func listConfigs(w http.ResponseWriter, r *http.Request) {
// Helper function which returns all the filenames in a check config directory
func readConfDir(path string) ([]string, error) {
var filenames []string
entries, err := ioutil.ReadDir(path)
entries, err := os.ReadDir(path)
if err != nil {
return filenames, err
}
@@ -454,10 +453,10 @@ func readConfDir(path string) ([]string, error) {
continue
}

subEntries, err := ioutil.ReadDir(filepath.Join(path, entry.Name()))
subEntries, err := os.ReadDir(filepath.Join(path, entry.Name()))
if err == nil {
for _, subEntry := range subEntries {
if hasRightEnding(subEntry.Name()) && subEntry.Mode().IsRegular() {
if hasRightEnding(subEntry.Name()) && subEntry.Type().IsRegular() {
// Save the full path of the config file {check_name.d}/{filename}
filenames = append(filenames, entry.Name()+"/"+subEntry.Name())
}
@@ -466,7 +465,7 @@ func readConfDir(path string) ([]string, error) {
continue
}

if hasRightEnding(entry.Name()) && entry.Mode().IsRegular() {
if hasRightEnding(entry.Name()) && entry.Type().IsRegular() {
filenames = append(filenames, entry.Name())
}
}
4 changes: 2 additions & 2 deletions cmd/agent/gui/gui.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"io"
"mime"
"net"
"net/http"
@@ -222,7 +222,7 @@ func authorizePOST(w http.ResponseWriter, r *http.Request, next http.HandlerFunc
// Helper function which unmarshals a POST requests data into a Payload object
func parseBody(r *http.Request) (Payload, error) {
var p Payload
body, e := ioutil.ReadAll(r.Body)
body, e := io.ReadAll(r.Body)
if e != nil {
return p, e
}
4 changes: 2 additions & 2 deletions cmd/agent/subcommands/dogstatsdcapture/command.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import (
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"io"
"time"

"go.uber.org/fx"
@@ -72,7 +72,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
dogstatsdCaptureCmd.Flags().BoolVarP(&cliParams.dsdCaptureCompressed, "compressed", "z", true, "Should capture be zstd compressed.")

// shut up grpc client!
grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard))

return []*cobra.Command{dogstatsdCaptureCmd}
}
3 changes: 1 addition & 2 deletions cmd/agent/subcommands/dogstatsdstats/command.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"

"go.uber.org/fx"
@@ -132,7 +131,7 @@ func requestDogstatsdStats(log log.Component, config config.Component, cliParams
}
}

if err := ioutil.WriteFile(cliParams.dsdStatsFilePath, []byte(s), 0644); err != nil {
if err := os.WriteFile(cliParams.dsdStatsFilePath, []byte(s), 0644); err != nil {
fmt.Println("Error while writing the file (is the location writable by the dd-agent user?):", err)
} else {
fmt.Println("Dogstatsd stats written in:", cliParams.dsdStatsFilePath)
9 changes: 4 additions & 5 deletions cmd/agent/subcommands/integrations/command.go
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@@ -699,7 +698,7 @@ func validateRequirement(version *semver.Version, comp string, versionReq *semve
}

func minAllowedVersion(integration string) (*semver.Version, bool, error) {
lines, err := ioutil.ReadFile(reqAgentReleasePath)
lines, err := os.ReadFile(reqAgentReleasePath)
if err != nil {
return nil, false, err
}
@@ -795,7 +794,7 @@ func moveConfigurationFilesOf(cliParams *cliParams, integration string) error {
}

func moveConfigurationFiles(srcFolder string, dstFolder string) error {
files, err := ioutil.ReadDir(srcFolder)
files, err := os.ReadDir(srcFolder)
if err != nil {
return err
}
@@ -825,12 +824,12 @@ func moveConfigurationFiles(srcFolder string, dstFolder string) error {
}
src := filepath.Join(srcFolder, filename)
dst := filepath.Join(dstFolder, filename)
srcContent, err := ioutil.ReadFile(src)
srcContent, err := os.ReadFile(src)
if err != nil {
errorMsg = fmt.Sprintf("%s\nError reading configuration file %s: %v", errorMsg, src, err)
continue
}
err = ioutil.WriteFile(dst, srcContent, 0644)
err = os.WriteFile(dst, srcContent, 0644)
if err != nil {
errorMsg = fmt.Sprintf("%s\nError writing configuration file %s: %v", errorMsg, dst, err)
continue
5 changes: 2 additions & 3 deletions cmd/agent/subcommands/integrations/integrations_test.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@
package integrations

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -29,15 +28,15 @@ func TestMoveConfigurationsFiles(t *testing.T) {
assert.NoError(t, f.Close())
}

filesCreated, _ := ioutil.ReadDir(srcFolder)
filesCreated, _ := os.ReadDir(srcFolder)
assert.Equal(t, 5, len(filesCreated))
for _, file := range filesCreated {
assert.Contains(t, append(yamlFiles, otherFile), file.Name())
}

moveConfigurationFiles(srcFolder, dstFolder)

filesMoved, _ := ioutil.ReadDir(dstFolder)
filesMoved, _ := os.ReadDir(dstFolder)
assert.Equal(t, 4, len(filesMoved))
for _, file := range filesMoved {
assert.Contains(t, yamlFiles, file.Name())
5 changes: 2 additions & 3 deletions cmd/agent/subcommands/status/command.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
@@ -175,7 +174,7 @@ func requestStatus(config config.Component, cliParams *cliParams) error {
}

if cliParams.statusFilePath != "" {
ioutil.WriteFile(cliParams.statusFilePath, []byte(s), 0644) //nolint:errcheck
os.WriteFile(cliParams.statusFilePath, []byte(s), 0644) //nolint:errcheck
} else {
fmt.Println(s)
}
@@ -238,7 +237,7 @@ func componentStatus(config config.Component, cliParams *cliParams, component st
}

if cliParams.statusFilePath != "" {
ioutil.WriteFile(cliParams.statusFilePath, []byte(s), 0644) //nolint:errcheck
os.WriteFile(cliParams.statusFilePath, []byte(s), 0644) //nolint:errcheck
} else {
fmt.Println(s)
}
Loading
Oops, something went wrong.

0 comments on commit afff598

Please sign in to comment.