Skip to content

Commit

Permalink
Merge pull request #1179 from manugupt1/static-check
Browse files Browse the repository at this point in the history
Implements static check in nerdctl CI
  • Loading branch information
AkihiroSuda committed Jul 3, 2022
2 parents 130c12d + 86056b8 commit 137f753
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ linters:
# - rowserrcheck
# - scopelint
# - sqlclosecheck
# - staticcheck
- staticcheck
- stylecheck
# - testpackage
# - tparallel
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/apparmor_inspect_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func apparmorInspectAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), b)
_, err = fmt.Fprint(cmd.OutOrStdout(), b)
return err
}
5 changes: 4 additions & 1 deletion cmd/nerdctl/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"strings"
"text/template"

"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/containerd/containerd/api/services/introspection/v1"
"github.com/containerd/nerdctl/pkg/infoutil"
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
Expand Down Expand Up @@ -231,7 +234,7 @@ func prettyPrintInfoDockerCompat(cmd *cobra.Command, info *dockercompat.Info) er
if k == "name" {
continue
}
fmt.Fprintf(w, " %s: %s\n", strings.Title(k), v)
fmt.Fprintf(w, " %s: %s\n", cases.Title(language.English).String(k), v)
}
}
fmt.Fprintf(w, " Kernel Version: %s\n", info.KernelVersion)
Expand Down
3 changes: 2 additions & 1 deletion cmd/nerdctl/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"

"github.com/moby/sys/signal"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func killAction(cmd *cobra.Command, args []string) error {
killSignal = "SIG" + killSignal
}

