The "cargo fmt" code style checking tool is something we wish to run on all
Rust 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(a)redhat.com>
---
.gitlab-ci.yml | 5 +++++
containers/cargo-fmt/Dockerfile | 11 +++++++++++
containers/cargo-fmt/README.rst | 20 ++++++++++++++++++++
containers/cargo-fmt/cargo-fmt.sh | 22 ++++++++++++++++++++++
4 files changed, 58 insertions(+)
create mode 100644 containers/cargo-fmt/Dockerfile
create mode 100644 containers/cargo-fmt/README.rst
create mode 100755 containers/cargo-fmt/cargo-fmt.sh
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 080c8d1..e5f121f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -27,6 +27,11 @@ go-fmt-container:
variables:
NAME: go-fmt
+cargo-fmt-container:
+ <<: *build_container_definition
+ variables:
+ NAME: cargo-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/cargo-fmt/Dockerfile b/containers/cargo-fmt/Dockerfile
new file mode 100644
index 0000000..0ed1c46
--- /dev/null
+++ b/containers/cargo-fmt/Dockerfile
@@ -0,0 +1,11 @@
+FROM rust:1.43
+
+RUN export DEBIAN_FRONTEND=noninteractive && \
+ apt-get update && \
+ apt-get install --no-install-recommends -y \
+ diffstat && \
+ apt-get autoclean -y
+
+RUN rustup component add rustfmt
+
+COPY cargo-fmt.sh /cargo-fmt
diff --git a/containers/cargo-fmt/README.rst b/containers/cargo-fmt/README.rst
new file mode 100644
index 0000000..00f50a2
--- /dev/null
+++ b/containers/cargo-fmt/README.rst
@@ -0,0 +1,20 @@
+================================================
+Container for running cargo fmt code style check
+================================================
+
+This container provides a simple way to invoke ``cargo fmt`` to validate code
+style across a Rust codebase. It should be integrated into CI by adding
+the following snippet to ``.gitlab-ci.yml``
+
+::
+
+ cargo-fmt:
+ stage: prebuild
+ image:
registry.gitlab.com/libvirt/libvirt-ci/cargo-fmt:master
+ script:
+ - /cargo-fmt
+ artifacts:
+ paths:
+ - cargo-fmt.patch
+ expire_in: 1 week
+ when: on_failure
diff --git a/containers/cargo-fmt/cargo-fmt.sh b/containers/cargo-fmt/cargo-fmt.sh
new file mode 100755
index 0000000..53fb0df
--- /dev/null
+++ b/containers/cargo-fmt/cargo-fmt.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+cargo fmt -v -- --check > cargo-fmt.patch
+
+if test -s cargo-fmt.patch
+then
+ echo
+ echo "❌ ERROR: some files failed cargo fmt code style check"
+ echo
+ diffstat cargo-fmt.patch
+ echo
+ echo "See the cargo-fmt patch artifact for full details of mistakes."
+ echo
+ echo "For guidance on how to configure Emacs or Vim to automatically"
+ echo "run cargo fmt when saving files read"
+ echo
+ echo "
https://github.com/rust-lang/rustfmt"
+ echo
+ exit 1
+fi
+
+echo "✔ OK: all files passed cargo fmt code style check"
--
2.26.2