Architecture checks for Go projects

Related tags

Command Line arch-go
Overview

Arch-Go

Architecture checks for Go projects

Supported rules

Dependencies Checks

Supports defining import rules

  • Allowed dependencies
  • Not allowed dependencies

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

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

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 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]

Supported flags

Flag Description
--verbose Includes detailed information while the command is running. The shorthand is -v

Examples

$ arch-go 
$ arch-go -v
$ arch-go --verbose

Contributions

Feel free to contribute.

You might also like...
Powerful CLI written in GO to generate projects in various technologies
Powerful CLI written in GO to generate projects in various technologies

Barca CLI is a project generator written in GO and its purpose is to build and configure HTTP servers, web proxy, SPA/PWA, Blog and custom landing page. It's easy, fast and productive.

A tool to automate the setup and running of projects

Project CLI This project is still a work in progress but the mvp is working Are you tired of forgetting which commands to run or are you annoyed by th

Softsuite - Start from gofiber boilerplate and plan to build large projects

Softsuite Thanks to Cozy (ItsCosmas) to build gofiber boilerplate. I start learn

Godzillacli - Create, Run Godzilla Projects Using Godzilla CLI

godzillacli Create, Run Godzilla Projects Using Godzilla CLI About: godzillacli

Go linter that checks types that are json encoded - reports unsupported types and unnecessary error checks

Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omited.

Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture
Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture

go-clean-arch Changelog v1: checkout to the v1 branch Proposed on 2017, archived to v1 branch on 2018 Desc: Initial proposal by me. The story can be r

Golang Clean Architecture based on Uncle Bob's Clean Architecture and Summer internship in 2021

clean-architecture-api Description This is an example of implemention of Clean Architecture in Golang projects. This project has 4 layer : Infrastruct

Hexagonal architecture paradigms, such as dividing adapters into primary (driver) and secondary (driven)Hexagonal architecture paradigms, such as dividing adapters into primary (driver) and secondary (driven)

authorizer Architecture In this project, I tried to apply hexagonal architecture paradigms, such as dividing adapters into primary (driver) and second

This is a POC (Proof of Concept) using Hexagonal Architecture, SOLID, DDD, Clean Code, Clean Architecture
This is a POC (Proof of Concept) using Hexagonal Architecture, SOLID, DDD, Clean Code, Clean Architecture

Projeto Planetas Star Wars: Esse projeto de trata de uma POC utilizando os conceitos de Clean Arch, Hexagonal Arch, Clean Code, DDD, e SOLID. O princi

Clean architecture validator for go, like a The Dependency Rule and interaction between packages in your Go projects.
Clean architecture validator for go, like a The Dependency Rule and interaction between packages in your Go projects.

Clean Architecture checker for Golang go-cleanarch was created to keep Clean Architecture rules, like a The Dependency Rule and interaction between mo

estruct traverses javascript projects and maps all the dependencies and relationships to a JSON. the output can be used to build network visualizations of the project and document the architecture.
estruct traverses javascript projects and maps all the dependencies and relationships to a JSON. the output can be used to build network visualizations of the project and document the architecture.

EStruct traverses javascript projects and maps all the dependencies and relationships to a JSON. The output can be used to build network visualizations of the project and document the architecture.

Clean Architecture in Go (Golang) projects-DDD
Clean Architecture in Go (Golang) projects-DDD

go-clean-arch Changelog v1: checkout to the v1 branch Proposed on 2017, archived to v1 branch on 2018 Desc: Initial proposal by me. The story can be r

Clones github projects into ~/Projects/github/{org}/{repo}

Tidy clone Github cli extension (gh extension) to clone repos into ~/Projects/github/{org}/{repo} on the local filesystem Install gh extension install

Library for enabling asynchronous health checks in your service

go-health A library that enables async dependency health checking for services running on an orchestrated container platform such as kubernetes or mes

A library built to provide support for defining service health for golang services. It allows you to register async health checks for your dependencies and the service itself, provides a health endpoint that exposes their status, and health metrics.
apicompat checks recent changes to a Go project for backwards incompatible changes

