mirror of
https://github.com/containers/podman.git
synced 2025-09-22 12:14:26 +08:00
use strings.SplitSeq where possible
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
@ -125,8 +125,7 @@ func port(_ *cobra.Command, args []string) error {
|
||||
if hostIP == "" {
|
||||
hostIP = "0.0.0.0"
|
||||
}
|
||||
protocols := strings.Split(v.Protocol, ",")
|
||||
for _, protocol := range protocols {
|
||||
for protocol := range strings.SplitSeq(v.Protocol, ",") {
|
||||
// If not searching by port or port/proto, then dump what we see
|
||||
if port == "" {
|
||||
for i := uint16(0); i < v.Range; i++ {
|
||||
|
@ -39,8 +39,7 @@ func ValidateExtraHost(val string) (string, error) {
|
||||
}
|
||||
|
||||
// Split the hostnames by semicolon and validate each one
|
||||
nameList := strings.Split(names, ";")
|
||||
for _, name := range nameList {
|
||||
for name := range strings.SplitSeq(names, ";") {
|
||||
if len(name) == 0 {
|
||||
return "", fmt.Errorf("hostname in add-host %q is empty", val)
|
||||
}
|
||||
|
@ -294,8 +294,7 @@ func handler(ctx context.Context, conn io.Reader, pm rkport.Manager) error {
|
||||
func exposePorts(pm rkport.Manager, portMappings []types.PortMapping, childIP string) error {
|
||||
ctx := context.TODO()
|
||||
for _, port := range portMappings {
|
||||
protocols := strings.Split(port.Protocol, ",")
|
||||
for _, protocol := range protocols {
|
||||
for protocol := range strings.SplitSeq(port.Protocol, ",") {
|
||||
hostIP := port.HostIP
|
||||
if hostIP == "" {
|
||||
hostIP = "0.0.0.0"
|
||||
|
@ -96,7 +96,7 @@ func addPathToRegistry(dir string) error {
|
||||
}
|
||||
|
||||
// Is this directory already on the windows path?
|
||||
for _, element := range strings.Split(existing, ";") {
|
||||
for element := range strings.SplitSeq(existing, ";") {
|
||||
if strings.EqualFold(element, dir) {
|
||||
// Path already added
|
||||
return nil
|
||||
@ -147,7 +147,7 @@ func removePathFromRegistry(path string) error {
|
||||
// No point preallocating we can't know how big the array needs to be.
|
||||
//nolint:prealloc
|
||||
var elements []string
|
||||
for _, element := range strings.Split(existing, ";") {
|
||||
for element := range strings.SplitSeq(existing, ";") {
|
||||
if strings.EqualFold(element, path) {
|
||||
continue
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ func StartWithOptions(options *Options) (*Registry, error) {
|
||||
|
||||
// Parse the output.
|
||||
registry := Registry{}
|
||||
for _, s := range strings.Split(out, "\n") {
|
||||
for s := range strings.SplitSeq(out, "\n") {
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
|
@ -201,7 +201,7 @@ func (c *Container) Top(descriptors []string) ([]string, error) {
|
||||
// Also support comma-separated input.
|
||||
psgoDescriptors := []string{}
|
||||
for _, d := range descriptors {
|
||||
for _, s := range strings.Split(d, ",") {
|
||||
for s := range strings.SplitSeq(d, ",") {
|
||||
if s != "" {
|
||||
psgoDescriptors = append(psgoDescriptors, s)
|
||||
}
|
||||
|
@ -1084,8 +1084,7 @@ func containerToV1Container(ctx context.Context, c *Container, getService bool)
|
||||
func portMappingToContainerPort(portMappings []types.PortMapping, getService bool) ([]v1.ContainerPort, error) {
|
||||
containerPorts := make([]v1.ContainerPort, 0, len(portMappings))
|
||||
for _, p := range portMappings {
|
||||
protocols := strings.Split(p.Protocol, ",")
|
||||
for _, proto := range protocols {
|
||||
for proto := range strings.SplitSeq(p.Protocol, ",") {
|
||||
var protocol v1.Protocol
|
||||
switch strings.ToUpper(proto) {
|
||||
case "TCP":
|
||||
@ -1360,7 +1359,7 @@ func generateKubeSecurityContext(c *Container) (*v1.SecurityContext, bool, error
|
||||
}
|
||||
var selinuxOpts v1.SELinuxOptions
|
||||
selinuxHasData := false
|
||||
for _, label := range strings.Split(c.config.Spec.Annotations[define.InspectAnnotationLabel], ",label=") {
|
||||
for label := range strings.SplitSeq(c.config.Spec.Annotations[define.InspectAnnotationLabel], ",label=") {
|
||||
opt, val, hasVal := strings.Cut(label, ":")
|
||||
if hasVal {
|
||||
switch opt {
|
||||
|
@ -363,8 +363,7 @@ func (r *ConmonOCIRuntime) StopContainer(ctr *Container, timeout uint, all bool)
|
||||
|
||||
// Before handling error from KillContainer, convert STDERR to a []string
|
||||
// (one string per line of output) and print it.
|
||||
stderrLines := strings.Split(stderr.String(), "\n")
|
||||
for _, line := range stderrLines {
|
||||
for line := range strings.SplitSeq(stderr.String(), "\n") {
|
||||
if line != "" {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", line)
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ func (r *ConmonOCIRuntime) createRootlessContainer(ctr *Container, restoreOption
|
||||
for dir := filepath.Dir(rootPath); ; dir = filepath.Dir(dir) {
|
||||
if m, found := byMountpoint[dir]; found {
|
||||
parentMount = dir
|
||||
for _, o := range strings.Split(m.Optional, ",") {
|
||||
for o := range strings.SplitSeq(m.Optional, ",") {
|
||||
opt := strings.Split(o, ":")
|
||||
if opt[0] == "shared" {
|
||||
isShared = true
|
||||
|
@ -205,8 +205,7 @@ func writeHijackHeader(r *http.Request, conn io.Writer, tty bool) {
|
||||
func makeInspectPortBindings(bindings []types.PortMapping) map[string][]define.InspectHostPort {
|
||||
portBindings := make(map[string][]define.InspectHostPort)
|
||||
for _, port := range bindings {
|
||||
protocols := strings.Split(port.Protocol, ",")
|
||||
for _, protocol := range protocols {
|
||||
for protocol := range strings.SplitSeq(port.Protocol, ",") {
|
||||
for i := uint16(0); i < port.Range; i++ {
|
||||
key := fmt.Sprintf("%d/%s", port.ContainerPort+i, protocol)
|
||||
hostPorts := portBindings[key]
|
||||
|
@ -24,9 +24,8 @@ func VolumeOptions(opts map[string]string) ([]libpod.VolumeCreateOption, error)
|
||||
case "o":
|
||||
// o has special handling to parse out UID, GID.
|
||||
// These are separate Libpod options.
|
||||
splitVal := strings.Split(value, ",")
|
||||
finalVal := []string{}
|
||||
for _, o := range splitVal {
|
||||
for o := range strings.SplitSeq(value, ",") {
|
||||
// Options will be formatted as either "opt" or
|
||||
// "opt=value"
|
||||
opt, val, hasVal := strings.Cut(o, "=")
|
||||
|
@ -135,7 +135,7 @@ func (ic *ContainerEngine) prepareAutomountImages(ctx context.Context, forContai
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
for _, imageName := range strings.Split(automount, ";") {
|
||||
for imageName := range strings.SplitSeq(automount, ";") {
|
||||
img, fullName, err := ic.Libpod.LibimageRuntime().LookupImage(imageName, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("image %s from container %s does not exist in local storage, cannot automount: %w", imageName, forContainer, err)
|
||||
|
@ -75,7 +75,7 @@ func (ms *machineSession) Bytes() []byte {
|
||||
func (ms *machineSession) outputToStringSlice() []string {
|
||||
var results []string
|
||||
output := string(ms.Out.Contents())
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
for line := range strings.SplitSeq(output, "\n") {
|
||||
if line != "" {
|
||||
results = append(results, line)
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ func (n UsernsMode) GetKeepIDOptions() (*KeepIDUserNsOptions, error) {
|
||||
if !hasOpts {
|
||||
return &options, nil
|
||||
}
|
||||
for _, o := range strings.Split(nsopts, ",") {
|
||||
for o := range strings.SplitSeq(nsopts, ",") {
|
||||
opt, val, hasVal := strings.Cut(o, "=")
|
||||
if !hasVal {
|
||||
return nil, fmt.Errorf("invalid option specified: %q", o)
|
||||
|
@ -26,9 +26,8 @@ func GetRacct(filter string) (map[string]uint64, error) {
|
||||
return nil, fmt.Errorf("error calling rctl_get_racct with filter %s: %v", filter, errno)
|
||||
}
|
||||
len := bytes.IndexByte(buf[:], byte(0))
|
||||
entries := strings.Split(string(buf[:len]), ",")
|
||||
res := make(map[string]uint64)
|
||||
for _, entry := range entries {
|
||||
for entry := range strings.SplitSeq(string(buf[:len]), ",") {
|
||||
key, valstr, _ := strings.Cut(entry, "=")
|
||||
val, err := strconv.ParseUint(valstr, 10, 0)
|
||||
if err != nil {
|
||||
|
@ -230,7 +230,7 @@ func becomeRootInUserNS(pausePid string) (_ bool, _ int, retErr error) {
|
||||
for _, m := range mounts {
|
||||
if m.Mountpoint == "/" {
|
||||
isShared := false
|
||||
for _, o := range strings.Split(m.Optional, ",") {
|
||||
for o := range strings.SplitSeq(m.Optional, ",") {
|
||||
if strings.HasPrefix(o, "shared:") {
|
||||
isShared = true
|
||||
break
|
||||
|
@ -293,8 +293,7 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
|
||||
|
||||
ulimitVal, ok := opts.Annotations[define.UlimitAnnotation]
|
||||
if ok {
|
||||
ulimits := strings.Split(ulimitVal, ",")
|
||||
for _, ul := range ulimits {
|
||||
for ul := range strings.SplitSeq(ulimitVal, ",") {
|
||||
parsed, err := units.ParseUlimit(ul)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -378,9 +378,8 @@ func createPortMappings(s *specgen.SpecGenerator, imageData *libimage.ImageData)
|
||||
// Check a string to ensure it is a comma-separated set of valid protocols
|
||||
func checkProtocol(protocol string) ([]string, error) {
|
||||
protocols := make(map[string]struct{})
|
||||
splitProto := strings.Split(protocol, ",")
|
||||
// Don't error on duplicates - just deduplicate
|
||||
for _, p := range splitProto {
|
||||
for p := range strings.SplitSeq(protocol, ",") {
|
||||
p = strings.ToLower(p)
|
||||
switch p {
|
||||
case protoTCP, "":
|
||||
|
@ -1263,7 +1263,7 @@ func parseLinuxResourcesDeviceAccess(device string) (specs.LinuxDeviceCgroup, er
|
||||
minor = &m
|
||||
}
|
||||
access = value[2]
|
||||
for _, c := range strings.Split(access, "") {
|
||||
for c := range strings.SplitSeq(access, "") {
|
||||
if !cgroupDeviceAccess[c] {
|
||||
return specs.LinuxDeviceCgroup{}, fmt.Errorf("invalid device access in device-access-add: %s", c)
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ func (p *NotifyProxy) listen() {
|
||||
sBuilder.Write(buffer[:n])
|
||||
var isBarrier, isReady bool
|
||||
|
||||
for _, line := range strings.Split(sBuilder.String(), "\n") {
|
||||
for line := range strings.SplitSeq(sBuilder.String(), "\n") {
|
||||
switch line {
|
||||
case _notifyRdyMsg:
|
||||
isReady = true
|
||||
|
@ -288,7 +288,7 @@ func (s *PodmanSession) OutputToString() string {
|
||||
func (s *PodmanSession) OutputToStringArray() []string {
|
||||
var results []string
|
||||
output := string(s.Out.Contents())
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
for line := range strings.SplitSeq(output, "\n") {
|
||||
if line != "" {
|
||||
results = append(results, line)
|
||||
}
|
||||
|
Reference in New Issue
Block a user