Organizes Go imports according to OpenShift best practices
Organizes Go imports into the following groups:
- standard - Any of the Go standard library packages
- other - Anything not specifically called out in this list
- kubernetes - Anything that starts with
k8s.io
- openshift - Anything that starts with
github.com/openshift
- intermediates - Optional list of groups
- module - Anything that is part of the current module
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
istorage "github.com/containers/image/v5/storage"
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
restclient "k8s.io/client-go/rest"
buildapiv1 "github.com/openshift/api/build/v1"
buildscheme "github.com/openshift/client-go/build/clientset/versioned/scheme"
buildclientv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
"github.com/openshift/library-go/pkg/git"
"github.com/openshift/library-go/pkg/serviceability"
s2iapi "github.com/openshift/source-to-image/pkg/api"
s2igit "github.com/openshift/source-to-image/pkg/scm/git"
bld "github.com/openshift/builder/pkg/build/builder"
"github.com/openshift/builder/pkg/build/builder/cmd/scmauth"
"github.com/openshift/builder/pkg/build/builder/timing"
builderutil "github.com/openshift/builder/pkg/build/builder/util"
utillog "github.com/openshift/builder/pkg/build/builder/util/log"
"github.com/openshift/builder/pkg/version"
)
# Install using go get
$ go get -u github.com/openshift-eng/openshift-goimports
Usage:
openshift-goimports [flags]
Flags:
-h, --help help for openshift-goimports
-i, --intermediate stringArray Names of go modules to put between openshift and module to organize. Example usage: -i github.com/thirdy/one -i thirdy.io/two
-l, --list List files whose imports are not sorted without making changes
-m, --module string The name of the go module. Example: github.com/example-org/example-repo (optional)
-p, --path string The path to the go module to organize. Defaults to the current directory. (default ".") (optional)
-d, --dry Dry run only, do not actually make any changes to files
-v, --v Level number for the log level verbosity
Each group is identified by a pattern that is sought as a substring in the import path. For example, the kubernetes group is defined by searching for the substring "k8s.io". Multiple groups can match one import. In such a case, the import is put into the first matching group in the following list.
- the module to organize
- the intermediate modules, in the order given on the command line
- kubernetes
- openshift
- other
An import whose path matches no other group's pattern is put in the standard group.
openshift-goimports
will try to automatically determine the module using the go.mod
file, if present, at the provided path location.
# Basic usage, command executed against current directory
$ openshift-goimports
# Basic usage with command executed in provided directory
$ openshift-goimports --module github.com/example-org/example-repo --path ~/go/src/example-org/example-repo
This file will ensure that the github.com/openshift-eng/openshift-goimports
repo is vendored into your project.
//go:build tools
// +build tools
package hack
// Add tools that hack scripts depend on here, to ensure they are vendored.
import (
_ "github.com/openshift-eng/openshift-goimports"
)
This file will check if there are any go files that need to be formatted. If there are, it will print a list of them, and exit with status one (1), otherwise it will exit with status zero (0).
#!/bin/bash
bad_files=$(go run ./vendor/github.com/openshift-eng/openshift-goimports -m github.com/example/example-repo -l)
if [[ -n "${bad_files}" ]]; then
echo "!!! openshift-goimports needs to be run on the following files:"
echo "${bad_files}"
echo "Try running 'make imports'"
exit 1
fi
imports: ## Organize imports in go files using openshift-goimports. Example: make imports
go run ./vendor/github.com/openshift-eng/openshift-goimports/ -m github.com/example/example-repo
.PHONY: imports
verify-imports: ## Run import verifications. Example: make verify-imports
hack/verify-imports.sh
.PHONY: verify-imports