/arch-go

Architecture checks for Go projects

Primary LanguageGoMIT LicenseMIT

Arch-Go

Architecture checks for Go projects

Supported rules

Dependencies Checks

Supports defining import rules

  • Allowed dependencies (same module)
  • Not allowed dependencies (same module)
  • Allowed external dependencies (different module and not part of standard library)

Package Content Checks

Allows you to define the contents of a set of packages, e.g. you can define that a desired package should only contain interfaces definitions. The supported checks are:

  • shouldNotContainInterfaces
  • shouldNotContainStructs
  • shouldNotContainFunctions
  • shouldNotContainMethods
  • shouldOnlyContainInterfaces
  • shouldOnlyContainStructs
  • shouldOnlyContainFunctions
  • shouldOnlyContainMethods

Cyclic Dependencies checks

Checks if a set of packages contains cyclic dependencies.

Function checks

Checks some functions properties, like the following:

  • Maximum number of parameters
  • Maximum number of return values
  • Maximum number of public functions per file
  • Maximum number of lines in the function body

Naming rules checks

Checks some naming rules, like the following:

  • If a struct implements an interface that match some name pattern, then it's name should starts or ends with a specific pattern. For example, all structs that implements 'Verificator' interface, should have a name that ends with 'Verificator'

Configuration

File arch-go.yml

dependenciesRules:
  - package: "**.impl.*"
    shouldOnlyDependsOn:
      - "**.foo.*"
      - "*.bar.*"
    shouldNotDependsOn: ["**.model.**"]
  - package: "**.utils.**"
    shouldOnlyDependsOn:
      - "**.model.**"

contentsRules:
  - package: "**.impl.model"
    shouldNotContainInterfaces: true
  - package: "**.impl.config"
    shouldOnlyContainFunctions: true
  - package: "**.impl.dependencies"
    shouldNotContainStructs: true
    shouldNotContainInterfaces: true
    shouldNotContainMethods: true
    shouldNotContainFunctions: true

functionsRules:
  - package: "**.impl.**"
    maxParameters: 3
    maxReturnValues: 2
    maxPublicFunctionPerFile: 1
    maxLines: 50

cyclesRules:
  - package: "**.cmd"
    shouldNotContainCycles: true

namingRules:
  - package: "**.arch-go.**"
    interfaceImplementationNamingRule:
      structsThatImplement: "*Connection"
      shouldHaveSimpleNameEndingWith: "Connection"

Package name patterns

The package name can be defined as a fixed value or using * special character, to create a simple pattern.

Example Description
*.name Package should end with name and anything before, supporting multiple levels (for example either foo/name and foo/bar/name)
**.name Package should end with name and anything before, supporting only one level (for example foo/name, but no foo/bar/name)
name.* Package should start with name and anything before, supporting multiple levels (for example either name/foo and name/foo/bar)
name.** Package should start with name and anything before, supporting only one level (for example name/foo, but no name/foo/bar)
**.name.** Package should contain name, supporting multiple levels before and after (for example both foo/name/x/y/z, foo/bar/name and foo/bar/name/x)
**.foo/bar.** Package should contain foo/bar, supporting multiple levels before and after (for example both x/y/foo/bar/w/z, foo/bar/name and x/y/foo/bar)
foo.**.bar Package should start with foo, and ends with bar, and can have anything between them. (for example foo/bar, foo/test/blah/bar and foo/ok/bar)
foo.*.bar Package should start with foo, and ends with bar, and can have only one level between them. (for example foo/bar and foo/ok/bar, but no foo/test/blah/bar)

Usage

To install Arch-Go, run

$ go get -u github.com/fdaines/arch-go

To execute this tool you have to be in the module path

$ cd [path-to-your-module]

Now you can execute Arch-Go tool

$ arch-go [flags]

Describing your architecture guidelines

Arch-Go includes a command to describe the architecture rules from arch-go.yml file.

$ arch-go describe

The output of the describe command is similar to:

$ arch-go describe
Dependency Rules
        * Packages that match pattern '**.cmd.*',
                * Should only depends on packages that matches:
                        - '**.arch-go.**'

Function Rules
        * Packages that match pattern '**.arch-go.**' should comply with the following rules:
                * Functions should not have more than 50 lines
                * Functions should not have more than 4 parameters
                * Functions should not have more than 2 return values
                * Files should not have more than 5 public functions

Content Rules
        * Packages that match pattern '**.impl.model' should not contain functions or methods
        * Packages that match pattern '**.impl.config' should only contain functions

Naming Rules
        * Packages that match pattern '**.arch-go.**' should comply with:
                * Structs that implement interfaces matching name '*Verification' should have simple name ending with 'Verification'

Supported flags

Flag Description
--verbose Includes detailed information while the command is running. The shorthand is -v
--html Generates a simple HTL report with the evaluation result.

Examples

$ arch-go 
$ arch-go -v
$ arch-go --verbose
$ arch-go --html
$ arch-go describe

Contributions

Feel free to contribute.