Skip to content

Commit

Permalink
replace strings.SplitN(..., 2) by strings.Cut
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys committed Dec 18, 2023
1 parent 526c329 commit 5cdd88c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 37 deletions.
8 changes: 4 additions & 4 deletions cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error

droneEnv := make(map[string]string)
for _, env := range c.StringSlice("env") {
envs := strings.SplitN(env, "=", 2)
droneEnv[envs[0]] = envs[1]
if _, exists := environ[envs[0]]; exists {
before, after, _ := strings.Cut(env, "=")
droneEnv[before] = after
if _, exists := environ[before]; exists {
// don't override existing values
continue
}
environ[envs[0]] = envs[1]
environ[before] = before
}

tmpl, err := envsubst.ParseFile(file)
Expand Down
6 changes: 3 additions & 3 deletions cli/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ func ParseRepo(client woodpecker.Client, str string) (repoID int64, err error) {
func ParseKeyPair(p []string) map[string]string {
params := map[string]string{}
for _, i := range p {
parts := strings.SplitN(i, "=", 2)
if len(parts) != 2 { //nolint:gomnd
before, after, ok := strings.Cut(i, "=")
if !ok || before == "" {
continue
}
params[parts[0]] = parts[1]
params[before] = after
}
return params
}
6 changes: 3 additions & 3 deletions cli/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func pipelineCreate(c *cli.Context) error {
variables := make(map[string]string)

for _, vaz := range c.StringSlice("var") {
sp := strings.SplitN(vaz, "=", 2)
if len(sp) == 2 { //nolint:gomnd
variables[sp[0]] = sp[1]
before, after, _ := strings.Cut(vaz, "=")
if before != "" && after != "" {
variables[before] = after
}
}

Expand Down
13 changes: 6 additions & 7 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,13 @@ func stringSliceAddToMap(sl []string, m map[string]string) error {
if m == nil {
m = make(map[string]string)
}
//nolint: gomnd
for _, v := range sl {
parts := strings.SplitN(v, "=", 2)
switch len(parts) {
case 2:
m[parts[0]] = parts[1]
case 1:
return fmt.Errorf("key '%s' does not have a value assigned", parts[0])
before, after, _ := strings.Cut(v, "=")
switch {
case before != "" && after != "":
m[before] = after
case after != "":
return fmt.Errorf("key '%s' does not have a value assigned", before)
default:
return fmt.Errorf("empty string in slice")
}
Expand Down
6 changes: 3 additions & 3 deletions pipeline/frontend/metadata/drone_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ DRONE_TARGET_BRANCH=main`
func convertListToEnvMap(t *testing.T, list string) map[string]string {
result := make(map[string]string)
for _, s := range strings.Split(list, "\n") {
ss := strings.SplitN(strings.TrimSpace(s), "=", 2)
if len(ss) != 2 {
before, after, _ := strings.Cut(strings.TrimSpace(s), "=")
if before == "" || after == "" {
t.Fatal("helper function got invalid test data")
}
result[ss[0]] = ss[1]
result[before] = after
}
return result
}
11 changes: 2 additions & 9 deletions pipeline/frontend/yaml/types/base/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ func (s *SliceOrMap) UnmarshalYAML(unmarshal func(any) error) error {
parts := map[string]string{}
for _, s := range sliceType {
if str, ok := s.(string); ok {
str := strings.TrimSpace(str)
keyValueSlice := strings.SplitN(str, "=", 2)

key := keyValueSlice[0]
val := ""
if len(keyValueSlice) == 2 { //nolint: gomnd
val = keyValueSlice[1]
}
parts[key] = val
before, after, _ := strings.Cut(strings.TrimSpace(str), "=")
parts[before] = after
} else {
return fmt.Errorf("cannot unmarshal '%v' of type %T into a string value", s, s)
}
Expand Down
8 changes: 4 additions & 4 deletions server/plugins/environments/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func Parse(params []string) model.EnvironService {
var globals []*model.Environ

for _, item := range params {
kvPair := strings.SplitN(item, ":", 2)
if len(kvPair) != 2 {
before, after, _ := strings.Cut(item, ":")
if after != "" {
// ignore items only containing a key and no value
log.Warn().Msgf("key '%s' has no value, will be ignored", kvPair[0])
log.Warn().Msgf("key '%s' has no value, will be ignored", before)
continue
}
globals = append(globals, &model.Environ{Name: kvPair[0], Value: kvPair[1]})
globals = append(globals, &model.Environ{Name: before, Value: after})
}
return &builtin{globals}
}
Expand Down
8 changes: 4 additions & 4 deletions server/plugins/registry/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func decodeAuth(authStr string) (string, string, error) {
if n > decLen {
return "", "", fmt.Errorf("something went wrong decoding auth config")
}
arr := strings.SplitN(string(decoded), ":", 2)
if len(arr) != 2 { //nolint:gomnd
before, after, _ := strings.Cut(string(decoded), ":")
if before == "" || after == "" {
return "", "", fmt.Errorf("invalid auth configuration file")
}
password := strings.Trim(arr[1], "\x00")
return arr[0], password, nil
password := strings.Trim(after, "\x00")
return before, password, nil
}

0 comments on commit 5cdd88c

Please sign in to comment.