Skip to content

Commit e56cb8e

Browse files
Merge pull request #5239 from serprex/strings-cut
Replace strings.SplitN with strings.Cut
2 parents 51fc7f2 + 53c65dd commit e56cb8e

File tree

4 files changed

+106
-131
lines changed

4 files changed

+106
-131
lines changed

imagebuildah/executor.go

+18-31
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,13 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o
337337
// We have to be careful here - it's either an argument
338338
// and value, or just an argument, since they can be
339339
// separated by either "=" or whitespace.
340-
list := strings.SplitN(arg.Value, "=", 2)
340+
argName, argValue, hasValue := strings.Cut(arg.Value, "=")
341341
if !foundFirstStage {
342-
if len(list) > 1 {
343-
globalArgs[list[0]] = list[1]
342+
if hasValue {
343+
globalArgs[argName] = argValue
344344
}
345345
}
346-
delete(exec.unusedArgs, list[0])
346+
delete(exec.unusedArgs, argName)
347347
}
348348
case "FROM":
349349
foundFirstStage = true
@@ -496,12 +496,7 @@ func (b *Executor) buildStage(ctx context.Context, cleanupStages map[int]*StageE
496496
var labelLine string
497497
labels := append([]string{}, b.labels...)
498498
for _, labelSpec := range labels {
499-
label := strings.SplitN(labelSpec, "=", 2)
500-
key := label[0]
501-
value := ""
502-
if len(label) > 1 {
503-
value = label[1]
504-
}
499+
key, value, _ := strings.Cut(labelSpec, "=")
505500
// check only for an empty key since docker allows empty values
506501
if key != "" {
507502
labelLine += fmt.Sprintf(" %q=%q", key, value)
@@ -523,10 +518,8 @@ func (b *Executor) buildStage(ctx context.Context, cleanupStages map[int]*StageE
523518
if len(b.envs) > 0 {
524519
var envLine string
525520
for _, envSpec := range b.envs {
526-
env := strings.SplitN(envSpec, "=", 2)
527-
key := env[0]
528-
if len(env) > 1 {
529-
value := env[1]
521+
key, value, hasValue := strings.Cut(envSpec, "=")
522+
if hasValue {
530523
envLine += fmt.Sprintf(" %q=%q", key, value)
531524
} else {
532525
return "", nil, false, fmt.Errorf("BUG: unresolved environment variable: %q", key)
@@ -844,24 +837,18 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image
844837
mountFlags := strings.TrimPrefix(flag, "--mount=")
845838
fields := strings.Split(mountFlags, ",")
846839
for _, field := range fields {
847-
if strings.HasPrefix(field, "from=") {
848-
fromField := strings.SplitN(field, "=", 2)
849-
if len(fromField) > 1 {
850-
mountFrom := fromField[1]
851-
// Check if this base is a stage if yes
852-
// add base to current stage's dependency tree
853-
// but also confirm if this is not in additional context.
854-
if _, ok := b.additionalBuildContexts[mountFrom]; !ok {
855-
// Treat from as a rootfs we need to preserve
856-
b.rootfsMap[mountFrom] = true
857-
if _, ok := dependencyMap[mountFrom]; ok {
858-
// update current stage's dependency info
859-
currentStageInfo := dependencyMap[stage.Name]
860-
currentStageInfo.Needs = append(currentStageInfo.Needs, mountFrom)
861-
}
840+
if mountFrom, hasFrom := strings.CutPrefix(field, "from="); hasFrom {
841+
// Check if this base is a stage if yes
842+
// add base to current stage's dependency tree
843+
// but also confirm if this is not in additional context.
844+
if _, ok := b.additionalBuildContexts[mountFrom]; !ok {
845+
// Treat from as a rootfs we need to preserve
846+
b.rootfsMap[mountFrom] = true
847+
if _, ok := dependencyMap[mountFrom]; ok {
848+
// update current stage's dependency info
849+
currentStageInfo := dependencyMap[stage.Name]
850+
currentStageInfo.Needs = append(currentStageInfo.Needs, mountFrom)
862851
}
863-
} else {
864-
return "", nil, fmt.Errorf("invalid value for field `from=`: %q", fromField[1])
865852
}
866853
}
867854
}

imagebuildah/stage_executor.go

+18-27
Original file line numberDiff line numberDiff line change
@@ -565,24 +565,23 @@ func (s *StageExecutor) runStageMountPoints(mountList []string) (map[string]inte
565565
stageMountPoints := make(map[string]internal.StageMountDetails)
566566
for _, flag := range mountList {
567567
if strings.Contains(flag, "from") {
568-
arr := strings.SplitN(flag, ",", 2)
569-
if len(arr) < 2 {
568+
tokens := strings.Split(flag, ",")
569+
if len(tokens) < 2 {
570570
return nil, fmt.Errorf("Invalid --mount command: %s", flag)
571571
}
572-
tokens := strings.Split(flag, ",")
573-
for _, val := range tokens {
574-
kv := strings.SplitN(val, "=", 2)
575-
switch kv[0] {
572+
for _, token := range tokens {
573+
key, val, hasVal := strings.Cut(token, "=")
574+
switch key {
576575
case "from":
577-
if len(kv) == 1 {
576+
if !hasVal {
578577
return nil, fmt.Errorf("unable to resolve argument for `from=`: bad argument")
579578
}
580-
if kv[1] == "" {
579+
if val == "" {
581580
return nil, fmt.Errorf("unable to resolve argument for `from=`: from points to an empty value")
582581
}
583-
from, fromErr := imagebuilder.ProcessWord(kv[1], s.stage.Builder.Arguments())
582+
from, fromErr := imagebuilder.ProcessWord(val, s.stage.Builder.Arguments())
584583
if fromErr != nil {
585-
return nil, fmt.Errorf("unable to resolve argument %q: %w", kv[1], fromErr)
584+
return nil, fmt.Errorf("unable to resolve argument %q: %w", val, fromErr)
586585
}
587586
// If additional buildContext contains this
588587
// give priority to that and break if additional
@@ -1748,9 +1747,9 @@ func (s *StageExecutor) getBuildArgsResolvedForRun() string {
17481747
dockerConfig := s.stage.Builder.Config()
17491748

17501749
for _, env := range dockerConfig.Env {
1751-
splitv := strings.SplitN(env, "=", 2)
1752-
if len(splitv) == 2 {
1753-
configuredEnvs[splitv[0]] = splitv[1]
1750+
key, val, hasVal := strings.Cut(env, "=")
1751+
if hasVal {
1752+
configuredEnvs[key] = val
17541753
}
17551754
}
17561755

@@ -2102,8 +2101,8 @@ func (s *StageExecutor) commit(ctx context.Context, createdBy string, emptyLayer
21022101
s.builder.SetPort(string(p))
21032102
}
21042103
for _, envSpec := range config.Env {
2105-
spec := strings.SplitN(envSpec, "=", 2)
2106-
s.builder.SetEnv(spec[0], spec[1])
2104+
key, val, _ := strings.Cut(envSpec, "=")
2105+
s.builder.SetEnv(key, val)
21072106
}
21082107
for _, envSpec := range s.executor.unsetEnvs {
21092108
s.builder.UnsetEnv(envSpec)
@@ -2139,12 +2138,8 @@ func (s *StageExecutor) commit(ctx context.Context, createdBy string, emptyLayer
21392138
// an intermediate image, in such case we must
21402139
// honor layer labels if they are configured.
21412140
for _, labelString := range s.executor.layerLabels {
2142-
label := strings.SplitN(labelString, "=", 2)
2143-
if len(label) > 1 {
2144-
s.builder.SetLabel(label[0], label[1])
2145-
} else {
2146-
s.builder.SetLabel(label[0], "")
2147-
}
2141+
labelk, labelv, _ := strings.Cut(labelString, "=")
2142+
s.builder.SetLabel(labelk, labelv)
21482143
}
21492144
}
21502145
for k, v := range config.Labels {
@@ -2157,12 +2152,8 @@ func (s *StageExecutor) commit(ctx context.Context, createdBy string, emptyLayer
21572152
s.builder.UnsetLabel(key)
21582153
}
21592154
for _, annotationSpec := range s.executor.annotations {
2160-
annotation := strings.SplitN(annotationSpec, "=", 2)
2161-
if len(annotation) > 1 {
2162-
s.builder.SetAnnotation(annotation[0], annotation[1])
2163-
} else {
2164-
s.builder.SetAnnotation(annotation[0], "")
2165-
}
2155+
annotationk, annotationv, _ := strings.Cut(annotationSpec, "=")
2156+
s.builder.SetAnnotation(annotationk, annotationv)
21662157
}
21672158
if imageRef != nil {
21682159
logName := transports.ImageName(imageRef)

internal/source/add.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ func (o *AddOptions) annotations() (map[string]string, error) {
2626
annotations := make(map[string]string)
2727

2828
for _, unparsed := range o.Annotations {
29-
parsed := strings.SplitN(unparsed, "=", 2)
30-
if len(parsed) != 2 {
29+
key, value, hasValue := strings.Cut(unparsed, "=")
30+
if !hasValue {
3131
return nil, fmt.Errorf("invalid annotation %q (expected format is \"key=value\")", unparsed)
3232
}
33-
if _, exists := annotations[parsed[0]]; exists {
34-
return nil, fmt.Errorf("annotation %q specified more than once", parsed[0])
33+
if _, exists := annotations[key]; exists {
34+
return nil, fmt.Errorf("annotation %q specified more than once", key)
3535
}
36-
annotations[parsed[0]] = parsed[1]
36+
annotations[key] = value
3737
}
3838

3939
return annotations, nil

0 commit comments

Comments
 (0)