From 200cfa41a434b7143620c2c252b3eb7ab3ef92f9 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Thu, 11 Jun 2020 14:40:38 -0400 Subject: Turn on More linters - misspell - prealloc - unparam - nakedret Signed-off-by: Daniel J Walsh --- cmd/podman/common/specgen.go | 27 ++++++++++++--------------- cmd/podman/containers/mount.go | 4 ++-- cmd/podman/containers/ps.go | 9 +++++---- cmd/podman/containers/stats.go | 4 ++-- cmd/podman/generate/systemd.go | 2 +- cmd/podman/images/history.go | 2 +- cmd/podman/inspect/inspect.go | 4 ++-- cmd/podman/networks/list.go | 5 +---- cmd/podman/pods/ps.go | 8 ++++---- cmd/podman/system/df.go | 8 ++++---- 10 files changed, 34 insertions(+), 39 deletions(-) (limited to 'cmd/podman') diff --git a/cmd/podman/common/specgen.go b/cmd/podman/common/specgen.go index 2a5ebea3c..0b44ef544 100644 --- a/cmd/podman/common/specgen.go +++ b/cmd/podman/common/specgen.go @@ -23,7 +23,7 @@ import ( "github.com/pkg/errors" ) -func getCPULimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) (*specs.LinuxCPU, error) { +func getCPULimits(c *ContainerCLIOpts) *specs.LinuxCPU { cpu := &specs.LinuxCPU{} hasLimits := false @@ -67,12 +67,12 @@ func getCPULimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) } if !hasLimits { - return nil, nil + return nil } - return cpu, nil + return cpu } -func getIOLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) (*specs.LinuxBlockIO, error) { +func getIOLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts) (*specs.LinuxBlockIO, error) { var err error io := &specs.LinuxBlockIO{} hasLimits := false @@ -87,7 +87,7 @@ func getIOLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) ( } if len(c.BlkIOWeightDevice) > 0 { - if err := parseWeightDevices(c.BlkIOWeightDevice, s); err != nil { + if err := parseWeightDevices(s, c.BlkIOWeightDevice); err != nil { return nil, err } hasLimits = true @@ -127,7 +127,7 @@ func getIOLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) ( return io, nil } -func getPidsLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) *specs.LinuxPids { +func getPidsLimits(c *ContainerCLIOpts) *specs.LinuxPids { pids := &specs.LinuxPids{} if c.CGroupsMode == "disabled" && c.PIDsLimit != 0 { return nil @@ -146,7 +146,7 @@ func getPidsLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) return nil } -func getMemoryLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string) (*specs.LinuxMemory, error) { +func getMemoryLimits(s *specgen.SpecGenerator, c *ContainerCLIOpts) (*specs.LinuxMemory, error) { var err error memory := &specs.LinuxMemory{} hasLimits := false @@ -446,19 +446,16 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string if s.ResourceLimits == nil { s.ResourceLimits = &specs.LinuxResources{} } - s.ResourceLimits.Memory, err = getMemoryLimits(s, c, args) - if err != nil { - return err - } - s.ResourceLimits.BlockIO, err = getIOLimits(s, c, args) + s.ResourceLimits.Memory, err = getMemoryLimits(s, c) if err != nil { return err } - s.ResourceLimits.Pids = getPidsLimits(s, c, args) - s.ResourceLimits.CPU, err = getCPULimits(s, c, args) + s.ResourceLimits.BlockIO, err = getIOLimits(s, c) if err != nil { return err } + s.ResourceLimits.Pids = getPidsLimits(c) + s.ResourceLimits.CPU = getCPULimits(c) if s.ResourceLimits.CPU == nil && s.ResourceLimits.Pids == nil && s.ResourceLimits.BlockIO == nil && s.ResourceLimits.Memory == nil { s.ResourceLimits = nil } @@ -700,7 +697,7 @@ func makeHealthCheckFromCli(inCmd, interval string, retries uint, timeout, start return &hc, nil } -func parseWeightDevices(weightDevs []string, s *specgen.SpecGenerator) error { +func parseWeightDevices(s *specgen.SpecGenerator, weightDevs []string) error { for _, val := range weightDevs { split := strings.SplitN(val, ":", 2) if len(split) != 2 { diff --git a/cmd/podman/containers/mount.go b/cmd/podman/containers/mount.go index af4d52caa..7f15616de 100644 --- a/cmd/podman/containers/mount.go +++ b/cmd/podman/containers/mount.go @@ -71,7 +71,6 @@ func init() { func mount(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors - mrs []mountReporter ) reports, err := registry.ContainerEngine().ContainerMount(registry.GetContext(), args, mountOpts) if err != nil { @@ -90,6 +89,7 @@ func mount(cmd *cobra.Command, args []string) error { if mountOpts.Format == "json" { return printJSON(reports) } + mrs := make([]mountReporter, 0, len(reports)) for _, r := range reports { mrs = append(mrs, mountReporter{r}) } @@ -110,7 +110,7 @@ func printJSON(reports []*entities.ContainerMountReport) error { Names []string Mountpoint string `json:"mountpoint"` } - var jreports []jreport + jreports := make([]jreport, 0, len(reports)) for _, r := range reports { jreports = append(jreports, jreport{ diff --git a/cmd/podman/containers/ps.go b/cmd/podman/containers/ps.go index a29b4da3d..ffd2054a6 100644 --- a/cmd/podman/containers/ps.go +++ b/cmd/podman/containers/ps.go @@ -74,7 +74,7 @@ func listFlagSet(flags *pflag.FlagSet) { _ = flags.MarkHidden("latest") } } -func checkFlags(c *cobra.Command, args []string) error { +func checkFlags(c *cobra.Command) error { // latest, and last are mutually exclusive. if listOpts.Last >= 0 && listOpts.Latest { return errors.Errorf("last and latest are mutually exclusive") @@ -144,8 +144,7 @@ func getResponses() ([]entities.ListContainer, error) { } func ps(cmd *cobra.Command, args []string) error { - var responses []psReporter - if err := checkFlags(cmd, args); err != nil { + if err := checkFlags(cmd); err != nil { return err } for _, f := range filters { @@ -172,6 +171,7 @@ func ps(cmd *cobra.Command, args []string) error { return quietOut(listContainers) } + responses := make([]psReporter, 0, len(listContainers)) for _, r := range listContainers { responses = append(responses, psReporter{r}) } @@ -351,7 +351,8 @@ func portsToString(ports []ocicni.PortMapping) string { first int32 last int32 } - var portDisplay []string + portDisplay := []string{} + if len(ports) == 0 { return "" } diff --git a/cmd/podman/containers/stats.go b/cmd/podman/containers/stats.go index 11aa3a4d2..260cbd25d 100644 --- a/cmd/podman/containers/stats.go +++ b/cmd/podman/containers/stats.go @@ -134,7 +134,7 @@ func outputStats(reports []*define.ContainerStats) error { tm.MoveCursor(1, 1) tm.Flush() } - var stats []*containerStats + stats := make([]*containerStats, 0, len(reports)) for _, r := range reports { stats = append(stats, &containerStats{r}) } @@ -228,7 +228,7 @@ func outputJSON(stats []*containerStats) error { BlockIO string `json:"block_io"` Pids string `json:"pids"` } - var jstats []jstat + jstats := make([]jstat, 0, len(stats)) for _, j := range stats { jstats = append(jstats, jstat{ Id: j.ID(), diff --git a/cmd/podman/generate/systemd.go b/cmd/podman/generate/systemd.go index 75031e070..e4fdd8690 100644 --- a/cmd/podman/generate/systemd.go +++ b/cmd/podman/generate/systemd.go @@ -41,7 +41,7 @@ func init() { flags.BoolVarP(&systemdOptions.New, "new", "", false, "Create a new container instead of starting an existing one") flags.StringVar(&systemdOptions.ContainerPrefix, "container-prefix", "container", "Systemd unit name prefix for containers") flags.StringVar(&systemdOptions.PodPrefix, "pod-prefix", "pod", "Systemd unit name prefix for pods") - flags.StringVar(&systemdOptions.Separator, "separator", "-", "Systemd unit name seperator between name/id and prefix") + flags.StringVar(&systemdOptions.Separator, "separator", "-", "Systemd unit name separator between name/id and prefix") flags.SetNormalizeFunc(utils.AliasFlags) } diff --git a/cmd/podman/images/history.go b/cmd/podman/images/history.go index 17a80557e..ea4b9983f 100644 --- a/cmd/podman/images/history.go +++ b/cmd/podman/images/history.go @@ -100,7 +100,7 @@ func history(cmd *cobra.Command, args []string) error { } return err } - var hr []historyreporter + hr := make([]historyreporter, 0, len(results.Layers)) for _, l := range results.Layers { hr = append(hr, historyreporter{l}) } diff --git a/cmd/podman/inspect/inspect.go b/cmd/podman/inspect/inspect.go index 223ce00f0..1ed033ec3 100644 --- a/cmd/podman/inspect/inspect.go +++ b/cmd/podman/inspect/inspect.go @@ -77,7 +77,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 []interface{} //nolint ctx := context.Background() if len(namesOrIDs) == 0 { @@ -132,7 +132,7 @@ func (i *inspector) inspect(namesOrIDs []string) error { } func (i *inspector) inspectAll(ctx context.Context, namesOrIDs []string) ([]interface{}, error) { - var data []interface{} + var data []interface{} //nolint for _, name := range namesOrIDs { imgData, err := i.imageEngine.Inspect(ctx, []string{name}, i.options) if err == nil { diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go index 498a4dc18..747fef26d 100644 --- a/cmd/podman/networks/list.go +++ b/cmd/podman/networks/list.go @@ -56,10 +56,6 @@ func init() { } func networkList(cmd *cobra.Command, args []string) error { - var ( - nlprs []ListPrintReports - ) - // validate the filter pattern. if len(networkListOptions.Filter) > 0 { tokens := strings.Split(networkListOptions.Filter, "=") @@ -82,6 +78,7 @@ func networkList(cmd *cobra.Command, args []string) error { return jsonOut(responses) } + nlprs := make([]ListPrintReports, 0, len(responses)) for _, r := range responses { nlprs = append(nlprs, ListPrintReports{r}) } diff --git a/cmd/podman/pods/ps.go b/cmd/podman/pods/ps.go index bcd1db84c..0171bb243 100644 --- a/cmd/podman/pods/ps.go +++ b/cmd/podman/pods/ps.go @@ -68,7 +68,6 @@ func pods(cmd *cobra.Command, args []string) error { var ( w io.Writer = os.Stdout row string - lpr []ListPodReporter ) if psInput.Quiet && len(psInput.Format) > 0 { @@ -102,6 +101,7 @@ func pods(cmd *cobra.Command, args []string) error { return nil } + lpr := make([]ListPodReporter, 0, len(responses)) for _, r := range responses { lpr = append(lpr, ListPodReporter{r}) } @@ -220,7 +220,7 @@ func (l ListPodReporter) InfraId() string { //nolint } func (l ListPodReporter) ContainerIds() string { - var ctrids []string + ctrids := make([]string, 0, len(l.Containers)) for _, c := range l.Containers { id := c.Id if !noTrunc { @@ -232,7 +232,7 @@ func (l ListPodReporter) ContainerIds() string { } func (l ListPodReporter) ContainerNames() string { - var ctrNames []string + ctrNames := make([]string, 0, len(l.Containers)) for _, c := range l.Containers { ctrNames = append(ctrNames, c.Names) } @@ -240,7 +240,7 @@ func (l ListPodReporter) ContainerNames() string { } func (l ListPodReporter) ContainerStatuses() string { - var statuses []string + statuses := make([]string, 0, len(l.Containers)) for _, c := range l.Containers { statuses = append(statuses, c.Status) } diff --git a/cmd/podman/system/df.go b/cmd/podman/system/df.go index 9318bba12..c56990cb5 100644 --- a/cmd/podman/system/df.go +++ b/cmd/podman/system/df.go @@ -147,15 +147,13 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error { func printVerbose(reports *entities.SystemDfReport) error { var ( - dfImages []*dfImage - dfContainers []*dfContainer - dfVolumes []*dfVolume - w io.Writer = os.Stdout + w io.Writer = os.Stdout ) // Images fmt.Print("\nImages space usage:\n\n") // convert to dfImage for output + dfImages := make([]*dfImage, 0, len(reports.Images)) for _, d := range reports.Images { dfImages = append(dfImages, &dfImage{SystemDfImageReport: d}) } @@ -170,6 +168,7 @@ func printVerbose(reports *entities.SystemDfReport) error { fmt.Print("\nContainers space usage:\n\n") // convert to dfContainers for output + dfContainers := make([]*dfContainer, 0, len(reports.Containers)) for _, d := range reports.Containers { dfContainers = append(dfContainers, &dfContainer{SystemDfContainerReport: d}) } @@ -183,6 +182,7 @@ func printVerbose(reports *entities.SystemDfReport) error { // Volumes fmt.Print("\nLocal Volumes space usage:\n\n") + dfVolumes := make([]*dfVolume, 0, len(reports.Volumes)) // convert to dfVolume for output for _, d := range reports.Volumes { dfVolumes = append(dfVolumes, &dfVolume{SystemDfVolumeReport: d}) -- cgit v1.2.3-54-g00ecf