diff --git a/cmd/podman/common/create_test.go b/cmd/podman/common/create_test.go index 27835999a0..e9cfc47185 100644 --- a/cmd/podman/common/create_test.go +++ b/cmd/podman/common/create_test.go @@ -21,13 +21,13 @@ func TestPodOptions(t *testing.T) { cc := reflect.ValueOf(&exampleOptions).Elem() pc := reflect.ValueOf(&podOptions).Elem() - pcType := reflect.TypeOf(podOptions) + pcType := reflect.TypeFor[entities.PodCreateOptions]() for i := 0; i < pc.NumField(); i++ { podField := pc.FieldByIndex([]int{i}) podType := pcType.Field(i) for j := 0; j < cc.NumField(); j++ { containerField := cc.FieldByIndex([]int{j}) - containerType := reflect.TypeOf(exampleOptions).Field(j) + containerType := reflect.TypeFor[entities.ContainerCreateOptions]().Field(j) tagPod := strings.Split(podType.Tag.Get("json"), ",")[0] tagContainer := strings.Split(containerType.Tag.Get("json"), ",")[0] if tagPod == tagContainer && (tagPod != "" && tagContainer != "") { diff --git a/cmd/podman/machine/machine.go b/cmd/podman/machine/machine.go index 41d1534d21..288a36d086 100644 --- a/cmd/podman/machine/machine.go +++ b/cmd/podman/machine/machine.go @@ -70,7 +70,7 @@ func autocompleteMachineSSH(_ *cobra.Command, args []string, toComplete string) // autocompleteMachineCp - Autocomplete machine cp command. func autocompleteMachineCp(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) < 2 { - if i := strings.IndexByte(toComplete, ':'); i > -1 { + if found := strings.Contains(toComplete, ":"); found { // TODO: offer virtual machine path completion // the user already set the machine name, so don't use the host file autocompletion diff --git a/cmd/quadlet/main.go b/cmd/quadlet/main.go index 9a17a1517b..815edb2360 100644 --- a/cmd/quadlet/main.go +++ b/cmd/quadlet/main.go @@ -316,14 +316,14 @@ func isUnambiguousName(imageName string) bool { } // Otherwise we require a fully qualified name - firstSlash := strings.Index(imageName, "/") - if firstSlash == -1 { + before, _, ok := strings.Cut(imageName, "/") + if !ok { // No domain or path, not fully qualified return false } // What is before the first slash can be a domain or a path - domain := imageName[:firstSlash] + domain := before // If its a domain (has dot or port or is "localhost") it is considered fq if strings.ContainsAny(domain, ".:") || domain == "localhost" { diff --git a/libpod/container_internal_common.go b/libpod/container_internal_common.go index 05519076c0..1ffeaceb66 100644 --- a/libpod/container_internal_common.go +++ b/libpod/container_internal_common.go @@ -2583,7 +2583,7 @@ func (c *Container) groupEntry(groupname, gid string, list []string) string { // Returns password entry (as a string that can be appended to /etc/passwd) and // any error that occurred. func (c *Container) generatePasswdEntry() (string, error) { - passwdString := "" + var passwdString strings.Builder addedUID := 0 for _, userid := range c.config.HostUsers { @@ -2596,14 +2596,14 @@ func (c *Container) generatePasswdEntry() (string, error) { if err != nil { return "", err } - passwdString += entry + passwdString.WriteString(entry) } if c.config.AddCurrentUserPasswdEntry { entry, uid, _, err := c.generateCurrentUserPasswdEntry() if err != nil { return "", err } - passwdString += entry + passwdString.WriteString(entry) addedUID = uid } if c.config.User != "" { @@ -2611,10 +2611,10 @@ func (c *Container) generatePasswdEntry() (string, error) { if err != nil { return "", err } - passwdString += entry + passwdString.WriteString(entry) } - return passwdString, nil + return passwdString.String(), nil } // generateCurrentUserPasswdEntry generates an /etc/passwd entry for the user diff --git a/libpod/container_secrets_test.go b/libpod/container_secrets_test.go index 5c3f70a398..b67f32c79e 100644 --- a/libpod/container_secrets_test.go +++ b/libpod/container_secrets_test.go @@ -3,6 +3,7 @@ package libpod import ( + "slices" "testing" "github.com/opencontainers/runtime-tools/generate" @@ -94,13 +95,7 @@ func TestInjectEnvSecrets(t *testing.T) { } else { assert.NoError(t, err) for key, val := range tt.expectedEnv { - found := false - for _, env := range g.Config.Process.Env { - if env == key+"="+val { - found = true - break - } - } + found := slices.Contains(g.Config.Process.Env, key+"="+val) assert.True(t, found, "Expected env %s=%s not found", key, val) } } diff --git a/pkg/api/handlers/libpod/manifests.go b/pkg/api/handlers/libpod/manifests.go index 15451ffde5..002b0521e8 100644 --- a/pkg/api/handlers/libpod/manifests.go +++ b/pkg/api/handlers/libpod/manifests.go @@ -554,9 +554,7 @@ func ManifestModify(w http.ResponseWriter, r *http.Request) { // If the data was multipart, then save items from it into a // directory that will be removed along with this list, // whenever that happens. - artifactExtraction.Add(1) - go func() { - defer artifactExtraction.Done() + artifactExtraction.Go(func() { storageConfig := runtime.StorageConfig() // FIXME: knowing that this is the location of the // per-image-record-stuff directory is a little too @@ -618,7 +616,7 @@ func ManifestModify(w http.ResponseWriter, r *http.Request) { } // Save the list of files that we created. body.ArtifactFiles = contentFiles - }() + }) } if tlsVerify, ok := r.URL.Query()["tlsVerify"]; ok { diff --git a/pkg/api/handlers/utils/images.go b/pkg/api/handlers/utils/images.go index b1c750049b..462eaca43b 100644 --- a/pkg/api/handlers/utils/images.go +++ b/pkg/api/handlers/utils/images.go @@ -146,15 +146,9 @@ func jsonProgressToString(p *jsonstream.Progress) string { } } - percentage := int(float64(p.Current)/float64(p.Total)*100) / 2 - if percentage > 50 { - percentage = 50 - } + percentage := min(int(float64(p.Current)/float64(p.Total)*100)/2, 50) - numSpaces := 0 - if 50-percentage > 0 { - numSpaces = 50 - percentage - } + numSpaces := max(50-percentage, 0) pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces)) switch { diff --git a/pkg/domain/infra/abi/quadlet.go b/pkg/domain/infra/abi/quadlet.go index abab6cf5bd..c3e00b66d6 100644 --- a/pkg/domain/infra/abi/quadlet.go +++ b/pkg/domain/infra/abi/quadlet.go @@ -296,8 +296,8 @@ func getFileName(resp *http.Response, fileURL string) (string, error) { cd := resp.Header.Get("Content-Disposition") if cd != "" { const prefix = "filename=" - if idx := strings.Index(cd, prefix); idx != -1 { - filename := cd[idx+len(prefix):] + if _, after, ok := strings.Cut(cd, prefix); ok { + filename := after filename = strings.Trim(filename, "\"'") return filename, nil } @@ -471,8 +471,8 @@ func parseMultiQuadletFile(filePath string) ([]quadletSection, error) { // extractFileNameFromSection extracts the FileName from a comment in the quadlet section // The comment must be in the format: # FileName=my-name func extractFileNameFromSection(content string) (string, error) { - lines := strings.Split(content, "\n") - for _, line := range lines { + lines := strings.SplitSeq(content, "\n") + for line := range lines { line = strings.TrimSpace(line) // Look for comment lines starting with # if strings.HasPrefix(line, "#") { @@ -499,8 +499,8 @@ func extractFileNameFromSection(content string) (string, error) { // Returns the appropriate file extension (.container, .volume, .network, etc.) func detectQuadletType(content string) (string, error) { // Look for section headers like [Container], [Volume], [Network], etc. - lines := strings.Split(content, "\n") - for _, line := range lines { + lines := strings.SplitSeq(content, "\n") + for line := range lines { line = strings.TrimSpace(line) if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { sectionName := strings.ToLower(strings.Trim(line, "[]")) diff --git a/pkg/trust/policy.go b/pkg/trust/policy.go index dfdc75cbf2..6f222f312f 100644 --- a/pkg/trust/policy.go +++ b/pkg/trust/policy.go @@ -134,8 +134,8 @@ func parseUids(colonDelimitKeys []byte) []string { parseduid := uid if ltidx := strings.Index(uid, "<"); ltidx != -1 { subuid := parseduid[ltidx+1:] - if gtidx := strings.Index(subuid, ">"); gtidx != -1 { - parseduid = subuid[:gtidx] + if before, _, ok := strings.Cut(subuid, ">"); ok { + parseduid = before } } parseduids = append(parseduids, parseduid) diff --git a/test/e2e/images_test.go b/test/e2e/images_test.go index 1892121d37..4fd56ef7ad 100644 --- a/test/e2e/images_test.go +++ b/test/e2e/images_test.go @@ -86,7 +86,7 @@ var _ = Describe("Podman images", func() { output := session.OutputToString() Expect(output).To(BeValidJSON()) - images := []map[string]interface{}{} + images := []map[string]any{} err := json.Unmarshal([]byte(output), &images) Expect(err).ToNot(HaveOccurred()) diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go index b971057dd9..82da967142 100644 --- a/test/e2e/play_kube_test.go +++ b/test/e2e/play_kube_test.go @@ -1573,14 +1573,14 @@ func generateKubeYaml(kind string, object any, pathname string) error { // generateMultiDocKubeYaml writes multiple kube objects in one Yaml document. func generateMultiDocKubeYaml(kubeObjects []string, pathname string) error { - var multiKube string + var multiKube strings.Builder for _, k := range kubeObjects { - multiKube += "---\n" - multiKube += k + multiKube.WriteString("---\n") + multiKube.WriteString(k) } - return writeYaml(multiKube, pathname) + return writeYaml(multiKube.String(), pathname) } func createSecret(podmanTest *PodmanTestIntegration, name string, value []byte) { //nolint:unparam diff --git a/test/e2e/search_mock_registry.go b/test/e2e/search_mock_registry.go index 1254e69cc1..2450417bd7 100644 --- a/test/e2e/search_mock_registry.go +++ b/test/e2e/search_mock_registry.go @@ -399,10 +399,7 @@ func applyLimitToResults(results map[string]any, limitNum int) map[string]any { if limitNum > 0 { if resultsArray, ok := resultsCopy["results"].([]any); ok { - actualLimit := limitNum - if len(resultsArray) < limitNum { - actualLimit = len(resultsArray) - } + actualLimit := min(len(resultsArray), limitNum) resultsCopy["results"] = resultsArray[:actualLimit] resultsCopy["num_results"] = actualLimit } @@ -491,10 +488,7 @@ func applyPagination(allTags []string, limit int, last string, repoName string) } if limit > 0 { - endIndex := startIndex + limit - if endIndex > len(allTags) { - endIndex = len(allTags) - } + endIndex := min(startIndex+limit, len(allTags)) return allTags[startIndex:endIndex] }