signal, err := containerd.ParseSignal(killSignal)
signal, err := signal.ParseSignal(killSignal)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/nerdctl/main_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func shellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete s
logrus.Warn(err)
return nil, cobra.ShellCompDirectiveError
}
candidates := []string{}
var candidates []string
candidates = append(candidates, nsList...)
return candidates, cobra.ShellCompDirectiveNoFileComp
}
Expand All @@ -62,7 +62,7 @@ func shellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
candidates := []string{}
var candidates []string
candidates = append(candidates, snapshotterPlugins...)
return candidates, cobra.ShellCompDirectiveNoFileComp
}
8 changes: 5 additions & 3 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ import (
"github.com/containerd/nerdctl/pkg/referenceutil"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/containerd/nerdctl/pkg/taskutil"
"github.com/docker/cli/opts" // nolint: stylecheck
dopts "github.com/docker/cli/opts" // nolint: stylecheck
dopts "github.com/docker/cli/opts"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -926,7 +925,7 @@ func readKVStringsMapfFromLabel(cmd *cobra.Command) (map[string]string, error) {
return nil, err
}
labelsFilePath = strutil.DedupeStrSlice(labelsFilePath)
labels, err := opts.ReadKVStrings(labelsFilePath, labelsMap)
labels, err := dopts.ReadKVStrings(labelsFilePath, labelsMap)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1068,6 +1067,9 @@ func writeCIDFile(path, id string) error {
return fmt.Errorf("container ID file found, make sure the other container isn't running or delete %s", path)
} else if errors.Is(err, os.ErrNotExist) {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
if err != nil {
return fmt.Errorf("failed to create the container ID file: %s", err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,6 @@ func statsAction(cmd *cobra.Command, args []string) error {
return errors.New("unsupported format: \"raw\"")
default:
tmpl, err = parseTemplate(format)
if err != nil {
break
}
}

for _, c := range ccstats {
Expand Down
14 changes: 8 additions & 6 deletions cmd/nerdctl/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import (
"fmt"
"time"

"github.com/spf13/cobra"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/labels"

"github.com/moby/sys/signal"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func newStopCommand() *cobra.Command {
Expand Down Expand Up @@ -138,18 +140,18 @@ func stopContainer(ctx context.Context, container containerd.Container, timeout
}

if *timeout > 0 {
signal, err := containerd.ParseSignal("SIGTERM")
sig, err := signal.ParseSignal("SIGTERM")
if err != nil {
return err
}
if stopSignal, ok := l[containerd.StopSignalLabel]; ok {
signal, err = containerd.ParseSignal(stopSignal)
sig, err = signal.ParseSignal(stopSignal)
if err != nil {
return err
}
}

if err := task.Kill(ctx, signal); err != nil {
if err := task.Kill(ctx, sig); err != nil {
return err
}

Expand All @@ -176,12 +178,12 @@ func stopContainer(ctx context.Context, container containerd.Container, timeout
}
}

signal, err := containerd.ParseSignal("SIGKILL")
sig, err := signal.ParseSignal("SIGKILL")
if err != nil {
return err
}

if err := task.Kill(ctx, signal); err != nil {
if err := task.Kill(ctx, sig); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func validatePSArgs(psArgs string) error {
// So we use fieldsASCII instead of strings.Fields in parsePSOutput.
// See https://github.com/docker/docker/pull/24358
// nolint: gosimple
re := regexp.MustCompile("\\s+([^\\s]*)=\\s*(PID[^\\s]*)")
re := regexp.MustCompile(`\s+(\S*)=\s*(PID\S*)`)
for _, group := range re.FindAllStringSubmatch(psArgs, -1) {
if len(group) >= 3 {
k := group[1]
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/sys/signal v0.7.0 // indirect
github.com/moby/sys/signal v0.7.0
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
Expand Down Expand Up @@ -166,7 +166,7 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.0 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.7
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20220426171045-31bebdecfb46 // indirect
google.golang.org/grpc v1.47.0 // indirect
Expand Down
14 changes: 9 additions & 5 deletions pkg/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"strings"
"time"

"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/oci"
Expand All @@ -38,6 +41,7 @@ func ContainerStatus(ctx context.Context, c containerd.Container) string {
// Just in case, there is something wrong in server.
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
titleCaser := cases.Title(language.English)

task, err := c.Task(ctx, nil)
if err != nil {
Expand All @@ -46,18 +50,18 @@ func ContainerStatus(ctx context.Context, c containerd.Container) string {
// when it exits. So, the status will be "created" for this
// case.
if errdefs.IsNotFound(err) {
return strings.Title(string(containerd.Created))
return titleCaser.String(string(containerd.Created))
}
return strings.Title(string(containerd.Unknown))
return titleCaser.String(string(containerd.Unknown))
}

status, err := task.Status(ctx)
if err != nil {
return strings.Title(string(containerd.Unknown))
return titleCaser.String(string(containerd.Unknown))
}
labels, err := c.Labels(ctx)
if err != nil {
return strings.Title(string(containerd.Unknown))
return titleCaser.String(string(containerd.Unknown))
}

switch s := status.Status; s {
Expand All @@ -69,7 +73,7 @@ func ContainerStatus(ctx context.Context, c containerd.Container) string {
case containerd.Running:
return "Up" // TODO: print "status.UpTime" (inexistent yet)
default:
return strings.Title(string(s))
return titleCaser.String(string(s))
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/imgutil/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func Pull(ctx context.Context, client *containerd.Client, ref string, config *Co
opts := []containerd.RemoteOpt{
containerd.WithResolver(config.Resolver),
containerd.WithImageHandler(h),
containerd.WithSchema1Conversion,
//nolint:staticcheck
containerd.WithSchema1Conversion, //lint:ignore SA1019 nerdctl should support schema1 as well.
containerd.WithPlatformMatcher(platformMC),
}
opts = append(opts, config.RemoteOpts...)
Expand Down
3 changes: 3 additions & 0 deletions pkg/mountutil/mountutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ func ProcessFlagV(s string, volStore volumestore.VolumeStore) (*Processed, error
fstype = "bind"
found = true
}
if found {
break
}
}
if !found {
options = append(options, "rbind")
Expand Down
4 changes: 0 additions & 4 deletions pkg/portutil/port_allocate_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func portAllocate(protocol string, ip string, count uint64) (uint64, uint64, err
usedPort[value.LocalPort] = true
}
start := uint64(allocateStart)
var results []uint64
if count > uint64(allocateEnd-allocateStart+1) {
return 0, 0, fmt.Errorf("can not allocate %d ports", count)
}
Expand All @@ -85,9 +84,6 @@ func portAllocate(protocol string, ip string, count uint64) (uint64, uint64, err
}
}
if needReturn {
for i := start; i < start+count; i++ {
results = append(results, i)
}
return start, start + count - 1, nil
}
start += count
Expand Down

0 comments on commit 137f753

Please sign in to comment.