- .github
-
CHANGELOG
- CHANGELOG-1.10.md
- CHANGELOG-1.11.md
- CHANGELOG-1.12.md
- CHANGELOG-1.13.md
- CHANGELOG-1.14.md
- CHANGELOG-1.15.md
- CHANGELOG-1.16.md
- CHANGELOG-1.17.md
- CHANGELOG-1.18.md
- CHANGELOG-1.19.md
- CHANGELOG-1.2.md
- CHANGELOG-1.20.md
- CHANGELOG-1.21.md
- CHANGELOG-1.22.md
- CHANGELOG-1.23.md
- CHANGELOG-1.24.md
- CHANGELOG-1.25.md
- CHANGELOG-1.26.md
- CHANGELOG-1.27.md
- CHANGELOG-1.28.md
- CHANGELOG-1.29.md
- CHANGELOG-1.3.md
- CHANGELOG-1.30.md
- CHANGELOG-1.31.md
- CHANGELOG-1.4.md
- CHANGELOG-1.5.md
- CHANGELOG-1.6.md
- CHANGELOG-1.7.md
- CHANGELOG-1.8.md
- CHANGELOG-1.9.md
- OWNERS
- README.md
- LICENSES
- api
- build
- cluster
-
cmd
-
clicheck
-
cloud-controller-manager
-
dependencycheck
-
dependencyverifier
-
fieldnamedocscheck
-
gendocs
-
genkubedocs
-
genman
-
genswaggertypedocs
-
genutils
-
genyaml
-
gotemplate
-
import-boss
-
importverifier
-
kube-apiserver
-
kube-controller-manager
-
kube-proxy
-
kube-scheduler
-
kubeadm
-
kubectl
-
kubectl-convert
-
kubelet
-
kubemark
-
preferredimports
-
prune-junit-xml
-
yamlfmt
- OWNERS
-
clicheck
- docs
-
hack
-
boilerplate
-
conformance
-
e2e-internal
-
gen-swagger-doc
-
jenkins
-
lib
-
make-rules
-
testdata
-
tools
-
verify-flags
- .descriptions_failures
- .import-aliases
- .spelling_failures
- OWNERS
- README.md
- _update-generated-proto-bindings-dockerized.sh
- _update-generated-protobuf-dockerized.sh
- apidiff.sh
- benchmark-go.sh
- build-cross.sh
- build-go.sh
- cherry_pick_pull.sh
- dev-build-and-push.sh
- dev-build-and-up.sh
- dev-push-conformance.sh
- e2e-node-test.sh
- generate-docs.sh
- get-build.sh
- ginkgo-e2e.sh
- golangci-hints.yaml
- golangci-strict.yaml
- golangci.yaml
- golangci.yaml.in
- grab-profiles.sh
- install-etcd.sh
- install-protoc.sh
- lint-dependencies.sh
- list-feature-tests.sh
- local-up-cluster.sh
- logcheck.conf
- module-graph.sh
- pin-dependency.sh
- print-workspace-status.sh
- run-prometheus-on-etcd-scrapes.sh
- serve-prom-scrapes.sh
- test-go.sh
- test-integration.sh
- unwanted-dependencies.json
- update-all.sh
- update-codegen.sh
- update-conformance-yaml.sh
- update-generated-api-compatibility-data.sh
- update-generated-docs.sh
- update-generated-stable-metrics.sh
- update-go-workspace.sh
- update-gofmt.sh
- update-golangci-lint-config.sh
- update-import-aliases.sh
- update-internal-modules.sh
- update-kustomize.sh
- update-mocks.sh
- update-netparse-cve.sh
- update-openapi-spec.sh
- update-translations.sh
- update-vanity-imports.sh
- update-vendor-licenses.sh
- update-vendor.sh
- update-yamlfmt.sh
- verify-all.sh
- verify-api-groups.sh
- verify-boilerplate.sh
- verify-cli-conventions.sh
- verify-codegen.sh
- verify-conformance-requirements.sh
- verify-conformance-yaml.sh
- verify-description.sh
- verify-e2e-suites.sh
- verify-e2e-test-ownership.sh
- verify-external-dependencies-version.sh
- verify-fieldname-docs.sh
- verify-file-sizes.sh
- verify-flags-underscore.py
- verify-generated-docs.sh
- verify-generated-stable-metrics.sh
- verify-go-workspace.sh
- verify-gofmt.sh
- verify-golangci-lint-config.sh
- verify-golangci-lint-pr-hints.sh
- verify-golangci-lint-pr.sh
- verify-golangci-lint.sh
- verify-govulncheck.sh
- verify-import-aliases.sh
- verify-import-boss.sh
- verify-imports.sh
- verify-internal-modules.sh
- verify-licenses.sh
- verify-mocks.sh
- verify-netparse-cve.sh
- verify-no-vendor-cycles.sh
- verify-non-mutating-validation.sh
- verify-openapi-docs-urls.sh
- verify-openapi-spec.sh
- verify-pkg-names.sh
- verify-prerelease-lifecycle-tags.sh
- verify-prometheus-imports.sh
- verify-publishing-bot.py
- verify-readonly-packages.sh
- verify-shellcheck.sh
- verify-spelling.sh
- verify-staging-meta-files.sh
- verify-test-code.sh
- verify-test-featuregates.sh
- verify-test-images.sh
- verify-testing-import.sh
- verify-typecheck.sh
- verify-vendor-licenses.sh
- verify-vendor.sh
- verify-yamlfmt.sh
-
boilerplate
- logo
- pkg
- plugin
- staging
- test
- third_party
- vendor
- .generated_files
- .gitattributes
- .gitignore
- .go-version
- CHANGELOG.md
- CONTRIBUTING.md
- LICENSE
- Makefile
- OWNERS
- OWNERS_ALIASES
- README.md
- SECURITY_CONTACTS
- SUPPORT.md
- code-of-conduct.md
- go.mod
- go.sum
- go.work
- go.work.sum
Explanation
This code implements import-boss
, a tool that enforces import restrictions in a Go repository. It uses a .import-restrictions
file (YAML) in each package directory to define rules that govern imports within the repository.
Key components:
loadPkgs
function: Loads Go packages from the given patterns and checks for errors during parsing.massage
function: Renames test packages to include the original package name and handles test files, ensuring consistent package names.ImportBoss
struct:incomingImports
: Stores direct imports for each package.transitiveIncomingImports
: Stores the transitive closure of direct imports, indicating all packages that indirectly import a given package.newBoss
function: Creates anImportBoss
instance, populating the import maps.Verify
function: Iterates through loaded packages, verifies import restrictions against rules defined in.import-restrictions
files, and reports any violations.packageDir
function: Determines the directory of a package based on Go files or ignored files.FileFormat
struct: Represents a.import-restrictions
file, containingRules
andInverseRules
.Rule
struct: Defines an import restriction rule, including:SelectorRegexp
: Regex pattern to match import paths.AllowedPrefixes
: List of allowed prefixes for matched imports.ForbiddenPrefixes
: List of forbidden prefixes for matched imports.Transitive
: Boolean flag indicating whether the rule applies to transitive imports.Disposition
enum: Represents the allowed/forbidden status of a dependency.Evaluate
function: Evaluates a rule against a given import path to determine its allowed/forbidden status.recursiveRead
function: Recursively reads.import-restrictions
files from the current directory to the module root.readFile
function: Reads and unmarshals a.import-restrictions
file.isGoModRoot
function: Checks for the presence ofgo.mod
file to identify the module root.removeLastDir
function: Removes the last directory from a path.verifyRules
function: Checks direct and transitive imports of a package against defined rules.uniq
function: Removes duplicate strings from multiple slices.hasPathPrefix
function: Checks if a path has a given prefix.transitiveImports
function: Returns direct and transitive imports for a given package.dfsImports
function: Performs a depth-first search to collect transitive imports.verifyInverseRules
function: Checks that all packages that import a given package are allowed to do so based on the inverse rules defined in.import-restrictions
.transitiveClosure
function: Computes the transitive closure of a directed graph using Warshall’s algorithm.
Key features:
- Import Restriction Enforcement: The tool enforces import restrictions defined in
.import-restrictions
files. - Direct and Transitive Import Analysis: It checks both direct and transitive imports against rules.
- Inverse Rule Support: It verifies that all packages importing a specific package are allowed to do so.
- Recursive File Reading: It reads rules from
.import-restrictions
files recursively up to the module root. - Package Filtering and Handling: It filters out test packages and handles cases where a package may have both source files and test files.
- Error Reporting: It provides detailed error messages indicating violations of import restrictions.
Overall, import-boss
is a useful tool for maintaining consistent and controlled import practices within a Go repository. It helps enforce import restrictions, promotes modularity, and reduces potential dependencies on unexpected or forbidden packages.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph