Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Replace deprecated ioutil in CLI code #9846

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/argocd-dex/commands/argocd_dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"syscall"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cmd/argocd/commands/admin/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"sort"
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions cmd/argocd/commands/admin/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
Expand Down Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions cmd/argocd/commands/admin/generatespec_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package admin
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

Expand All @@ -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")
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/argocd/commands/admin/project_allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/argocd/commands/admin/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package admin

import (
"fmt"
"io/ioutil"
"os"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions cmd/argocd/commands/admin/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions cmd/argocd/commands/admin/settings_rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package admin

import (
"context"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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{
Expand Down
5 changes: 2 additions & 3 deletions cmd/argocd/commands/admin/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/argocd/commands/common_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package commands

import (
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions cmd/argocd/commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
},
Expand Down
3 changes: 1 addition & 2 deletions cmd/argocd/commands/context_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions cmd/argocd/commands/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/argocd/commands/logout_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"io/ioutil"
"os"
"testing"

Expand All @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions cmd/argocd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"text/tabwriter"

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
Loading