Skip to main content

Trunk - The New Super linter

· 3 min read
Mohammed Yahya

The DevOps super linter and formater

What is Trunk? and How are we using it with Terraform and Docker

trunk

Trunk is a blazingly fast meta code checker and formatter with extraordinary features like caching, preexisting issue detection, a daemon, and a language server.

Install

curl https://get.trunk.io -fsSL | bash

Or with:

Visual Studio Code

- Init Must be in root git level

✖ Trunk can only init if it's run at the root of a git repo

- Init Must be with commited files existed

✖ No committed files in repo, unable to analyze files. Trunk init enables linters automatically based on the contents of your repo, so you must have something in your repo for it to work.

Initialize trunk in your repo

trunk init

Run 30+ linters on your repo

trunk check

Auto-format your repo

trunk fmt

Terraform

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}

# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
access_key = "my-access-key"
secret_key = "my-secret-key"
}

# Create a VPC
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}

Docker

# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

Demo

$ trunk init
Initializing

✔ Analyzing files in your repo... done (3 files in 0.01s)
✔ Detecting latest linter versions... done (3 linters in 0.00s)
✔ Creating config files... done (0.27s)


Enabled 3 Linters

• gitleaks
• hadolint (created .hadolint.yaml)
• terraform-fmt


✔ Enabled 3 linters (see .trunk/trunk.yaml)
32 additional linters can be enabled in .trunk/trunk.yaml
$ trunk check --all --no-fix
Checking 100% [====================================================================================>] 12/12 0.4s

AUTOFIXES

main.tf
1:1 high Incorrect formatting terraform-fmt

10 | # Configure the AWS Provider
11 | provider "aws" {
12 | region = "us-east-1"
| region = "us-east-1"
13 | access_key = "my-access-key"
14 | secret_key = "my-secret-key"
| secret_key = "my-secret-key"
15 | }
16 |

→ Apply autofix (Y/n): n
Autofix discarded.

ISSUES

Dockerfile:3:1
3:1 medium Pin versions in apk add. Instead of `apk add <package>` use `apk add <package>=<version>` hadolint/DL3018
6:1 low `yarn cache clean` missing after `yarn install` was run. hadolint/DL3060

main.tf:1:1
1:1 high Incorrect formatting, autoformat by running 'trunk fmt' terraform-fmt

3 blocking issues
$ trunk fmt
Checking 100% [======================================================================================>] 3/3 0.4s

✔ Auto-fixed main.tf

Re-checking autofixed files...

Checking 67% [==========================================================> ] 2/3 0.4s

✔ No issues