Sign inSign up

fe3dback/go-arch-lint

By fe3dback

•Updated 24 days ago

Golang architecture linter

Image
0

10K+

fe3dback/go-arch-lint repository overview

Logo image

Check all project imports and compare to arch rules defined in yml file

Go Report Card Docker Cloud Build Status

⁠Quick start

⁠Install globally and run
go get -u github.com/fe3dback/go-arch-lint

go will download and install binary to bin folder, usually is ~/go/bin

Run binary with flag "check --project-path" and absolutely path to your project, for example:

go-arch-lint check --project-path /home/user/go/src/github.com/fe3dback/galaxy
⁠alternative - Run with docker
docker run --rm \
    -v /home/user/go/src/github.com/fe3dback/galaxy:/app \
    fe3dback/go-arch-lint:latest-stable-release check --project-path /app

docker hub⁠

⁠Usage

Usage:
  go-arch-lint check [flags]

Flags:
      --arch-file string      arch file path (default ".go-arch-lint.yml")
  -h, --help                  help for check
      --max-warnings int      max number of warnings to output (default 512)
      --project-path string   absolute path to project directory (where '.go-arch-lint.yml' is located) (default "./")

Global Flags:
      --json                   (alias for --output-type=json)
      --output-color           use ANSI colors in terminal output (default true)
      --output-json-one-line   format JSON as single line payload (without line breaks), only for json output type
      --output-type string     type of command output, variants: [ascii, json] (default "default")

⁠Archfile example

Make archfile called '.go-arch-lint.yml' in root directory of your project, and put some arch rules to it

version: 1
allow:
  depOnAnyVendor: false

# ----------------------------------
# Exclude from analyse
# ----------------------------------
exclude:
  - .idea
  - vendor

excludeFiles:
  - "^.*_test\\.go$"
  - "^.*test/mock/.*\\.go$"

# ----------------------------------
# Vendor libs
# ----------------------------------
vendors:
  loaderYaml:
    in: gopkg.in/yaml.v2
  vectors:
    in: github.com/fe3dback/go-vec

# ----------------------------------
# Project components
#
# Used for split real modules and 
# packages to abstract namespaces
# ----------------------------------
components:
  main:
    in: .
  engine:
    in: engine
  engineVendorEvents:
    in: engine/vendor/*/event
  game:
    in: game
  gameComponent:
    in: game/components/**
  utils:
    in: utils

# ----------------------------------
# All components can import any 
# packages from "common" list
# ----------------------------------
commonComponents:
  - utils

# ----------------------------------
# All components can import any 
# vendors from "common" list
# ----------------------------------
commonVendors:
  - vectors

# ----------------------------------
# Dependency rules
# ----------------------------------
deps:
  engine:
    canUse:
      - loaderYaml  

  engineVendorEvents:
    mayDependOn:
      - engine

  game:
    mayDependOn:
      - engine
      - gameComponent

  main:
    mayDependOn:
      - game
      - engine

This project also uses arch lint, see example in .go-arch-lint.yml⁠

⁠Archfile Syntax

PathReq?TypeDescription
version+intschema version
allow-mapglobal rules
. depOnAnyVendor-boolallow import any vendor code to any project file
exclude-listlist of directories (relative path) for exclude from analyse
excludeFiles-listregExp rules for file names, for exclude from analyse
components+mapproject components used for split real modules and packages to abstract thing
. %name%+strname of component
. . in+strrelative directory name, support glob masking (src/*/engine/**)
vendors-mapvendor libs
. %name%+strname of vendor component
. . in+strfull import path
commonComponents-listlist of components, allow import them into any code
commonVendors-listlist of vendors, allow import them into any code
deps+mapdependency rules
. %name%+strname of component, exactly as defined in "components" section
. . anyVendorDeps-boolall component code can import any vendor code
. . anyProjectDeps-boolall component code can import any other project code, useful for DI/main component
. . mayDependOn-listlist of components that can by imported in %name%
. . canUse-listlist of vendors that can by imported in %name%

⁠Example of usage

This linter will return:

Status CodeDescription
0Project corresponds for defined rules
1Found warnings
$ go-arch-lint check --project-path ~/go/src/github.com/fe3dback/galaxy
Module: github.com/fe3dback/galaxy
[WARN] Component 'game_entities_factory': file '/game/entities/factory/bullet.go' shouldn't depend on 'github.com/fe3dback/galaxy/game/entities/components/game'
[WARN] Component 'game_loader': file '/game/loader/weaponloader/loader.go' shouldn't depend on 'github.com/fe3dback/galaxy/engine'
[WARN] File '/shared/ui/layer_shared_fps.go' not attached to any component in archfile

warnings found: 3
⁠json

Same warnings in json format

$ go-arch-lint check --project-path ~/go/src/github.com/fe3dback/galaxy --json
{
  "Type": "models.Check",
  "Payload": {
    "ExecutionWarnings": [],
    "ArchHasWarnings": true,
    "ArchWarningsDeps": [
      {
        "ComponentName": "game_entities_factory",
        "FileRelativePath": "/game/entities/factory/bullet.go",
        "FileAbsolutePath": "/home/neo/go/src/github.com/fe3dback/galaxy/game/entities/factory/bullet.go",
        "ResolvedImportName": "github.com/fe3dback/galaxy/game/entities/components/game"
      },
      {
        "ComponentName": "game_loader",
        "FileRelativePath": "/game/loader/weaponloader/loader.go",
        "FileAbsolutePath": "/home/neo/go/src/github.com/fe3dback/galaxy/game/loader/weaponloader/loader.go",
        "ResolvedImportName": "github.com/fe3dback/galaxy/engine"
      }
    ],
    "ArchWarningsNotMatched": [
      {
        "FileRelativePath": "/shared/ui/layer_shared_fps.go",
        "FileAbsolutePath": "/home/neo/go/src/github.com/fe3dback/galaxy/shared/ui/layer_shared_fps.go"
      }
    ],
    "ModuleName": "github.com/fe3dback/galaxy"
  }
}

Read more in docs⁠:

Tag summary

Content type

Image

Digest

sha256:9f4fb1216…

Size

314.2 MB

Last updated

24 days ago

docker pull fe3dback/go-arch-lint