From 2d6ce088acd4fb29271ffb6f6023dbb27594d59b Mon Sep 17 00:00:00 2001 From: jannfis Date: Thu, 7 Jul 2022 22:52:14 +0200 Subject: [PATCH] chore: Replace deprecated ioutil in CLI code (#9846) Signed-off-by: jannfis --- cmd/argocd-dex/commands/argocd_dex.go | 5 ++--- cmd/argocd/commands/admin/app.go | 3 +-- cmd/argocd/commands/admin/backup.go | 5 ++--- .../commands/admin/generatespec_utils_test.go | 13 +++++-------- cmd/argocd/commands/admin/project_allowlist.go | 3 +-- cmd/argocd/commands/admin/repo.go | 9 ++++----- cmd/argocd/commands/admin/settings.go | 7 +++---- cmd/argocd/commands/admin/settings_rbac_test.go | 4 ++-- cmd/argocd/commands/admin/settings_test.go | 5 ++--- cmd/argocd/commands/common_test.go | 4 ++-- cmd/argocd/commands/context.go | 5 ++--- cmd/argocd/commands/context_test.go | 3 +-- cmd/argocd/commands/gpg.go | 3 +-- cmd/argocd/commands/logout_test.go | 3 +-- cmd/argocd/commands/repo.go | 9 ++++----- cmd/argocd/commands/repocreds.go | 9 ++++----- 16 files changed, 37 insertions(+), 53 deletions(-) diff --git a/cmd/argocd-dex/commands/argocd_dex.go b/cmd/argocd-dex/commands/argocd_dex.go index 39644e9227513..42bf038db722f 100644 --- a/cmd/argocd-dex/commands/argocd_dex.go +++ b/cmd/argocd-dex/commands/argocd_dex.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "io/ioutil" "os" "os/exec" "syscall" @@ -84,7 +83,7 @@ func NewRunDexCommand() *cobra.Command { if len(dexCfgBytes) == 0 { log.Infof("dex is not configured") } else { - err = ioutil.WriteFile("/tmp/dex.yaml", dexCfgBytes, 0644) + err = os.WriteFile("/tmp/dex.yaml", dexCfgBytes, 0644) errors.CheckError(err) log.Debug(redactor(string(dexCfgBytes))) cmd = exec.Command("dex", "serve", "/tmp/dex.yaml") @@ -175,7 +174,7 @@ func NewGenDexConfigCommand() *cobra.Command { errors.CheckError(err) fmt.Print(string(maskedDexCfgBytes)) } else { - err = ioutil.WriteFile(out, dexCfgBytes, 0644) + err = os.WriteFile(out, dexCfgBytes, 0644) errors.CheckError(err) } return nil diff --git a/cmd/argocd/commands/admin/app.go b/cmd/argocd/commands/admin/app.go index 83ad78b61605e..99a798976fc02 100644 --- a/cmd/argocd/commands/admin/app.go +++ b/cmd/argocd/commands/admin/app.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "sort" @@ -303,7 +302,7 @@ func saveToFile(err error, outputFormat string, result reconcileResults, outputP return fmt.Errorf("format %s is not supported", outputFormat) } - return ioutil.WriteFile(outputPath, data, 0644) + return os.WriteFile(outputPath, data, 0644) } func getReconcileResults(ctx context.Context, appClientset appclientset.Interface, namespace string, selector string) ([]appReconcileResult, error) { diff --git a/cmd/argocd/commands/admin/backup.go b/cmd/argocd/commands/admin/backup.go index f8d1543b89566..65090ccb9cf71 100644 --- a/cmd/argocd/commands/admin/backup.go +++ b/cmd/argocd/commands/admin/backup.go @@ -4,7 +4,6 @@ import ( "bufio" "fmt" "io" - "io/ioutil" "os" "github.com/argoproj/gitops-engine/pkg/utils/kube" @@ -139,9 +138,9 @@ func NewImportCommand() *cobra.Command { var input []byte if in := args[0]; in == "-" { - input, err = ioutil.ReadAll(os.Stdin) + input, err = io.ReadAll(os.Stdin) } else { - input, err = ioutil.ReadFile(in) + input, err = os.ReadFile(in) } errors.CheckError(err) var dryRunMsg string diff --git a/cmd/argocd/commands/admin/generatespec_utils_test.go b/cmd/argocd/commands/admin/generatespec_utils_test.go index 4352ce331b333..ea71b1ffa76ae 100644 --- a/cmd/argocd/commands/admin/generatespec_utils_test.go +++ b/cmd/argocd/commands/admin/generatespec_utils_test.go @@ -3,7 +3,6 @@ package admin import ( "bytes" "fmt" - "io/ioutil" "os" "testing" @@ -24,19 +23,17 @@ func TestGetOutWriter_InlineOff(t *testing.T) { } func TestGetOutWriter_InlineOn(t *testing.T) { - tmpFile, err := ioutil.TempFile("", "") - require.NoError(t, err) + tmpFile := t.TempDir() defer func() { - _ = os.Remove(tmpFile.Name()) - _ = os.Remove(fmt.Sprintf("%s.back", tmpFile.Name())) + _ = os.Remove(fmt.Sprintf("%s.back", tmpFile)) }() - out, closer, err := getOutWriter(true, tmpFile.Name()) + out, closer, err := getOutWriter(true, tmpFile) require.NoError(t, err) defer io.Close(closer) - assert.Equal(t, tmpFile.Name(), out.(*os.File).Name()) - _, err = os.Stat(fmt.Sprintf("%s.back", tmpFile.Name())) + assert.Equal(t, tmpFile, out.(*os.File).Name()) + _, err = os.Stat(fmt.Sprintf("%s.back", tmpFile)) assert.NoError(t, err, "Back file must be created") } diff --git a/cmd/argocd/commands/admin/project_allowlist.go b/cmd/argocd/commands/admin/project_allowlist.go index 0860e62d0b777..1825a9a1e283f 100644 --- a/cmd/argocd/commands/admin/project_allowlist.go +++ b/cmd/argocd/commands/admin/project_allowlist.go @@ -4,7 +4,6 @@ import ( "bufio" "fmt" "io" - "io/ioutil" "os" "strings" @@ -99,7 +98,7 @@ func getResourceList(clientConfig clientcmd.ClientConfig) ([]*metav1.APIResource } func generateProjectAllowList(serverResources []*metav1.APIResourceList, clusterRoleFileName string, projName string) (*v1alpha1.AppProject, error) { - yamlBytes, err := ioutil.ReadFile(clusterRoleFileName) + yamlBytes, err := os.ReadFile(clusterRoleFileName) if err != nil { return nil, fmt.Errorf("error reading cluster role file: %s", err) } diff --git a/cmd/argocd/commands/admin/repo.go b/cmd/argocd/commands/admin/repo.go index b5375e29a8d0b..208a6ef8550f8 100644 --- a/cmd/argocd/commands/admin/repo.go +++ b/cmd/argocd/commands/admin/repo.go @@ -2,7 +2,6 @@ package admin import ( "fmt" - "io/ioutil" "os" log "github.com/sirupsen/logrus" @@ -87,13 +86,13 @@ func NewGenRepoSpecCommand() *cobra.Command { // Specifying ssh-private-key-path is only valid for SSH repositories if repoOpts.SshPrivateKeyPath != "" { if ok, _ := git.IsSSHURL(repoOpts.Repo.Repo); ok { - keyData, err := ioutil.ReadFile(repoOpts.SshPrivateKeyPath) + keyData, err := os.ReadFile(repoOpts.SshPrivateKeyPath) if err != nil { log.Fatal(err) } repoOpts.Repo.SSHPrivateKey = string(keyData) } else { - err := fmt.Errorf("--ssh-private-key-path is only supported for SSH repositories.") + err := fmt.Errorf("--ssh-private-key-path is only supported for SSH repositories") errors.CheckError(err) } } @@ -108,9 +107,9 @@ func NewGenRepoSpecCommand() *cobra.Command { // Specifying tls-client-cert-path is only valid for HTTPS repositories if repoOpts.TlsClientCertPath != "" { if git.IsHTTPSURL(repoOpts.Repo.Repo) { - tlsCertData, err := ioutil.ReadFile(repoOpts.TlsClientCertPath) + tlsCertData, err := os.ReadFile(repoOpts.TlsClientCertPath) errors.CheckError(err) - tlsCertKey, err := ioutil.ReadFile(repoOpts.TlsClientCertKeyPath) + tlsCertKey, err := os.ReadFile(repoOpts.TlsClientCertKeyPath) errors.CheckError(err) repoOpts.Repo.TLSClientCertData = string(tlsCertData) repoOpts.Repo.TLSClientCertKey = string(tlsCertKey) diff --git a/cmd/argocd/commands/admin/settings.go b/cmd/argocd/commands/admin/settings.go index 012d82f3bcc4d..9fda66fa73a43 100644 --- a/cmd/argocd/commands/admin/settings.go +++ b/cmd/argocd/commands/admin/settings.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "reflect" "sort" @@ -77,7 +76,7 @@ func (opts *settingsOpts) createSettingsManager(ctx context.Context) (*settings. return nil, err } } else { - data, err := ioutil.ReadFile(opts.argocdCMPath) + data, err := os.ReadFile(opts.argocdCMPath) if err != nil { return nil, err } @@ -90,7 +89,7 @@ func (opts *settingsOpts) createSettingsManager(ctx context.Context) (*settings. var argocdSecret *corev1.Secret if opts.argocdSecretPath != "" { - data, err := ioutil.ReadFile(opts.argocdSecretPath) + data, err := os.ReadFile(opts.argocdSecretPath) if err != nil { return nil, err } @@ -364,7 +363,7 @@ func NewResourceOverridesCommand(cmdCtx commandContext) *cobra.Command { } func executeResourceOverrideCommand(ctx context.Context, cmdCtx commandContext, args []string, callback func(res unstructured.Unstructured, override v1alpha1.ResourceOverride, overrides map[string]v1alpha1.ResourceOverride)) { - data, err := ioutil.ReadFile(args[0]) + data, err := os.ReadFile(args[0]) errors.CheckError(err) res := unstructured.Unstructured{} diff --git a/cmd/argocd/commands/admin/settings_rbac_test.go b/cmd/argocd/commands/admin/settings_rbac_test.go index 5de89236bbb9f..93601eed1d303 100644 --- a/cmd/argocd/commands/admin/settings_rbac_test.go +++ b/cmd/argocd/commands/admin/settings_rbac_test.go @@ -2,7 +2,7 @@ package admin import ( "context" - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -59,9 +59,9 @@ func Test_PolicyFromYAML(t *testing.T) { } func Test_PolicyFromK8s(t *testing.T) { + data, err := os.ReadFile("testdata/rbac/policy.csv") ctx := context.Background() - data, err := ioutil.ReadFile("testdata/rbac/policy.csv") require.NoError(t, err) kubeclientset := fake.NewSimpleClientset(&v1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ diff --git a/cmd/argocd/commands/admin/settings_test.go b/cmd/argocd/commands/admin/settings_test.go index fbd4869069d56..fed7f03afc356 100644 --- a/cmd/argocd/commands/admin/settings_test.go +++ b/cmd/argocd/commands/admin/settings_test.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "testing" @@ -35,7 +34,7 @@ func captureStdout(callback func()) (string, error) { callback() utils.Close(w) - data, err := ioutil.ReadAll(r) + data, err := io.ReadAll(r) if err != nil { return "", err @@ -235,7 +234,7 @@ spec: ) func tempFile(content string) (string, io.Closer, error) { - f, err := ioutil.TempFile("", "*.yaml") + f, err := os.CreateTemp("", "*.yaml") if err != nil { return "", nil, err } diff --git a/cmd/argocd/commands/common_test.go b/cmd/argocd/commands/common_test.go index ab8db5b0f0c35..c86429b32e0c8 100644 --- a/cmd/argocd/commands/common_test.go +++ b/cmd/argocd/commands/common_test.go @@ -1,7 +1,7 @@ package commands import ( - "io/ioutil" + "io" "os" "testing" @@ -59,7 +59,7 @@ func captureOutput(f func() error) (string, error) { os.Stdout = stdout return "", err } - str, err := ioutil.ReadAll(r) + str, err := io.ReadAll(r) os.Stdout = stdout if err != nil { return "", err diff --git a/cmd/argocd/commands/context.go b/cmd/argocd/commands/context.go index e4fcabef4cad3..ec12aa380cd58 100644 --- a/cmd/argocd/commands/context.go +++ b/cmd/argocd/commands/context.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "io/ioutil" "os" "path" "strings" @@ -50,7 +49,7 @@ func NewContextCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { prevCtxFile := path.Join(argoCDDir, ".prev-ctx") if ctxName == "-" { - prevCtxBytes, err := ioutil.ReadFile(prevCtxFile) + prevCtxBytes, err := os.ReadFile(prevCtxFile) errors.CheckError(err) ctxName = string(prevCtxBytes) } @@ -66,7 +65,7 @@ func NewContextCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { err = localconfig.WriteLocalConfig(*localCfg, clientOpts.ConfigPath) errors.CheckError(err) - err = ioutil.WriteFile(prevCtxFile, []byte(prevCtx), 0644) + err = os.WriteFile(prevCtxFile, []byte(prevCtx), 0644) errors.CheckError(err) fmt.Printf("Switched to context '%s'\n", localCfg.CurrentContext) }, diff --git a/cmd/argocd/commands/context_test.go b/cmd/argocd/commands/context_test.go index 1e0304409f582..c258485b8181f 100644 --- a/cmd/argocd/commands/context_test.go +++ b/cmd/argocd/commands/context_test.go @@ -1,7 +1,6 @@ package commands import ( - "io/ioutil" "os" "testing" @@ -40,7 +39,7 @@ const testConfigFilePath = "./testdata/local.config" func TestContextDelete(t *testing.T) { // Write the test config file - err := ioutil.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) + err := os.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) assert.NoError(t, err) defer os.Remove(testConfigFilePath) diff --git a/cmd/argocd/commands/gpg.go b/cmd/argocd/commands/gpg.go index fd4efa0864c65..7a48a915bebec 100644 --- a/cmd/argocd/commands/gpg.go +++ b/cmd/argocd/commands/gpg.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "io/ioutil" "os" "strings" "text/tabwriter" @@ -116,7 +115,7 @@ func NewGPGAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { if fromFile == "" { errors.CheckError(fmt.Errorf("--from is mandatory")) } - keyData, err := ioutil.ReadFile(fromFile) + keyData, err := os.ReadFile(fromFile) if err != nil { errors.CheckError(err) } diff --git a/cmd/argocd/commands/logout_test.go b/cmd/argocd/commands/logout_test.go index 86fca3070b690..f70992c17bb93 100644 --- a/cmd/argocd/commands/logout_test.go +++ b/cmd/argocd/commands/logout_test.go @@ -1,7 +1,6 @@ package commands import ( - "io/ioutil" "os" "testing" @@ -15,7 +14,7 @@ import ( func TestLogout(t *testing.T) { // Write the test config file - err := ioutil.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) + err := os.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) assert.NoError(t, err) defer os.Remove(testConfigFilePath) diff --git a/cmd/argocd/commands/repo.go b/cmd/argocd/commands/repo.go index 8b46b03c8e1e7..4332a24a73c90 100644 --- a/cmd/argocd/commands/repo.go +++ b/cmd/argocd/commands/repo.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "io/ioutil" "os" "text/tabwriter" @@ -91,7 +90,7 @@ func NewRepoAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { // Specifying ssh-private-key-path is only valid for SSH repositories if repoOpts.SshPrivateKeyPath != "" { if ok, _ := git.IsSSHURL(repoOpts.Repo.Repo); ok { - keyData, err := ioutil.ReadFile(repoOpts.SshPrivateKeyPath) + keyData, err := os.ReadFile(repoOpts.SshPrivateKeyPath) if err != nil { log.Fatal(err) } @@ -112,9 +111,9 @@ func NewRepoAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { // Specifying tls-client-cert-path is only valid for HTTPS repositories if repoOpts.TlsClientCertPath != "" { if git.IsHTTPSURL(repoOpts.Repo.Repo) { - tlsCertData, err := ioutil.ReadFile(repoOpts.TlsClientCertPath) + tlsCertData, err := os.ReadFile(repoOpts.TlsClientCertPath) errors.CheckError(err) - tlsCertKey, err := ioutil.ReadFile(repoOpts.TlsClientCertKeyPath) + tlsCertKey, err := os.ReadFile(repoOpts.TlsClientCertKeyPath) errors.CheckError(err) repoOpts.Repo.TLSClientCertData = string(tlsCertData) repoOpts.Repo.TLSClientCertKey = string(tlsCertKey) @@ -127,7 +126,7 @@ func NewRepoAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command { // Specifying github-app-private-key-path is only valid for HTTPS repositories if repoOpts.GithubAppPrivateKeyPath != "" { if git.IsHTTPSURL(repoOpts.Repo.Repo) { - githubAppPrivateKey, err := ioutil.ReadFile(repoOpts.GithubAppPrivateKeyPath) + githubAppPrivateKey, err := os.ReadFile(repoOpts.GithubAppPrivateKeyPath) errors.CheckError(err) repoOpts.Repo.GithubAppPrivateKey = string(githubAppPrivateKey) } else { diff --git a/cmd/argocd/commands/repocreds.go b/cmd/argocd/commands/repocreds.go index ba6e61803e63a..bfc4aa50679f0 100644 --- a/cmd/argocd/commands/repocreds.go +++ b/cmd/argocd/commands/repocreds.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "io/ioutil" "os" "text/tabwriter" @@ -83,7 +82,7 @@ func NewRepoCredsAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comma // Specifying ssh-private-key-path is only valid for SSH repositories if sshPrivateKeyPath != "" { if ok, _ := git.IsSSHURL(repo.URL); ok { - keyData, err := ioutil.ReadFile(sshPrivateKeyPath) + keyData, err := os.ReadFile(sshPrivateKeyPath) if err != nil { log.Fatal(err) } @@ -104,9 +103,9 @@ func NewRepoCredsAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comma // Specifying tls-client-cert-path is only valid for HTTPS repositories if tlsClientCertPath != "" { if git.IsHTTPSURL(repo.URL) { - tlsCertData, err := ioutil.ReadFile(tlsClientCertPath) + tlsCertData, err := os.ReadFile(tlsClientCertPath) errors.CheckError(err) - tlsCertKey, err := ioutil.ReadFile(tlsClientCertKeyPath) + tlsCertKey, err := os.ReadFile(tlsClientCertKeyPath) errors.CheckError(err) repo.TLSClientCertData = string(tlsCertData) repo.TLSClientCertKey = string(tlsCertKey) @@ -119,7 +118,7 @@ func NewRepoCredsAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comma // Specifying github-app-private-key-path is only valid for HTTPS repositories if githubAppPrivateKeyPath != "" { if git.IsHTTPSURL(repo.URL) { - githubAppPrivateKey, err := ioutil.ReadFile(githubAppPrivateKeyPath) + githubAppPrivateKey, err := os.ReadFile(githubAppPrivateKeyPath) errors.CheckError(err) repo.GithubAppPrivateKey = string(githubAppPrivateKey) } else {