Introduction apicompat is a tool to check for the introduction of backwards incompatible changes. apicompat: Guarantees that all consumers of a librar

errcheck checks that you checked errors.

errcheck errcheck is a program for checking for unchecked errors in go programs. Install go get -u github.com/kisielk/errcheck errcheck requires Go 1

Distributed, lock-free, self-hosted health checks and status pages
Distributed, lock-free, self-hosted health checks and status pages

Checkup is distributed, lock-free, self-hosted health checks and status pages, written in Go. It features an elegant, minimalistic CLI and an idiomati

bodyclose is a static analysis tool which checks whether res.Body is correctly closed.

bodyclose is a static analysis tool which checks whether res.Body is correctly closed. Install You can get bodyclose by go get command. $ go

Comments
  • I would like to enforce the absence of an external dependency

    I would like to enforce the absence of an external dependency

    My problem I have a single repo with multiple micro-services. I'm ok that my services share some library code, but I do not want one micro-service go and import code from another service. This is an implicit rule in the team that has been broken and gotten undetected for a while now.

    Suggested solution I would like to have a rule that detects whenever module A refers to module B. As I understand it is possible to either

    • package a should not refer to package b : dependenciesRules.shouldNotDependsOn
    • module A should only refer to external module B : dependenciesRules.shouldOnlyDependsOnExternal

    Which does not seem to be able to address my concern. Seems like it'd be easy enough to add that to the source code, unless there are some difficulties I haven't thought of.

    Alternative solutions I can't think of any so far

    opened by martinsson 6
  • panic on unexpected rule

    panic on unexpected rule

    Hello,

    arch-go is a very nice initiative and I would like to use it :)

    I tried the below rule on my code and arch-go panicked:

    dependenciesRules:
      - package: "**.my.package"
        shouldNotDependsOn:
          - "*.(?!my-site).*$.*"
    

    output was:

    panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x90 pc=0x1110c0b]
    
    goroutine 1 [running]:
    regexp.(*Regexp).doExecute(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0001884c1, 0x37, 0x0, 0x0, ...)
            /usr/local/Cellar/go/1.16.3/libexec/src/regexp/exec.go:527 +0x56b
    regexp.(*Regexp).doMatch(...)
            /usr/local/Cellar/go/1.16.3/libexec/src/regexp/exec.go:514
    regexp.(*Regexp).MatchString(...)
            /usr/local/Cellar/go/1.16.3/libexec/src/regexp/regexp.go:525
    github.com/fdaines/arch-go/internal/impl/dependencies.(*DependencyRuleVerification).checkComplianceWithRestrictedInternalImports(0xc0005c0190, 0xc0001884c1, 0x37, 0x0, 0xc0004a0b01, 0xc00049ef01)
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/internal/impl/dependencies/dependency_verification.go:93 +0x3e7
    github.com/fdaines/arch-go/internal/impl/dependencies.(*DependencyRuleVerification).Verify(0xc0005c0190, 0xc00013dca0)
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/internal/impl/dependencies/dependency_verification.go:56 +0x2b6
    github.com/fdaines/arch-go/internal/impl.CheckArchitecture.func1()
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/internal/impl/rule_runner.go:40 +0x18d
    github.com/fdaines/arch-go/internal/utils.ExecuteWithTimer(0xc00013dd70)
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/internal/utils/timer_functions.go:12 +0x50
    github.com/fdaines/arch-go/internal/impl.CheckArchitecture(0x0)
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/internal/impl/rule_runner.go:25 +0x4e
    github.com/fdaines/arch-go/internal/cmd.runCommand(0x158c980, 0x15bf000, 0x0, 0x0)
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/internal/cmd/root.go:35 +0x25
    github.com/spf13/cobra.(*Command).execute(0x158c980, 0xc000012210, 0x0, 0x0, 0x158c980, 0xc000012210)
            /Users/yoavsadeh/go/pkg/mod/github.com/spf13/[email protected]/command.go:860 +0x2c2
    github.com/spf13/cobra.(*Command).ExecuteC(0x158c980, 0x10013b0, 0x0, 0x0)
            /Users/yoavsadeh/go/pkg/mod/github.com/spf13/[email protected]/command.go:974 +0x375
    github.com/spf13/cobra.(*Command).Execute(...)
            /Users/yoavsadeh/go/pkg/mod/github.com/spf13/[email protected]/command.go:902
    github.com/fdaines/arch-go/internal/cmd.Execute()
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/internal/cmd/root.go:24 +0x2d
    main.main()
            /Users/yoavsadeh/go/pkg/mod/github.com/fdaines/[email protected]/main.go:8 +0x25
    

    I would expect arch-go to print a meaningful error (in this case that go could not compile the regex I tried to feed it) and gracefully shutdown.

    bug 
    opened by yoavsv 3
  • arch-go seem to ignore a rule

    arch-go seem to ignore a rule

    Hello,

    I'm using the following configuration:

    dependenciesRules:
      - package: ".package1.subackage1.subackage2"
        shouldNotDependsOn:
          - "**.hashicorp.**"
    

    in github.com/my-company/internal/package1/subackage1/subackage2 there's a file which imports github.com/hashicorp/go-multierror but the package passes the check.

    Is it a bug?

    opened by yoavsv 2
  • How to set up

    How to set up "shouldOnlyDependsOn any golang standard library"

    Hi, great library! The only way I find is list all the dependencies, even the standard ones, example:

    dependenciesRules:
      - package: "*.domain"
        shouldOnlyDependsOn:
          - "domain"
          - "errors" # why I need to mention the standard ones?
          - "time"   # why I need to mention the standard ones?
    

    Thanks!

    opened by mucardoso 3
