Skip to content

Add Terraform formatter step #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extend Gradle plugin to support Terraform fmt
  • Loading branch information
carhartl committed Oct 7, 2021
commit 1c3186bd267946fa0d17ab1976a3c4a7b96f95bd
31 changes: 31 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui
- [SQL](#sql) ([dbeaver](#dbeaver), [prettier](#prettier))
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
- [JSON](#json)
- [Terraform](#terraform)
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
- javascript, jsx, angular, vue, flow, typescript, css, less, scss, html, json, graphql, markdown, ymaml
Expand Down Expand Up @@ -569,6 +570,36 @@ spotless {
}
```

## Terraform

- `com.diffplug.gradle.spotless.TerraformExtension` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/5.15.2/com/diffplug/gradle/spotless/TerraformExtension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/TerraformExtension.java)

```gradle
spotless {
terraform {
target 'src/**/*.tf', 'src/**/*.tfvars' // you have to set the target manually

fmt() // has its own section below
}
}
```

### fmt

[homepage](https://www.terraform.io/docs/cli/commands/fmt.html). [changelog](https://github.com/hashicorp/terraform/blob/main/CHANGELOG.md).

```gradle
fmt('1.0.5') // version is optional

// if the terraform cli is not on your path, you must specify its location manually
fmt().pathToExe('/opt/homebrew/bin/terraform')
// Spotless always checks the version of the terraform cli it is using
// and will fail with an error if it does not match the expected version
// (whether manually specified or default). If there is a problem, Spotless
// will suggest commands to help install the correct version.
// TODO: handle installation & packaging automatically - https://github.com/diffplug/spotless/issues/674
```

<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>

## Prettier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public void json(Action<JsonExtension> closure) {
format(JsonExtension.NAME, JsonExtension.class, closure);
}

/** Configures the special terraform-specific extension for terraform files. */
public void terraform(Action<TerraformExtension> closure) {
format(TerraformExtension.NAME, TerraformExtension.class, closure);
}

/** Configures a custom extension. */
public void format(String name, Action<FormatExtension> closure) {
requireNonNull(name, "name");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;

import javax.inject.Inject;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.terraform.TerraformFmtStep;

public class TerraformExtension extends FormatExtension {
static final String NAME = "terraform";

@Inject
public TerraformExtension(SpotlessExtension spotless) {
super(spotless);
}

public TerraformFmtConfig fmt() {
return fmt(TerraformFmtStep.defaultVersion());
}

public TerraformFmtConfig fmt(String version) {
return new TerraformFmtConfig(version);
}

public class TerraformFmtConfig {
TerraformFmtStep stepCfg;

TerraformFmtConfig(String version) {
this.stepCfg = TerraformFmtStep.withVersion(version);
addStep(createStep());
}

public TerraformFmtConfig pathToExe(String pathToTerraformCli) {
stepCfg = stepCfg.withPathToExe(pathToTerraformCli);
replaceStep(createStep());
return this;
}

private FormatterStep createStep() {
return stepCfg.create();
}
}

@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
throw noDefaultTargetException();
}
super.setupTask(task);
}
}