Skip to content

Modernize sources #26830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/podman/common/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -337,9 +338,7 @@ func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *Buil
if err != nil {
return nil, err
}
for name, val := range fargs {
args[name] = val
}
maps.Copy(args, fargs)
}
}
if c.Flag("build-arg").Changed {
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ func convertFormatSuggestions(suggestions []formatSuggestion) []string {
// This function will only work for pointer to structs other types are not supported.
// When "{{." is typed the field and method names of the given struct will be completed.
// This also works recursive for nested structs.
func AutocompleteFormat(o interface{}) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
func AutocompleteFormat(o any) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// this function provides shell completion for go templates
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// autocomplete json when nothing or json is typed
Expand Down Expand Up @@ -1454,7 +1454,7 @@ func AutocompleteFormat(o interface{}) func(cmd *cobra.Command, args []string, t
}
}

func getEntityType(cmd *cobra.Command, args []string, o interface{}) interface{} {
func getEntityType(cmd *cobra.Command, args []string, o any) any {
// container logic
if containers, _ := getContainers(cmd, args[0], completeDefault); len(containers) > 0 {
return &define.InspectContainerData{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/images/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func imageSearch(cmd *cobra.Command, args []string) error {
return rpt.Execute(searchReport)
}

func printArbitraryJSON(v interface{}) error {
func printArbitraryJSON(v any) error {
prettyJSON, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/podman/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func newInspector(options entities.InspectOptions) (*inspector, error) {
// inspect inspects the specified container/image names or IDs.
func (i *inspector) inspect(namesOrIDs []string) error {
// data - dumping place for inspection results.
var data []interface{}
var data []any
var errs []error
ctx := context.Background()

Expand Down Expand Up @@ -157,7 +157,7 @@ func (i *inspector) inspect(namesOrIDs []string) error {
}
// Always print an empty array
if data == nil {
data = []interface{}{}
data = []any{}
}

var err error
Expand Down Expand Up @@ -191,8 +191,8 @@ func (i *inspector) inspect(namesOrIDs []string) error {
return nil
}

func (i *inspector) inspectAll(ctx context.Context, namesOrIDs []string) ([]interface{}, []error, error) {
var data []interface{}
func (i *inspector) inspectAll(ctx context.Context, namesOrIDs []string) ([]any, []error, error) {
var data []any
allErrs := []error{}
for _, name := range namesOrIDs {
ctrData, errs, err := i.containerEngine.ContainerInspect(ctx, []string{name}, i.options)
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/machine/client9p.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func client9p(portNum uint32, mountPath string) error {
conn *vsock.Conn
retries = 20
)
for i := 0; i < retries; i++ {
for range retries {
// Host connects to non-hypervisor processes on the host running the VM.
conn, err = vsock.Dial(vsock.Host, portNum, nil)
// If errors.Is worked on this error, we could detect non-timeout errors.
Expand Down
8 changes: 4 additions & 4 deletions cmd/podman/machine/os/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func GetDistribution() Distribution {

l := bufio.NewScanner(f)
for l.Scan() {
if strings.HasPrefix(l.Text(), "ID=") {
dist.Name = strings.TrimPrefix(l.Text(), "ID=")
if after, ok := strings.CutPrefix(l.Text(), "ID="); ok {
dist.Name = after
}
if strings.HasPrefix(l.Text(), "VARIANT_ID=") {
dist.Variant = strings.Trim(strings.TrimPrefix(l.Text(), "VARIANT_ID="), "\"")
if after, ok := strings.CutPrefix(l.Text(), "VARIANT_ID="); ok {
dist.Variant = strings.Trim(after, "\"")
}
}
return dist
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import (

type logrusLogger struct{}

func (l logrusLogger) Errorf(format string, args ...interface{}) {
func (l logrusLogger) Errorf(format string, args ...any) {
logrus.Errorf(format, args...)
}
func (l logrusLogger) Debugf(format string, args ...interface{}) {
func (l logrusLogger) Debugf(format string, args ...any) {
logrus.Debugf(format, args...)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/quadlet/quadlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
// logrusLogger implements the logiface.Logger interface using logrus
type logrusLogger struct{}

func (l logrusLogger) Errorf(format string, args ...interface{}) {
func (l logrusLogger) Errorf(format string, args ...any) {
logrus.Errorf(format, args...)
}

func (l logrusLogger) Debugf(format string, args ...interface{}) {
func (l logrusLogger) Debugf(format string, args ...any) {
logrus.Debugf(format, args...)
}

Expand Down
8 changes: 3 additions & 5 deletions cmd/podman/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"runtime"
"runtime/pprof"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -452,11 +453,8 @@ func loggingHook() {
}
logLevel = "debug"
}
for _, l := range common.LogLevels {
if l == strings.ToLower(logLevel) {
found = true
break
}
if slices.Contains(common.LogLevels, strings.ToLower(logLevel)) {
found = true
}
if !found {
fmt.Fprintf(os.Stderr, "Log Level %q is not supported, choose from: %s\n", logLevel, strings.Join(common.LogLevels, ", "))
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/system/df.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func printVerbose(cmd *cobra.Command, reports *entities.SystemDfReport) error {
return writeTemplate(rpt, hdrs, dfVolumes)
}

func writeTemplate(rpt *report.Formatter, hdrs []map[string]string, output interface{}) error {
func writeTemplate(rpt *report.Formatter, hdrs []map[string]string, output any) error {
if rpt.RenderHeaders {
if err := rpt.Execute(hdrs); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func RemoveSlash(input []string) []string {
return output
}

func PrintGenericJSON(data interface{}) error {
func PrintGenericJSON(data any) error {
enc := json.NewEncoder(os.Stdout)
// by default, json marshallers will force utf=8 from
// a string.
Expand Down
9 changes: 4 additions & 5 deletions cmd/podman/validate/choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validate

import (
"fmt"
"slices"
"strings"
)

Expand Down Expand Up @@ -29,11 +30,9 @@ func (c *ChoiceValue) String() string {
}

func (c *ChoiceValue) Set(value string) error {
for _, v := range c.choices {
if v == value {
*c.value = value
return nil
}
if slices.Contains(c.choices, value) {
*c.value = value
return nil
}
return fmt.Errorf("%q is not a valid value. Choose from: %q", value, c.Choices())
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/quadlet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func logToKmsg(s string) bool {
return true
}

func Logf(format string, a ...interface{}) {
func Logf(format string, a ...any) {
s := fmt.Sprintf(format, a...)
line := fmt.Sprintf("quadlet-generator[%d]: %s", os.Getpid(), s)

Expand All @@ -84,7 +84,7 @@ func enableDebug() {
debugEnabled = true
}

func Debugf(format string, a ...interface{}) {
func Debugf(format string, a ...any) {
if debugEnabled {
Logf(format, a...)
}
Expand Down Expand Up @@ -421,11 +421,11 @@ func generateUnitsInfoMap(units []*parser.UnitFile) map[string]*quadlet.UnitInfo
// quadletLogger implements the logiface.Logger interface using quadlet's custom logging
type quadletLogger struct{}

func (l quadletLogger) Errorf(format string, args ...interface{}) {
func (l quadletLogger) Errorf(format string, args ...any) {
Logf(format, args...)
}

func (l quadletLogger) Debugf(format string, args ...interface{}) {
func (l quadletLogger) Debugf(format string, args ...any) {
Debugf(format, args...)
}

Expand Down
2 changes: 1 addition & 1 deletion hack/podman-registry-go/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestStartAndStopMultipleRegistries(t *testing.T) {

// Start registries.
var errors *multierror.Error
for i := 0; i < 3; i++ {
for range 3 {
reg, err := StartWithOptions(registryOptions)
if err != nil {
errors = multierror.Append(errors, err)
Expand Down
17 changes: 4 additions & 13 deletions libpod/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"net"
"os"
"strings"
Expand Down Expand Up @@ -624,13 +625,9 @@ func (c *Container) Stdin() bool {
return c.config.Stdin
}

// Labels returns the container's labels
// Labels returns the container's labels.
func (c *Container) Labels() map[string]string {
labels := make(map[string]string)
for key, value := range c.config.Labels {
labels[key] = value
}
return labels
return maps.Clone(c.config.Labels)
}

// StopSignal is the signal that will be used to stop the container
Expand Down Expand Up @@ -1038,13 +1035,7 @@ func (c *Container) BindMounts() (map[string]string, error) {
}
}

newMap := make(map[string]string, len(c.state.BindMounts))

for key, val := range c.state.BindMounts {
newMap[key] = val
}

return newMap, nil
return maps.Clone(c.state.BindMounts), nil
}

// StoppedByUser returns whether the container was last stopped by an explicit
Expand Down
9 changes: 2 additions & 7 deletions libpod/container_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"strings"

"github.com/containers/buildah"
Expand Down Expand Up @@ -130,13 +131,7 @@ func (c *Container) Commit(ctx context.Context, destImage string, options Contai
// Only include anonymous named volumes added by the user by
// default.
for _, v := range c.config.NamedVolumes {
include := false
for _, userVol := range c.config.UserVolumes {
if userVol == v.Dest {
include = true
break
}
}
include := slices.Contains(c.config.UserVolumes, v.Dest)
if include {
vol, err := c.runtime.GetVolume(v.Name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion libpod/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ func (c *Container) exec(config *ExecConfig, streams *define.AttachStreams, resi
// errors.
func (c *Container) cleanupExecBundle(sessionID string) (err error) {
path := c.execBundlePath(sessionID)
for attempts := 0; attempts < 50; attempts++ {
for range 50 {
err = os.RemoveAll(path)
if err == nil || os.IsNotExist(err) {
return nil
Expand Down
15 changes: 3 additions & 12 deletions libpod/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package libpod
import (
"errors"
"fmt"
"maps"
"strings"

"github.com/containers/podman/v5/libpod/define"
Expand Down Expand Up @@ -414,19 +415,9 @@ func (c *Container) generateInspectContainerConfig(spec *spec.Spec) *define.Insp
ctrConfig.Entrypoint = c.config.Entrypoint
}

if len(c.config.Labels) != 0 {
ctrConfig.Labels = make(map[string]string)
for k, v := range c.config.Labels {
ctrConfig.Labels[k] = v
}
}
ctrConfig.Labels = maps.Clone(c.config.Labels)
ctrConfig.Annotations = maps.Clone(spec.Annotations)

if len(spec.Annotations) != 0 {
ctrConfig.Annotations = make(map[string]string)
for k, v := range spec.Annotations {
ctrConfig.Annotations[k] = v
}
}
ctrConfig.StopSignal = signal.ToDockerFormat(c.config.StopSignal)
// TODO: should JSON deep copy this to ensure internal pointers don't
// leak.
Expand Down
21 changes: 5 additions & 16 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/fs"
"maps"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -350,12 +351,7 @@ func (c *Container) handleRestartPolicy(ctx context.Context) (_ bool, retErr err
// Returns true if the container is in one of the given states,
// or false otherwise.
func (c *Container) ensureState(states ...define.ContainerStatus) bool {
for _, state := range states {
if state == c.state.State {
return true
}
}
return false
return slices.Contains(states, c.state.State)
}

// Sync this container with on-disk state and runtime status
Expand Down Expand Up @@ -476,19 +472,14 @@ func (c *Container) setupStorage(ctx context.Context) error {
// privileged containers or '--ipc host' only ProcessLabel will
// be set and so we will skip it for cases like that.
if options.Flags == nil {
options.Flags = make(map[string]interface{})
options.Flags = make(map[string]any)
}
options.Flags["ProcessLabel"] = c.config.ProcessLabel
options.Flags["MountLabel"] = c.config.MountLabel
}
if c.config.Privileged {
privOpt := func(opt string) bool {
for _, privopt := range []string{"nodev", "nosuid", "noexec"} {
if opt == privopt {
return true
}
}
return false
return slices.Contains([]string{"nodev", "nosuid", "noexec"}, opt)
}

defOptions, err := storage.GetMountOptions(c.runtime.store.GraphDriverName(), c.runtime.store.GraphOptions())
Expand Down Expand Up @@ -2479,9 +2470,7 @@ func (c *Container) setupOCIHooks(ctx context.Context, config *spec.Spec) (map[s
if len(ociHooks) > 0 || config.Hooks != nil {
logrus.Warnf("Implicit hook directories are deprecated; set --ociHooks-dir=%q explicitly to continue to load ociHooks from this directory", hDir)
}
for i, hook := range ociHooks {
allHooks[i] = hook
}
maps.Copy(allHooks, ociHooks)
}
} else {
manager, err := hooks.New(ctx, c.runtime.config.Engine.HooksDir.Get(), []string{"precreate", "poststop"})
Expand Down
Loading