Releases(v0.8.6)
Owner
Francisco Daines
Francisco Daines
Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. 🔍 📊

askgit is a command-line tool for running SQL queries on git repositories. It's meant for ad-hoc querying of git repositories on disk through a common interface (SQL), as an alternative to patching together various shell commands.

AskGit 3.2k Sep 28, 2022
Generate High Level Cloud Architecture diagrams using YAML syntax.

A commandline tool that generate High Level microservice & serverless Architecture diagrams using a declarative syntax defined in a YAML file.

Luca Sepe 544 Sep 6, 2022
Onion-Layer-Golang - A web application boilerplate built with go and clean architecture

go-clean-architecture-web-application-boilerplate A web application boilerplate

Endy Gigih Pratama 1 May 4, 2022
archy is an static binary to determine current kernel and machine architecture, with backwards compatible flags to uname, and offers alternative output format of Go runtime (i.e. GOOS, GOARCH).

archy archy is an simple binary to determine current kernel and machine architecture, which wraps uname and alternatively can read from Go runtime std

xargs-dev 3 Mar 18, 2022
General purpose reloader for all projects.

leaf General purpose reloader for all projects. Command leaf watches for changes in the working directory and runs the specified set of commands whene

Vaibhav 117 Jun 5, 2022
Handy commands to run in Go projects

Handy commands to run in Go projects

Nikolay Dubina 1.3k Sep 26, 2022
Contextual information about your git projects, right on the command-line

gitty gitty is a smart little CLI helper for git projects, that shows you all the relevant issues, pull requests and changes at a quick glance. It cur

Christian Muehlhaeuser 393 Sep 16, 2022
Libraries and CLIs for my personal all-in-one productivity system including components like bookmarks, notes, todos, projects, etc.

bntp.go Libraries and CLIs for my personal all-in-one productivity system including components like bookmarks, notes, todos, projects, etc. Neovim int

Jonas Mühlmann 13 Sep 13, 2022
depstat is a dependency analyzer for Go modules enabled projects.

depstat is a dependency analyzer for Go modules enabled projects. It runs as part of the Kubernetes CI pipeline to help evaluate dependency updates to Kubernetes.

Kubernetes SIGs 29 Aug 15, 2022
CLI for Setting up projects

Lemonade CLI for setting up projects Installation Install lemonade into your local machine. Prerequisites: Go v1.16+ Download Go from golang.org Clone

d-exclaimation 4 Jan 8, 2022