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 all commits
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
* Added formatter for [Terraform files](https://www.terraform.io/docs/cli/commands/fmt.html) ([#959](https://github.com/diffplug/spotless/pull/959))

## [2.18.0] - 2021-09-30
### Added
Expand Down
3 changes: 2 additions & 1 deletion gradle/special-tests.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
def special = [
'Npm',
'Black',
'Clang'
'Clang',
'TerraformFmt'
]

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.spotless.terraform;

import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

import com.diffplug.spotless.ForeignExe;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.ProcessRunner;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class TerraformFmtStep {
public static String name() {
return "terraformfmt";
}

public static String defaultVersion() {
return "1.0.8";
}

private final String version;
private final @Nullable String pathToExe;

private TerraformFmtStep(String version, @Nullable String pathToExe) {
this.version = version;
this.pathToExe = pathToExe;
}

public static TerraformFmtStep withVersion(String version) {
return new TerraformFmtStep(version, null);
}

public TerraformFmtStep withPathToExe(String pathToExe) {
return new TerraformFmtStep(version, pathToExe);
}

public FormatterStep create() {
return FormatterStep.createLazy(name(), this::createState, State::toFunc);
}

private State createState() throws IOException, InterruptedException {
String exeAbsPath = ForeignExe.nameAndVersion("terraform", version)
.pathToExe(pathToExe)
.versionRegex(Pattern.compile("v(\\S*)"))
.fixCantFind("Tell Spotless where it is with {@code terraform().pathToExe('path/to/executable')}")
.fixWrongVersion(
"You can tell Spotless to use the version you already have with {@code terraformFmt('{versionFound}')}" +
"or you can download the currently specified version, {version}.\n")
.confirmVersionAndGetAbsolutePath();
return new State(this, exeAbsPath);
}

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
static class State implements Serializable {
private static final long serialVersionUID = -1825662356883926318L;
// used for up-to-date checks and caching
final String version;
// used for executing
final transient List<String> args;

State(TerraformFmtStep step, String exeAbsPath) {
this.version = step.version;
this.args = Arrays.asList(exeAbsPath, "fmt", "-");
}

String format(ProcessRunner runner, String input) throws IOException, InterruptedException {
return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8);
}

FormatterFunc.Closeable toFunc() {
ProcessRunner runner = new ProcessRunner();
return FormatterFunc.Closeable.of(runner, this::format);
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Added
* Added formatter for [Terraform files](https://www.terraform.io/docs/cli/commands/fmt.html) ([#959](https://github.com/diffplug/spotless/pull/959))

## [5.15.2] - 2021-09-27
### Changed
Expand Down
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
3 changes: 2 additions & 1 deletion plugin-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ if (version.endsWith('-SNAPSHOT')) {
'scalafmt',
'scalafix',
'black',
'clang-format'
'clang-format',
'terraform'
]
plugins {
spotlessPlugin {
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);
}
}
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
* Added formatter for [Terraform files](https://www.terraform.io/docs/cli/commands/fmt.html) ([#959](https://github.com/diffplug/spotless/pull/959))

## [2.15.0] - 2021-09-30
### Added
Expand Down
38 changes: 38 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ user@machine repo % mvn spotless:check
- [Sql](#sql) ([dbeaver](#dbeaver))
- [Maven Pom](#maven-pom) ([sortPom](#sortpom))
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
- [Terraform](#terraform)
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
- [eclipse web tools platform](#eclipse-web-tools-platform)
Expand Down Expand Up @@ -628,6 +629,43 @@ The auto-discovery of config files (up the file tree) will not work when using t

For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt.

## Terraform

[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/terraform/Terraform.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/terraform/TerraformFmt.java).

```xml
<configuration>
<terraform>
<!-- You have to set the target manually -->
<includes>
<include>src/**/*.tf</include>
<include>src/**/*.tfvars</include>
</includes>

<fmt /> <!-- has its own section below -->
</terraform>
</configuration>
```

### fmt

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

```xml
<fmt>
<version>1.0.5</version> <!-- version is optional -->
<!-- if the terraform cli is not on your path, you must specify its location manually -->
<pathToExe>/opt/homebrew/bin/terraform</pathToExe>
<!--
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
-->
</fmt>
```

<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
@@ -0,0 +1,40 @@
/*
* 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.spotless.maven.terraform;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;
import com.diffplug.spotless.terraform.TerraformFmtStep;

public class Fmt implements FormatterStepFactory {
@Parameter
private String version;

@Parameter
private String pathToExe;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
TerraformFmtStep terraformFmt = TerraformFmtStep.withVersion(version == null ? TerraformFmtStep.defaultVersion() : version);
if (pathToExe != null) {
terraformFmt = terraformFmt.withPathToExe(pathToExe);
}
return terraformFmt.create();
}
}
Loading