diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index de51daaf70..ee75b046b5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -57,6 +57,13 @@ The coding style suggested by the Golang community is used in installer. See the Terraform has similar standards, and you can run `terraform fmt` to rewrite Terraform files to the canonical format. +### Import order + +The Golang import statements are organized into 4 sections: standard, default, prefix("github.com/openshift"), and blank imports with the [gci tool][gci-tool]. To automatically sort the imports run: +```sh +hack/go-fmt.sh . +``` + ## Commit Message Format We follow a rough convention for commit messages that is designed to answer two @@ -95,6 +102,7 @@ Some unit tests use mocks that are generated with gomock. If the underlying inte hack/go-genmock.sh ``` +[gci-tool]: https://github.com/daixiang0/gci [golang-style]: https://github.com/golang/go/wiki/CodeReviewComments [new-issue]: https://github.com/openshift/installer/issues/new [podman-install]: https://podman.io/getting-started/installation diff --git a/hack/go-fmt.sh b/hack/go-fmt.sh index 8bba626422..9263b1dfef 100755 --- a/hack/go-fmt.sh +++ b/hack/go-fmt.sh @@ -1,7 +1,9 @@ #!/bin/sh if [ "$IS_CONTAINER" != "" ]; then + gci_repo=github.com/daixiang0/gci for TARGET in "${@}"; do find "${TARGET}" -name '*.go' ! -path '*/vendor/*' ! -path '*/.build/*' -exec gofmt -s -w {} \+ + find "${TARGET}" -name '*.go' ! -path '*/vendor/*' ! -path '*/.build/*' -exec go run "$gci_repo" write -s standard -s default -s "prefix(github.com/openshift)" -s blank --skip-generated {} \+ done git diff --exit-code else diff --git a/tools.go b/tools.go index ecb8364504..fd67f9a45c 100644 --- a/tools.go +++ b/tools.go @@ -6,13 +6,12 @@ package tools -import ( - // dependency of hack/go-lint.sh - _ "golang.org/x/lint" - - // dependency of generating CRD for install-config - _ "sigs.k8s.io/controller-tools/cmd/controller-gen" - +import ( // dependency of hack/go-fmt.sh + _ "github.com/daixiang0/gci" // used to generate mocks _ "github.com/golang/mock/mockgen" + // dependency of hack/go-lint.sh + _ "golang.org/x/lint" + // dependency of generating CRD for install-config + _ "sigs.k8s.io/controller-tools/cmd/controller-gen" )