[libvirt-ci PATCH 2/2] containers: add a standard container for running go fmt

Daniel P. Berrangé berrange at redhat.com
Fri May 1 10:52:32 UTC 2020


The "go fmt" code style checking tool is something we wish to run on all
Go code, so it is useful to have a common container that can be used by
all relevant projects.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 .gitlab-ci.yml               |  5 +++++
 containers/go-fmt/Dockerfile |  9 +++++++++
 containers/go-fmt/README.rst | 20 ++++++++++++++++++++
 containers/go-fmt/go-fmt.sh  | 24 ++++++++++++++++++++++++
 4 files changed, 58 insertions(+)
 create mode 100644 containers/go-fmt/Dockerfile
 create mode 100644 containers/go-fmt/README.rst
 create mode 100755 containers/go-fmt/go-fmt.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index a1ac31a..080c8d1 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -22,6 +22,11 @@ check-dco-container:
   variables:
     NAME: check-dco
 
+go-fmt-container:
+  <<: *build_container_definition
+  variables:
+    NAME: go-fmt
+
 # Check that all commits are signed-off for the DCO. Skip
 # on master branch and -maint branches, since we only need
 # to test developer's personal branches.
diff --git a/containers/go-fmt/Dockerfile b/containers/go-fmt/Dockerfile
new file mode 100644
index 0000000..9079fea
--- /dev/null
+++ b/containers/go-fmt/Dockerfile
@@ -0,0 +1,9 @@
+FROM golang:1.14
+
+RUN export DEBIAN_FRONTEND=noninteractive && \
+    apt-get update && \
+    apt-get install --no-install-recommends -y \
+            diffstat && \
+    apt-get autoclean -y
+
+COPY go-fmt.sh /go-fmt
diff --git a/containers/go-fmt/README.rst b/containers/go-fmt/README.rst
new file mode 100644
index 0000000..0af6998
--- /dev/null
+++ b/containers/go-fmt/README.rst
@@ -0,0 +1,20 @@
+=============================================
+Container for running go fmt code style check
+=============================================
+
+This container provides a simple way to invoke ``go fmt`` to validate code
+style across a Golang codebase. It should be integrated into a CI by adding
+the following snippet to ``.gitlab-ci.yml``
+
+::
+
+   go-fmt:
+     stage: prebuild
+     image: registry.gitlab.com/libvirt/libvirt-ci/go-fmt:master
+     script:
+       - /go-fmt
+     artifacts:
+       paths:
+         - go-fmt.patch
+       expire_in: 1 week
+       when: on_failure
diff --git a/containers/go-fmt/go-fmt.sh b/containers/go-fmt/go-fmt.sh
new file mode 100755
index 0000000..9fda79d
--- /dev/null
+++ b/containers/go-fmt/go-fmt.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+GOFMT=$(go env GOROOT)/bin/gofmt
+
+find -name '*.go' | xargs $GOFMT -d -e > go-fmt.patch
+
+if test -n go-fmt.patch
+then
+    echo
+    echo "❌ ERROR: some files failed go fmt code style check"
+    echo
+    diffstat go-fmt.patch
+    echo
+    echo "See the go-fmt patch artifact for full details of mistakes."
+    echo
+    echo "For guidance on how to configure Emacs or Vim to automatically"
+    echo "run go fmt when saving files read"
+    echo
+    echo "     https://blog.golang.org/gofmt"
+    echo
+    exit 1
+fi
+
+echo "✔ OK: all files passed go fmt code style check"
-- 
2.25.4




More information about the libvir-list mailing list