From ecf801fcd6e6ae219e091d1d00f708b76d6e9c3c Mon Sep 17 00:00:00 2001 From: jannfis Date: Fri, 1 Jul 2022 07:31:31 +0000 Subject: [PATCH] chore: Replace deprecated ioutil in CLI code 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 a706388fc02cf..f7855914ce5ae 100644 --- a/cmd/argocd-dex/commands/argocd_dex.go +++ b/cmd/argocd-dex/commands/argocd_dex.go @@ -3,7 +3,6 @@ package commands import ( "context" "fmt" - "io/ioutil" "os" "os/exec" "syscall" @@ -83,7 +82,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") @@ -172,7 +171,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 6073daa33f22f..59adcef5ad90b 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" @@ -301,7 +300,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(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 bd7990edf0cce..e8661520f9def 100644 --- a/cmd/argocd/commands/admin/backup.go +++ b/cmd/argocd/commands/admin/backup.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "github.com/argoproj/gitops-engine/pkg/utils/kube" @@ -136,9 +135,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 d6e3ae9339e30..6a881e769de51 100644 --- a/cmd/argocd/commands/admin/repo.go +++ b/cmd/argocd/commands/admin/repo.go @@ -3,7 +3,6 @@ package admin import ( "context" "fmt" - "io/ioutil" "os" log "github.com/sirupsen/logrus" @@ -86,13 +85,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) } } @@ -107,9 +106,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 83fd57b4acba0..3f5a431576364 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() (*settings.SettingsManager, er 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() (*settings.SettingsManager, er 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 } @@ -362,7 +361,7 @@ func NewResourceOverridesCommand(cmdCtx commandContext) *cobra.Command { } func executeResourceOverrideCommand(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 ae58c7a34bc5e..93e85eeb42500 100644 --- a/cmd/argocd/commands/admin/settings_rbac_test.go +++ b/cmd/argocd/commands/admin/settings_rbac_test.go @@ -1,7 +1,7 @@ package admin import ( - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -54,7 +54,7 @@ func Test_PolicyFromYAML(t *testing.T) { } func Test_PolicyFromK8s(t *testing.T) { - data, err := ioutil.ReadFile("testdata/rbac/policy.csv") + data, err := os.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 9f5da58a42725..3905fd3c26690 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 @@ -231,7 +230,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 3cdc579924a65..bfe7d5869b7f5 100644 --- a/cmd/argocd/commands/gpg.go +++ b/cmd/argocd/commands/gpg.go @@ -3,7 +3,6 @@ package commands import ( "context" "fmt" - "io/ioutil" "os" "strings" "text/tabwriter" @@ -111,7 +110,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 37a7c46ffdf34..ec4b672055689 100644 --- a/cmd/argocd/commands/repo.go +++ b/cmd/argocd/commands/repo.go @@ -3,7 +3,6 @@ package commands import ( "context" "fmt" - "io/ioutil" "os" "text/tabwriter" @@ -90,7 +89,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) } @@ -111,9 +110,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) @@ -126,7 +125,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 0386047ed16bf..c0411ccaed1af 100644 --- a/cmd/argocd/commands/repocreds.go +++ b/cmd/argocd/commands/repocreds.go @@ -3,7 +3,6 @@ package commands import ( "context" "fmt" - "io/ioutil" "os" "text/tabwriter" @@ -82,7 +81,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) } @@ -103,9 +102,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) @@ -118,7 +117,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 {