Wednesday, April 24, 2024
HomeGolangA CLI Software for CI/CD pipelines to stop Golang spaghetti code

A CLI Software for CI/CD pipelines to stop Golang spaghetti code


A Golang-based command line device for CI/CD pipelines (and dev machines) that helps to stop Go spaghetti code (a.ok.a. large ball of mud).

Fortunately, within the Go world, the compiler already prevents round dependencies between packages. So this device has to care solely about further undesired dependencies.

I gave a chat that included the motivation for this device and a few (outdated) utilization examples: 

Moreover, this device paperwork the construction of a venture in its configuration.

Utilization of the Golang CLI Software

You possibly can merely name it with go run github.com/flowdev/spaghetti-cutter from anyplace inside your venture.

The attainable command line choices are:

Utilization of spaghetti-cutter:
  -e    do not report errors and do not exit with an error (shorthand)
  -noerror
        do not report errors and do not exit with an error
  -r string
        root listing of the venture (shorthand) (default ".")
  -root string
        root listing of the venture (default ".")

If no --root possibility is given the foundation listing is discovered by crawling up the listing tree beginning on the present working listing. The primary listing that incorporates the configuration file .spaghetti-cutter.hjson will probably be taken as venture root.

The output seems to be like:

2020/09/10 09:37:08 INFO - configuration 'allowOnlyIn': `github.com/hjson/**`: `x/config` ; `golang.org/x/instruments**`: `parse*`, `x/pkgs*`
2020/09/10 09:37:08 INFO - configuration 'allowAdditionally': `*_test`: `parse`
2020/09/10 09:37:08 INFO - configuration 'god': `most important`
2020/09/10 09:37:08 INFO - configuration 'device': `x/*`
2020/09/10 09:37:08 INFO - configuration 'db': ...
2020/09/10 09:37:08 INFO - configuration 'dimension': 1024
2020/09/10 09:37:08 INFO - configuration 'noGod': false
2020/09/10 09:37:08 INFO - root bundle: github.com/flowdev/spaghetti-cutter
2020/09/10 09:37:08 INFO - Measurement of bundle 'x/config': 699
2020/09/10 09:37:08 INFO - Measurement of bundle 'x/pkgs': 134
2020/09/10 09:37:08 INFO - Measurement of bundle 'deps': 401
2020/09/10 09:37:08 INFO - Measurement of bundle 'parse': 109
2020/09/10 09:37:08 INFO - Measurement of bundle 'dimension': 838
2020/09/10 09:37:08 INFO - Measurement of bundle 'x/dirs': 86
2020/09/10 09:37:08 INFO - Measurement of bundle '/': 202
2020/09/10 09:37:08 INFO - No errors discovered.

First, the configuration values and the foundation bundle are reported. So you’ll be able to simply be certain that the right configuration file is taken.

All bundle sizes are reported and final however not least, any violations are discovered. Since no error was discovered, the return code is 0.

A typical error message can be:

2020/09/10 10:31:14 ERROR - area bundle 'pkg/purchasing' is not allowed to import bundle 'pkg/cart'

The return code is first From the output, you’ll be able to see that

  • the bundle pkg/purchasing is acknowledged as a normal area bundle,
  • it imports the pkg/cart bundle and
  • there isn’t a allowAdditionally configuration to permit this.

You possibly can repair that by including a little bit of configuration.

Different non-zero return codes are attainable for technical issues (unparsable code: 6, …). If appropriately used within the construct pipeline, a non-zero return code will cease the construct, and the issue needs to be mounted first. So undesired imports (spaghetti relationships) are prevented.

Customary Use Case: Golang Internet API

In response to my very own unscientific analysis, this device was created with Internet APIs in thoughts as that’s what about 95% of all Gophers do.

So it presents particular dealing with for the next circumstances:

  • Instruments: Software packages can be utilized in every single place besides in different device packages. However they aren’t allowed to import another inside packages.
  • Database: DB packages can be utilized in different and normal (enterprise) packages. In fact they’ll use device packages however nothing else. Area information buildings may be both DB or device packages.
  • Database sub-packages: Sub-packages of DB packages are allowed to solely import device packages like DB packages. Moreover, they aren’t allowed for use anyplace else within the venture. So it’s best to use specific configuration with explanations as feedback (what the sub-packages include and why they exist in any respect).
  • Software sub-packages: Sub-packages of device packages aren’t allowed to import another inside bundle like device packages. Moreover, they aren’t allowed for use anyplace else within the venture. So it’s best to use specific configuration with explanations as feedback (what the sub-packages include and why they exist).
  • God: A god bundle can see and use all the pieces. You need to use this with nice care. most important is the one default god bundle used if no specific configuration is given. You need to solely hardly ever add extra. You possibly can swap most important to a normal bundle with the noGod configuration key. This is smart when you have received a number of most important packages with totally different dependencies.

These circumstances needn’t be used and may be overridden with specific configuration.

Configuration

It’s obligatory to make use of a HJSON configuration file .spaghetti-cutter.hjson within the root listing of your venture. This serves a number of functions:

  • It helps the spaghetti-cutter to search out the foundation listing of your venture.
  • It saves you from retyping command line choices many times.
  • It’s invaluable documentation, particularly for builders new to the venture.

The configuration can have the next parts:

  • devicedb and god for device, database and god packages as mentioned above.
  • allowOnlyIn: for limiting a bundle for use solely in some packages (permit “key” bundle solely in “worth” packages).
  • allowAdditionally: for permitting further dependencies (for “key” bundle permit further “worth” packages).
  • dimension: the utmost allowed dimension/complexity of a bundle. Default is 2048.
  • noGod: most important It received’t be god bundle.

The dimensions configuration key prevents a intelligent developer from simply throwing the entire spaghetti code right into a single bundle. With the spaghetti-cutter such issues will grow to be apparent and you may put them as technical dept into your again log.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments