1
0
mirror of https://github.com/containers/buildah.git synced 2025-04-18 07:04:05 +03:00

Use slices.Clone

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2025-04-07 12:27:12 -07:00
parent 84a3905f61
commit 0835cb4760
10 changed files with 27 additions and 22 deletions

View File

@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"syscall"
"time"
@ -61,14 +62,14 @@ func setPlatformUnshareOptions(spec *specs.Spec, cmd *unshare.Cmd) error {
uidmap, gidmap := spec.Linux.UIDMappings, spec.Linux.GIDMappings
if len(uidmap) == 0 {
// No UID mappings are configured for the container. Borrow our parent's mappings.
uidmap = append([]specs.LinuxIDMapping{}, hostUidmap...)
uidmap = slices.Clone(hostUidmap)
for i := range uidmap {
uidmap[i].HostID = uidmap[i].ContainerID
}
}
if len(gidmap) == 0 {
// No GID mappings are configured for the container. Borrow our parent's mappings.
gidmap = append([]specs.LinuxIDMapping{}, hostGidmap...)
gidmap = slices.Clone(hostGidmap)
for i := range gidmap {
gidmap[i].HostID = gidmap[i].ContainerID
}

View File

@ -331,7 +331,7 @@ func Stat(root string, directory string, options StatOptions, globs []string) ([
Request: requestStat,
Root: root,
Directory: directory,
Globs: append([]string{}, globs...),
Globs: slices.Clone(globs),
StatOptions: options,
}
resp, err := copier(nil, nil, req)
@ -382,7 +382,7 @@ func Get(root string, directory string, options GetOptions, globs []string, bulk
Request: requestGet,
Root: root,
Directory: directory,
Globs: append([]string{}, globs...),
Globs: slices.Clone(globs),
StatOptions: StatOptions{
CheckForArchives: options.ExpandArchives,
},
@ -598,7 +598,7 @@ func copierWithoutSubprocess(bulkReader io.Reader, bulkWriter io.Writer, req req
req.preservedRoot = req.Root
req.rootPrefix = string(os.PathSeparator)
req.preservedDirectory = req.Directory
req.preservedGlobs = append([]string{}, req.Globs...)
req.preservedGlobs = slices.Clone(req.Globs)
if !filepath.IsAbs(req.Directory) {
req.Directory = filepath.Join(req.Root, cleanerReldirectory(req.Directory))
}
@ -850,7 +850,7 @@ func copierMain() {
req.preservedRoot = req.Root
req.rootPrefix = string(os.PathSeparator)
req.preservedDirectory = req.Directory
req.preservedGlobs = append([]string{}, req.Globs...)
req.preservedGlobs = slices.Clone(req.Globs)
if chrooted {
// We'll need to adjust some things now that the root
// directory isn't what it was. Make the directory and

View File

@ -13,6 +13,7 @@ import (
"path"
"path/filepath"
"reflect"
"slices"
"sort"
"strconv"
"strings"
@ -1753,7 +1754,7 @@ func testMkdir(t *testing.T) {
return nil
})
require.NoErrorf(t, err, "error walking directory to catalog post-Mkdir contents: %v", err)
expected := append([]string{}, beforeNames...)
expected := slices.Clone(beforeNames)
for _, expect := range testCase.expect {
expected = append(expected, filepath.FromSlash(expect))
}

View File

@ -511,7 +511,7 @@ func (b *Executor) buildStage(ctx context.Context, cleanupStages map[int]*StageE
// layers, its easier to reuse cached layers.
if len(b.labels) > 0 {
var labelLine string
labels := append([]string{}, b.labels...)
labels := slices.Clone(b.labels)
for _, labelSpec := range labels {
key, value, _ := strings.Cut(labelSpec, "=")
// check only for an empty key since docker allows empty values

View File

@ -808,7 +808,7 @@ func (s *StageExecutor) Run(run imagebuilder.Run, config docker.Config) error {
defer devNull.Close()
stdin = devNull
}
namespaceOptions := append([]define.NamespaceOption{}, s.executor.namespaceOptions...)
namespaceOptions := slices.Clone(s.executor.namespaceOptions)
options := buildah.RunOptions{
Args: s.executor.runtimeArgs,
Cmd: config.Cmd,
@ -2375,7 +2375,7 @@ func (s *StageExecutor) commit(ctx context.Context, createdBy string, emptyLayer
s.builder.SetStopSignal(config.StopSignal)
if config.Healthcheck != nil {
s.builder.SetHealthcheck(&buildahdocker.HealthConfig{
Test: append([]string{}, config.Healthcheck.Test...),
Test: slices.Clone(config.Healthcheck.Test),
Interval: config.Healthcheck.Interval,
Timeout: config.Healthcheck.Timeout,
StartPeriod: config.Healthcheck.StartPeriod,

View File

@ -101,17 +101,17 @@ func GoDockerclientConfigFromSchema2Config(s2config *manifest.Schema2Config) *do
Tty: s2config.Tty,
OpenStdin: s2config.OpenStdin,
StdinOnce: s2config.StdinOnce,
Env: append([]string{}, s2config.Env...),
Cmd: append([]string{}, s2config.Cmd...),
Env: slices.Clone(s2config.Env),
Cmd: slices.Clone(s2config.Cmd),
Healthcheck: healthCheck,
ArgsEscaped: s2config.ArgsEscaped,
Image: s2config.Image,
Volumes: volumes,
WorkingDir: s2config.WorkingDir,
Entrypoint: append([]string{}, s2config.Entrypoint...),
Entrypoint: slices.Clone(s2config.Entrypoint),
NetworkDisabled: s2config.NetworkDisabled,
MacAddress: s2config.MacAddress,
OnBuild: append([]string{}, s2config.OnBuild...),
OnBuild: slices.Clone(s2config.OnBuild),
Labels: labels,
StopSignal: s2config.StopSignal,
Shell: s2config.Shell,

View File

@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"slices"
"strings"
"github.com/containers/buildah/docker"
@ -24,9 +25,9 @@ func firstStringElseSecondString(first, second string) string {
// slice of strings if it has contents, else the second slice
func firstSliceElseSecondSlice(first, second []string) []string {
if len(first) > 0 {
return append([]string{}, first...)
return slices.Clone(first)
}
return append([]string{}, second...)
return slices.Clone(second)
}
// firstSlicePairElseSecondSlicePair takes two pairs of string slices, and
@ -34,9 +35,9 @@ func firstSliceElseSecondSlice(first, second []string) []string {
// pair
func firstSlicePairElseSecondSlicePair(firstA, firstB, secondA, secondB []string) ([]string, []string) {
if len(firstA) > 0 || len(firstB) > 0 {
return append([]string{}, firstA...), append([]string{}, firstB...)
return slices.Clone(firstA), slices.Clone(firstB)
}
return append([]string{}, secondA...), append([]string{}, secondB...)
return slices.Clone(secondA), slices.Clone(secondB)
}
// mergeEnv combines variables from a and b into a single environment slice. if
@ -134,7 +135,7 @@ func Override(dconfig *docker.Config, oconfig *v1.ImageConfig, overrideChanges [
oconfig.Entrypoint, oconfig.Cmd = firstSlicePairElseSecondSlicePair(overrideConfig.Entrypoint, overrideConfig.Cmd, oconfig.Entrypoint, oconfig.Cmd)
if overrideConfig.Healthcheck != nil {
dconfig.Healthcheck = &docker.HealthConfig{
Test: append([]string{}, overrideConfig.Healthcheck.Test...),
Test: slices.Clone(overrideConfig.Healthcheck.Test),
Interval: overrideConfig.Healthcheck.Interval,
Timeout: overrideConfig.Healthcheck.Timeout,
StartPeriod: overrideConfig.Healthcheck.StartPeriod,

View File

@ -11,6 +11,7 @@ import (
"net"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"unicode"
@ -852,7 +853,7 @@ func SBOMScanOptionsFromFlagSet(flags *pflag.FlagSet, _ func(name string) *pflag
if image != "" || len(commands) > 0 || mergeStrategy != "" {
options = &define.SBOMScanOptions{
Image: image,
Commands: append([]string{}, commands...),
Commands: slices.Clone(commands),
MergeStrategy: define.SBOMMergeStrategy(mergeStrategy),
}
}

View File

@ -107,7 +107,7 @@ func (b *Builder) sbomScan(ctx context.Context, options CommitOptions) (imageFil
// Start by assuming it's shell -c $whatever.
parsedCommand := []string{"/bin/sh", "-c", commandSpec}
if shell := scanBuilder.Shell(); len(shell) != 0 {
parsedCommand = append(append([]string{}, shell...), commandSpec)
parsedCommand = append(slices.Clone(shell), commandSpec)
}
if !strings.ContainsAny(commandSpec, "<>|") { // An imperfect check for shell redirection being used.
// If we can parse it ourselves, though, prefer to use that result,

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"syscall"
@ -66,7 +67,7 @@ func getProcessUser(r *types.TestReport) error {
}
func getProcessArgs(r *types.TestReport) error {
r.Spec.Process.Args = append([]string{}, os.Args...)
r.Spec.Process.Args = slices.Clone(os.Args)
return nil
}