[lxc-devel] [crio-lxc/master] add lint target, clean up some trivial things

mikemccracken on Github lxc-bot at linuxcontainers.org
Mon Feb 10 23:59:16 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 304 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200210/1d560900/attachment-0001.bin>
-------------- next part --------------
From 282de8dec8e79cada960186e9cabba07b611a42f Mon Sep 17 00:00:00 2001
From: Michael McCracken <mikmccra at cisco.com>
Date: Mon, 10 Feb 2020 15:28:15 -0800
Subject: [PATCH 1/3] create: propagate errors from ensureShell function

Fixes #9

Signed-off-by: Michael McCracken <mikmccra at cisco.com>
---
 cmd/create.go | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/cmd/create.go b/cmd/create.go
index 6e6ed4b..0c52d23 100644
--- a/cmd/create.go
+++ b/cmd/create.go
@@ -54,32 +54,33 @@ var NamespaceMap = map[string]string{
 	"uts":     "uts",
 }
 
-func ensureShell(rootfs string) {
+func ensureShell(rootfs string) error {
 	shPath := filepath.Join(rootfs, "bin/sh")
 	if exists, _ := pathExists(shPath); exists {
-		return
+		return nil
 	}
 	var err error
 	err = RunCommand("mkdir", filepath.Join(rootfs, "bin"))
 	if err != nil {
-		fmt.Printf("Failed doing mkdir: %v\n", err)
+		return errors.Wrapf(err, "Failed doing mkdir")
 	}
 	err = RunCommand("cp", "/bin/busybox", filepath.Join(rootfs, "bin/"))
 	if err != nil {
-		fmt.Printf("Failed copying busybox: %v\n", err)
+		return errors.Wrapf(err, "Failed copying busybox")
 	}
 	err = RunCommand("ln", filepath.Join(rootfs, "bin/busybox"), filepath.Join(rootfs, "bin/stat"))
 	if err != nil {
-		fmt.Printf("Failed linking stat: %v\n", err)
+		return errors.Wrapf(err, "Failed linking stat")
 	}
 	err = RunCommand("ln", filepath.Join(rootfs, "bin/busybox"), filepath.Join(rootfs, "bin/sh"))
 	if err != nil {
-		fmt.Printf("Failed linking sh: %v\n", err)
+		return errors.Wrapf(err, "Failed linking sh")
 	}
 	err = RunCommand("ln", filepath.Join(rootfs, "bin/busybox"), filepath.Join(rootfs, "bin/tee"))
 	if err != nil {
-		fmt.Printf("Failed linking tee : %v\n", err)
+		return errors.Wrapf(err, "Failed linking tee")
 	}
+	return nil
 }
 
 const (
@@ -243,7 +244,9 @@ func configureContainer(ctx *cli.Context, c *lxc.Container, spec *specs.Spec) er
 		return errors.Wrapf(err, "couldn't write wrapper init")
 	}
 
-	ensureShell(spec.Root.Path)
+	if err := ensureShell(spec.Root.Path); err != nil {
+		return errors.Wrap(err, "couldn't ensure a shell exists in container")
+	}
 
 	if err := c.SetConfigItem("lxc.init.cwd", spec.Process.Cwd); err != nil {
 		return errors.Wrap(err, "failed to set CWD")

From 3718a05f3eb22bd0d3ba9979d3aa284cc44e4cee Mon Sep 17 00:00:00 2001
From: Michael McCracken <mikmccra at cisco.com>
Date: Mon, 10 Feb 2020 15:31:02 -0800
Subject: [PATCH 2/3] add linter target to makefile

adds initial lint.yaml with some common annoying messages excluded.

assumes you have golangci-lint installed.

Signed-off-by: Michael McCracken <mikmccra at cisco.com>
---
 Makefile  |  3 +++
 lint.yaml | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 lint.yaml

diff --git a/Makefile b/Makefile
index e92cbd1..1d8f2b2 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,9 @@ COMMIT=$(if $(shell git status --porcelain --untracked-files=no),$(COMMIT_HASH)-
 TEST?=$(patsubst test/%.bats,%,$(wildcard test/*.bats))
 PACKAGES_DIR?=~/packages
 
+lint:
+	golangci-lint run -c ./lint.yaml ./...
+
 crio-lxc: $(GO_SRC)
 	go build -ldflags "-X main.version=$(COMMIT)" -o crio-lxc ./cmd
 
diff --git a/lint.yaml b/lint.yaml
new file mode 100644
index 0000000..f58ce3b
--- /dev/null
+++ b/lint.yaml
@@ -0,0 +1,17 @@
+issues:
+  exclude:
+    - 'Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv). is not checked'
+    - 'error strings should not be capitalized'
+    - 'error strings should not end with punctuation'
+    - 'File is not `goimports`-ed'
+    - 'has \d* occurrences, make it a constant'
+    - 'line is \d* characters'
+    - 'is a global variable'
+    - 'ifElseChain: rewrite if-else to switch statement'
+    - 'Error return value of `.*` is not checked'
+    - 'cyclomatic complexity \d* of func'
+    - 'G107: Potential HTTP request made with variable url'
+    - 'should have name of the form ErrFoo'
+    - 'naked return in func'
+    - 'by other packages, and that stutters; consider calling this'
+    - 'File is not `gofmt`-ed with `-s`'

From fa3e39e1e3873b530feb280184df64dec3024145 Mon Sep 17 00:00:00 2001
From: Michael McCracken <mikmccra at cisco.com>
Date: Mon, 10 Feb 2020 15:34:59 -0800
Subject: [PATCH 3/3] minor lint fixes

Signed-off-by: Michael McCracken <mikmccra at cisco.com>
---
 cmd/create.go | 2 +-
 cmd/state.go  | 2 +-
 cmd/utils.go  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/cmd/create.go b/cmd/create.go
index 0c52d23..f0f20a5 100644
--- a/cmd/create.go
+++ b/cmd/create.go
@@ -105,7 +105,7 @@ func configureNamespaces(c *lxc.Container, spec *specs.Spec) error {
 	var configVal string
 	seenNamespaceTypes := map[specs.LinuxNamespaceType]bool{}
 	for _, ns := range spec.Linux.Namespaces {
-		if _, ok := seenNamespaceTypes[ns.Type]; ok == true {
+		if _, ok := seenNamespaceTypes[ns.Type]; ok {
 			return fmt.Errorf("duplicate namespace type %s", ns.Type)
 		}
 		seenNamespaceTypes[ns.Type] = true
diff --git a/cmd/state.go b/cmd/state.go
index 6494752..c98918e 100644
--- a/cmd/state.go
+++ b/cmd/state.go
@@ -109,7 +109,7 @@ func doState(ctx *cli.Context) error {
 	if err != nil {
 		return errors.Wrap(err, "failed to marshal json")
 	}
-	fmt.Fprintf(os.Stdout, string(stateJson))
+	fmt.Fprint(os.Stdout, string(stateJson))
 
 	return nil
 }
diff --git a/cmd/utils.go b/cmd/utils.go
index a83bf13..aa3a45b 100644
--- a/cmd/utils.go
+++ b/cmd/utils.go
@@ -31,7 +31,7 @@ func readBundleSpec(specFilePath string) (spec *specs.Spec, err error) {
 
 func configureLogging(ctx *cli.Context, c *lxc.Container) error {
 	if ctx.GlobalIsSet("log-level") {
-		logLevel := lxc.TRACE
+		var logLevel lxc.LogLevel
 		switch ctx.GlobalString("log-level") {
 		case "trace":
 			logLevel = lxc.TRACE


More information about the lxc-devel mailing list