Skip to content

Commit

Permalink
Style: Enforce maximum of 2 import groups for go files
Browse files Browse the repository at this point in the history
There isn't a whole lot of direction from the go project or tools for this.
A quick google search did turn up 2 projects that also document 2 groups, both
[Uber] and [CockroachDB].
We do have both goimports and gofumpt splitting stdlib vs others out if they
are mixed. But they don't do any futher kind of grouping. So in the absence of
concrete handling lets just settle on 2 because its easy to enforce. The script
below deletes newlines within `import ( ... )` blocks and then runs gofumpt which
will split and sort 2 at most 2 groups.

[Uber]: https://github.com/uber-go/guide/blob/22f61e283ac2f75ba34b62f565581cc70e35fb7a/style.md#import-group-ordering
[CockroachDB]: https://web.archive.org/web/20210509102622/https://wiki.crdb.io/wiki/spaces/CRDB/pages/181371303/Go+Golang+coding+guidelines#Import-Group-Ordering

Signed-off-by: Manuel Mendez <[email protected]>
  • Loading branch information
mmlb committed Dec 6, 2021
1 parent 3528b9e commit 1bbd307
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ One must support their proposed changes with unit tests.
As you submit a pull request(PR) the CI generates a code coverage report.
It will help you identify parts of the code that are not yet covered in unit tests.

#### Go

##### Import Groups

There should be two groups of import blocks, one for stdlib and the other for everything else.

## Understanding code structure

This is a nonexhaustive list important packages that happen to cover most of the code base.
Expand Down
4 changes: 4 additions & 0 deletions ci-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ if ! nixfmt shell.nix; then
failed=1
fi

if ! git ls-files '*.go' | xargs -I% sh -c 'sed -i "/^import (/,/^)/ { /^\s*$/ d }" % && gofumpt -w -s %'; then
failed=1
fi

if ! git diff | (! grep .); then
failed=1
fi
Expand Down

0 comments on commit 1bbd307

Please sign in to comment.