Skip to content

Commit 081835b

Browse files
committed
feat(build): #232 lint terraform
- Add argument, module and docs
1 parent 99e9f77 commit 081835b

File tree

11 files changed

+255
-11
lines changed

11 files changed

+255
-11
lines changed

.github/workflows/dev.yml

+8
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ jobs:
113113
name: /lintPython/module/cliMain
114114
with:
115115
args: sh -c "nix-env -if . && m . /lintPython/module/cliMain"
116+
lintTerraform_module:
117+
runs-on: ubuntu-latest
118+
steps:
119+
- uses: actions/checkout@v2
120+
- uses: docker://docker.io/nixos/nix:2.3.12
121+
name: /lintTerraform/module
122+
with:
123+
args: sh -c "nix-env -if . && m . /lintTerraform/module"
116124
lintWithLizard:
117125
runs-on: ubuntu-latest
118126
steps:

README.md

+79-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ in just a few steps, in any technology.
3838
- [lintMarkdown](#lintmarkdown)
3939
- [lintNix](#lintnix)
4040
- [lintPython](#lintpython)
41+
- [lintTerraform](#lintterraform)
4142
- [lintWithLizard](#lintwithlizard)
4243
- [Pinning](#pinning)
4344
- [inputs](#inputs)
@@ -419,6 +420,14 @@ Attributes:
419420
- pubKey (`str`):
420421
Public key of the [Cachix][CACHIX] cache.
421422

423+
Required environment variables:
424+
425+
- `CACHIX_AUTH_TOKEN`: API token of the [Cachix][CACHIX] cache.
426+
- For Public caches:
427+
If not set the cache will only be read, but not written to.
428+
- For private caches:
429+
If not set the cache won't be read, nor written to.
430+
422431
Example `makes.nix`:
423432

424433
```nix
@@ -431,14 +440,6 @@ Example `makes.nix`:
431440
}
432441
```
433442

434-
Required environment variables:
435-
436-
- `CACHIX_AUTH_TOKEN`: API token of the [Cachix][CACHIX] cache.
437-
- For Public caches:
438-
If not set the cache will only be read, but not written to.
439-
- For private caches:
440-
If not set the cache won't be read, nor written to.
441-
442443
## Formatters
443444

444445
Formatters help your code be consistent, beautiful and more maintainable.
@@ -773,6 +774,70 @@ Example invocation: `$ m . /lintPython/dirOfModules/makes/main`
773774

774775
Example invocation: `$ m . /lintPython/module/cliMain`
775776

777+
### lintTerraform
778+
779+
Lint [Terraform][TERRAFORM] code
780+
with [TFLint][TFLINT].
781+
782+
Attributes:
783+
784+
- enable (`boolean`): Optional.
785+
Defaults to false.
786+
- config (`lines`): Optional.
787+
Defaults to:
788+
789+
```hcl
790+
config {
791+
module = true
792+
}
793+
794+
plugin "aws" {
795+
enabled = true
796+
}
797+
```
798+
799+
- modules (`attrsOf moduleType`): Optional.
800+
Path to [Terraform][TERRAFORM] modules to lint.
801+
Defaults to `{ }`.
802+
803+
Custom Types:
804+
805+
- moduleType (`submodule`):
806+
- src (`str`):
807+
Path to the [Terraform][TERRAFORM] module.
808+
- version (`str`):
809+
[Terraform][TERRAFORM] version your module is built with.
810+
811+
Required environment variables:
812+
813+
- If your [Terraform][TERRAFORM] module uses the AWS provider:
814+
- `AWS_ACCESS_KEY_ID`
815+
- `AWS_SECRET_ACCESS_KEY`
816+
- `AWS_DEFAULT_REGION`
817+
- `AWS_SESSION_TOKEN`: Required only if the AWS credentials are temporary.
818+
819+
Example `makes.nix`:
820+
821+
```nix
822+
{
823+
lintTerraform = {
824+
enable = true;
825+
modules = {
826+
module1 = {
827+
src = "/my/module1";
828+
version = "0.12";
829+
};
830+
module2 = {
831+
src = "/my/module2";
832+
version = "0.16";
833+
};
834+
};
835+
};
836+
}
837+
```
838+
839+
Example invocation: `$ m . /lintTerraform`
840+
776841
### lintWithLizard
777842

778843
Using [Lizard][LIZARD] to check
@@ -883,9 +948,9 @@ Custom Types:
883948

884949
Required environment variables:
885950

886-
- CI_REGISTRY_USER and CI_REGISTRY_PASSWORD, when deploying to GitLab.
887-
- DOCKER_HUB_USER and DOCKER_HUB_PASS, when deploying to Docker Hub.
888-
- GITHUB_ACTOR and GITHUB_TOKEN, when deploying to Github Container Registry.
951+
- `CI_REGISTRY_USER` and `CI_REGISTRY_PASSWORD`, when deploying to GitLab.
952+
- `DOCKER_HUB_USER` and `DOCKER_HUB_PASS`, when deploying to Docker Hub.
953+
- `GITHUB_ACTOR` and `GITHUB_TOKEN`, when deploying to Github Container Registry.
889954

890955
Example `makes.nix`:
891956

@@ -1598,6 +1663,9 @@ $ m . /example
15981663
- [TERRAFORM_FMT]: https://www.terraform.io/docs/cli/commands/fmt.html
15991664
[Terraform FMT][TERRAFORM_FMT]
16001665

1666+
- [TFLINT]: https://github.com/terraform-linters/tflint
1667+
[TFLint][TFLINT]
1668+
16011669
- [TRAVIS_CI]: https://travis-ci.org/
16021670
[Travis CI][TRAVIS_CI]
16031671

makes.nix

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@
8686
};
8787
};
8888
};
89+
lintTerraform = {
90+
enable = true;
91+
modules = {
92+
module = {
93+
src = "/test/lintTerraform/module";
94+
version = "0.13";
95+
};
96+
};
97+
};
8998
lintWithLizard = {
9099
enable = true;
91100
targets = [ "/" ];

src/args/default.nix

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ let
2121
formatTerraform = import ./format-terraform args;
2222
getAttr = import ./get-attr/default.nix;
2323
inherit inputs;
24+
lintTerraform = import ./lint-terraform/default.nix args;
2425
makeContainerImage = import ./make-container-image/default.nix args;
2526
makeDerivation = import ./make-derivation/default.nix args;
2627
makeDerivationParallel = import ./make-derivation-parallel/default.nix args;
@@ -29,13 +30,15 @@ let
2930
makeScript = import ./make-script/default.nix args;
3031
makeScriptParallel = import ./make-script-parallel/default.nix args;
3132
makeSearchPaths = import ./make-search-paths/default.nix args;
33+
makeTerraformEnvironment = import ./make-terraform-environment/default.nix args;
3234
inherit makesVersion;
3335
makeTemplate = import ./make-template/default.nix args;
3436
inherit outputs;
3537
path = path: head + path;
3638
pathImpure = path: headImpure + path;
3739
sortAscii = builtins.sort (a: b: a < b);
3840
sortAsciiCaseless = builtins.sort (a: b: lib.toLower a < lib.toLower b);
41+
toDerivationName = lib.strings.sanitizeDerivationName;
3942
toFileJson = import ./to-file-json/default.nix args;
4043
toFileLst = import ./to-file-lst/default.nix;
4144
};

src/args/lint-terraform/default.nix

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{ __nixpkgs__
2+
, makeScript
3+
, makeTerraformEnvironment
4+
, ...
5+
}:
6+
{ config
7+
, name
8+
, version
9+
, src
10+
, ...
11+
}:
12+
makeScript {
13+
entrypoint = ./entrypoint.sh;
14+
replace = {
15+
__argConfig__ = config;
16+
__argSrc__ = src;
17+
};
18+
name = "lint-terraform-for-${name}";
19+
searchPaths = {
20+
bin = [
21+
__nixpkgs__.tflint
22+
];
23+
source = [
24+
(makeTerraformEnvironment {
25+
inherit version;
26+
})
27+
];
28+
};
29+
}

src/args/lint-terraform/entrypoint.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# shellcheck shell=bash
2+
3+
function main {
4+
cd "$(mktemp -d)" \
5+
&& copy '__argSrc__' . \
6+
&& info Initializing '__argSrc__' \
7+
&& terraform init \
8+
&& info Linting '__argSrc__' \
9+
&& tflint -c '__argConfig__' .
10+
}
11+
12+
main "${@}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# shellcheck shell=bash
2+
3+
function main {
4+
local pip=(python -m pip --cache-dir .)
5+
6+
info Creating virtualenv \
7+
&& python -m venv "${out}" \
8+
&& info Activating virtualenv \
9+
&& source "${out}/bin/activate" \
10+
&& info Installing \
11+
&& HOME=. "${pip[@]}" install --requirement "${envRequirementsFile}" \
12+
&& info Freezing \
13+
&& HOME=. "${pip[@]}" freeze | sort --ignore-case > "${out}/installed" \
14+
&& sed -E 's|^(.*)\[.*?\](.*)$|\1\2|g' "${envRequirementsFile}" > "${out}/desired" \
15+
&& if test "$(cat "${out}/desired")" = "$(cat "${out}/installed")"; then
16+
info Integrity check passed
17+
else
18+
info Integrity check failed \
19+
&& info You need to specify all dependencies: \
20+
&& git --no-pager diff --no-index "${out}/desired" "${out}/installed" \
21+
&& error Stopping due to failed integrity check
22+
fi \
23+
&& rm -f "${out}/desired" \
24+
&& rm -f "${out}/installed"
25+
}
26+
27+
main "${@}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{ __nixpkgs__
2+
, makeSearchPaths
3+
, ...
4+
}:
5+
{ version
6+
}:
7+
let
8+
terraform = {
9+
"0.12" = __nixpkgs__.terraform_0_12;
10+
"0.13" = __nixpkgs__.terraform_0_13;
11+
"0.14" = __nixpkgs__.terraform_0_14;
12+
"0.15" = __nixpkgs__.terraform_0_15;
13+
"0.16" = __nixpkgs__.terraform_0_16;
14+
}.${version};
15+
in
16+
makeSearchPaths {
17+
bin = [ terraform ];
18+
}

src/evaluator/modules/outputs/builtins/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ args:
1414
(import ./lint-markdown/default.nix args)
1515
(import ./lint-nix/default.nix args)
1616
(import ./lint-python/default.nix args)
17+
(import ./lint-terraform/default.nix args)
1718
(import ./lint-with-lizard/default.nix args)
1819
];
1920
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{ __nixpkgs__
2+
, lintTerraform
3+
, path
4+
, ...
5+
}:
6+
{ config
7+
, lib
8+
, ...
9+
}:
10+
let
11+
makeModule = name: { src, version }: {
12+
name = "/lintTerraform/${name}";
13+
value = lintTerraform {
14+
config = builtins.toFile "tflint.hcl" config.lintTerraform.config;
15+
inherit name;
16+
src = path src;
17+
inherit version;
18+
};
19+
};
20+
in
21+
{
22+
options = {
23+
lintTerraform = {
24+
enable = lib.mkOption {
25+
default = false;
26+
type = lib.types.bool;
27+
};
28+
config = lib.mkOption {
29+
default = ''
30+
config {
31+
module = true
32+
}
33+
plugin "aws" {
34+
enabled = true
35+
}
36+
'';
37+
type = lib.types.lines;
38+
};
39+
modules = lib.mkOption {
40+
default = { };
41+
type = lib.types.attrsOf (lib.types.submodule (_: {
42+
options = {
43+
src = lib.mkOption {
44+
type = lib.types.str;
45+
};
46+
version = lib.mkOption {
47+
type = lib.types.enum [
48+
"0.12"
49+
"0.13"
50+
"0.14"
51+
"0.15"
52+
"0.16"
53+
];
54+
};
55+
};
56+
}));
57+
};
58+
};
59+
};
60+
config = {
61+
outputs = lib.mkIf config.lintTerraform.enable
62+
(builtins.foldl'
63+
(all: one: all // { "${one.name}" = one.value; })
64+
{ }
65+
(lib.attrsets.mapAttrsToList
66+
makeModule
67+
config.lintTerraform.modules));
68+
};
69+
}

test/lintTerraform/module/main.tf

Whitespace-only changes.

0 commit comments

Comments
 (0)