Skip to content

Commit 9e62b9f

Browse files
committed
refactor: replace strings.Replace with strings.ReplaceAll
strings.ReplaceAll(s, old, new) is a wrapper function for strings.Replace(s, old, new, -1). But strings.ReplaceAll is more readable and removes the hardcoded -1. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
1 parent 1ff15dd commit 9e62b9f

17 files changed

Lines changed: 53 additions & 46 deletions

File tree

cmd/run.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"os"
8+
"strings"
9+
610
"github.com/loft-sh/devspace/pkg/devspace/config"
711
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
812
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
@@ -13,10 +17,7 @@ import (
1317
"github.com/loft-sh/devspace/pkg/util/exit"
1418
"github.com/loft-sh/devspace/pkg/util/interrupt"
1519
"github.com/loft-sh/devspace/pkg/util/log"
16-
"io"
1720
"mvdan.cc/sh/v3/interp"
18-
"os"
19-
"strings"
2021

2122
"github.com/loft-sh/devspace/cmd/flags"
2223
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
@@ -284,7 +285,7 @@ func ExecuteCommand(ctx context.Context, cmd *latest.CommandConfig, variables ma
284285
if appendArgs {
285286
// Append args to shell command
286287
for _, arg := range args {
287-
arg = strings.Replace(arg, "'", "'\"'\"'", -1)
288+
arg = strings.ReplaceAll(arg, "'", "'\"'\"'")
288289

289290
shellCommand += " '" + arg + "'"
290291
}

e2e/tests/deploy/deploy.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ package deploy
22

33
import (
44
"context"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
510
"github.com/loft-sh/devspace/cmd"
611
"github.com/loft-sh/devspace/cmd/flags"
712
"github.com/loft-sh/devspace/e2e/framework"
813
"github.com/loft-sh/devspace/e2e/kube"
914
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
1015
"github.com/loft-sh/devspace/pkg/util/factory"
1116
"github.com/onsi/ginkgo"
12-
"io/ioutil"
1317
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14-
"os"
15-
"path/filepath"
16-
"strings"
1718
)
1819

1920
var _ = DevSpaceDescribe("deploy", func() {
@@ -107,8 +108,8 @@ var _ = DevSpaceDescribe("deploy", func() {
107108
out, err := ioutil.ReadFile(manifests)
108109
framework.ExpectNoError(err)
109110

110-
data := strings.Replace(string(out), "###NAMESPACE1###", ns, -1)
111-
data = strings.Replace(data, "###NAMESPACE2###", ns2, -1)
111+
data := strings.ReplaceAll(string(out), "###NAMESPACE1###", ns)
112+
data = strings.ReplaceAll(data, "###NAMESPACE2###", ns2)
112113

113114
err = ioutil.WriteFile(manifests, []byte(data), 0777)
114115
framework.ExpectNoError(err)

e2e/tests/replacepods/replacepods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var _ = DevSpaceDescribe("replacepods", func() {
135135
fileContents, err := ioutil.ReadFile("devspace.yaml")
136136
framework.ExpectNoError(err)
137137

138-
newString := strings.Replace(string(fileContents), "ubuntu:18.04", "alpine:3.14", -1)
138+
newString := strings.ReplaceAll(string(fileContents), "ubuntu:18.04", "alpine:3.14")
139139
err = ioutil.WriteFile("devspace.yaml", []byte(newString), 0666)
140140
framework.ExpectNoError(err)
141141

@@ -237,7 +237,7 @@ var _ = DevSpaceDescribe("replacepods", func() {
237237
fileContents, err := ioutil.ReadFile("devspace.yaml")
238238
framework.ExpectNoError(err)
239239

240-
newString := strings.Replace(string(fileContents), "ubuntu:18.04", "alpine:3.14", -1)
240+
newString := strings.ReplaceAll(string(fileContents), "ubuntu:18.04", "alpine:3.14")
241241
err = ioutil.WriteFile("devspace.yaml", []byte(newString), 0666)
242242
framework.ExpectNoError(err)
243243

helper/server/tar.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package server
33
import (
44
"archive/tar"
55
"compress/gzip"
6-
"github.com/loft-sh/devspace/pkg/util/fsutil"
76
"io"
87
"io/ioutil"
98
"os"
@@ -13,6 +12,8 @@ import (
1312
"strings"
1413
"time"
1514

15+
"github.com/loft-sh/devspace/pkg/util/fsutil"
16+
1617
"github.com/pkg/errors"
1718
)
1819

@@ -275,7 +276,7 @@ func tarFile(basePath string, fileInformation *fileInformation, writtenFiles map
275276
}
276277

277278
func getRelativeFromFullPath(fullpath string, prefix string) string {
278-
return strings.TrimPrefix(strings.Replace(strings.Replace(fullpath[len(prefix):], "\\", "/", -1), "//", "/", -1), ".")
279+
return strings.TrimPrefix(strings.ReplaceAll(strings.ReplaceAll(fullpath[len(prefix):], "\\", "/"), "//", "/"), ".")
279280
}
280281

281282
func createFileInformationFromStat(relativePath string, stat os.FileInfo) *fileInformation {

pkg/devspace/config/loader/loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,7 @@ profiles:
21062106
// Execute test cases
21072107
for index, testCase := range testCases {
21082108
testMap := map[string]interface{}{}
2109-
err := yaml.Unmarshal([]byte(strings.Replace(testCase.in.config, " ", " ", -1)), &testMap)
2109+
err := yaml.Unmarshal([]byte(strings.ReplaceAll(testCase.in.config, " ", " ")), &testMap)
21102110
if err != nil {
21112111
t.Fatal(err)
21122112
}

pkg/devspace/config/loader/variable/legacy/replace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package legacy
22

33
import (
44
"fmt"
5-
"github.com/loft-sh/devspace/pkg/util/dockerfile"
65
"regexp"
76
"strings"
87

98
buildtypes "github.com/loft-sh/devspace/pkg/devspace/build/types"
109
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
1110
"github.com/loft-sh/devspace/pkg/devspace/imageselector"
11+
"github.com/loft-sh/devspace/pkg/util/dockerfile"
1212

1313
config2 "github.com/loft-sh/devspace/pkg/devspace/config"
1414
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
@@ -132,7 +132,7 @@ func resolveImage(value string, config config2.Config, dependencies []types.Depe
132132

133133
// does the config have a tag defined?
134134
if tag == "" && len(configImage.Tags) > 0 {
135-
tag = strings.Replace(configImage.Tags[0], "#", "x", -1)
135+
tag = strings.ReplaceAll(configImage.Tags[0], "#", "x")
136136
}
137137

138138
// only return the tag

pkg/devspace/config/loader/variable/resolver.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package variable
33
import (
44
"context"
55
"fmt"
6+
"path/filepath"
7+
"regexp"
8+
"strings"
9+
610
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression"
711
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/runtime"
812
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
913
"github.com/loft-sh/devspace/pkg/devspace/dependency/graph"
10-
"path/filepath"
11-
"regexp"
12-
"strings"
1314

1415
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
1516
"github.com/loft-sh/devspace/pkg/devspace/deploy/deployer/kubectl/walk"
@@ -233,8 +234,8 @@ func (r *resolver) insertVariableGraph(g *graph.Graph, node *latest.Variable) er
233234
func (r *resolver) FillVariablesInclude(ctx context.Context, haystack interface{}, includedPaths []string) (interface{}, error) {
234235
paths := []*regexp.Regexp{}
235236
for _, path := range includedPaths {
236-
path = strings.Replace(path, "*", "[^/]+", -1)
237-
path = strings.Replace(path, "**", ".+", -1)
237+
path = strings.ReplaceAll(path, "*", "[^/]+")
238+
path = strings.ReplaceAll(path, "**", ".+")
238239
path = "^" + path
239240
expr, err := regexp.Compile(path)
240241
if err != nil {
@@ -263,8 +264,8 @@ func (r *resolver) FillVariablesInclude(ctx context.Context, haystack interface{
263264
func (r *resolver) FillVariablesExclude(ctx context.Context, haystack interface{}, excludedPaths []string) (interface{}, error) {
264265
paths := []*regexp.Regexp{}
265266
for _, path := range excludedPaths {
266-
path = strings.Replace(path, "*", "[^/]+", -1)
267-
path = strings.Replace(path, "**", ".+", -1)
267+
path = strings.ReplaceAll(path, "*", "[^/]+")
268+
path = strings.ReplaceAll(path, "**", ".+")
268269
path = "^" + path
269270
expr, err := regexp.Compile(path)
270271
if err != nil {

pkg/devspace/config/loader/variable/runtime/runtime_variable.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package runtime
22

33
import (
44
"fmt"
5+
"strings"
6+
57
buildtypes "github.com/loft-sh/devspace/pkg/devspace/build/types"
68
"github.com/loft-sh/devspace/pkg/devspace/config"
79
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
810
"github.com/loft-sh/devspace/pkg/devspace/dependency/types"
911
"github.com/pkg/errors"
10-
"strings"
1112
)
1213

1314
var Locations = []string{
@@ -181,7 +182,7 @@ func BuildImageString(c config.Config, name string, fallbackImage string, fallba
181182
if imageCache.Tag != "" {
182183
tag = imageCache.Tag
183184
} else if fallbackTag != "" {
184-
tag = strings.Replace(fallbackTag, "#", "x", -1)
185+
tag = strings.ReplaceAll(fallbackTag, "#", "x")
185186
}
186187

187188
// only return the tag

pkg/devspace/config/versions/v1beta11/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func (c *Config) Upgrade(log log.Logger) (config.Config, error) {
388388
devPipeline := deployPipeline + "\n" + "start_dev --all" + "\n"
389389
if c.Dev.Terminal != nil && c.Dev.Terminal.ImageSelector == "" && len(c.Dev.Terminal.LabelSelector) == 0 && len(c.Dev.Terminal.Command) > 0 {
390390
for _, c := range c.Dev.Terminal.Command {
391-
devPipeline += "'" + strings.Replace(c, "'", "'\"'\"'", -1) + "' "
391+
devPipeline += "'" + strings.ReplaceAll(c, "'", "'\"'\"'") + "' "
392392
}
393393

394394
devPipeline += "\n"

pkg/devspace/deploy/deployer/kubectl/kubectl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func New(ctx devspacecontext.Context, deployConfig *latest.DeploymentConfig) (de
6262

6363
manifests := []string{}
6464
for _, ptrManifest := range deployConfig.Kubectl.Manifests {
65-
manifest := strings.Replace(ptrManifest, "*", "", -1)
65+
manifest := strings.ReplaceAll(ptrManifest, "*", "")
6666
if deployConfig.Kubectl.Kustomize != nil && *deployConfig.Kubectl.Kustomize {
6767
manifest = strings.TrimSuffix(manifest, "kustomization.yaml")
6868
}

0 commit comments

Comments
 (0)