Skip to content

Commit

Permalink
Apply terraform format changes when version 12 in use
Browse files Browse the repository at this point in the history
  • Loading branch information
kmanning committed Sep 13, 2020
1 parent 8fdcb28 commit ec7a105
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/TerraformFormatCommand.groovy
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class TerraformFormatCommand {
private static globalPlugins = []
private appliedPlugins = []
private static boolean check = false
private static boolean recursive = false
private static boolean diff = false
Expand All @@ -8,6 +10,8 @@ class TerraformFormatCommand {
private Closure diffOptionPattern

public String toString() {
applyPluginsOnce()

def pattern
def parts = []
parts << 'terraform fmt'
Expand All @@ -25,6 +29,19 @@ class TerraformFormatCommand {
return parts.join(' ')
}

public static addPlugin(TerraformFormatCommandPlugin plugin) {
this.globalPlugins << plugin
}

private applyPluginsOnce() {
def remainingPlugins = globalPlugins - appliedPlugins

for (TerraformValidateCommandPlugin plugin in remainingPlugins) {
plugin.apply(this)
appliedPlugins << plugin
}
}

public static withCheck(newValue = true) {
check = newValue
return this
Expand Down
7 changes: 7 additions & 0 deletions src/TerraformPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* before strating on your own.
*/
class TerraformPlugin implements TerraformValidateCommandPlugin,
TerraformFormatCommandPlugin,
TerraformPlanCommandPlugin,
TerraformApplyCommandPlugin,
TerraformValidateStagePlugin {
Expand All @@ -22,6 +23,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin,
def plugin = new TerraformPlugin()

TerraformValidateCommand.addPlugin(plugin)
TerraformFormatCommand.addPlugin(plugin)
TerraformPlanCommand.addPlugin(plugin)
TerraformApplyCommand.addPlugin(plugin)
TerraformValidateStage.addPlugin(plugin)
Expand Down Expand Up @@ -100,6 +102,11 @@ class TerraformPlugin implements TerraformValidateCommandPlugin,
applyToCommand(command)
}

@Override
void apply(TerraformFormatCommand command) {
applyToCommand(command)
}

@Override
void apply(TerraformPlanCommand command) {
applyToCommand(command)
Expand Down
15 changes: 15 additions & 0 deletions test/TerraformPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ class TerraformPluginTest {
}
}

class ApplyTerraformFormatCommand {
@Test
void shouldApplyTheCorrectStrategyToTerraformFormatCommand() {
def formatCommand = mock(TerraformFormatCommand.class)
def strategy = mock(TerraformPluginVersion.class)
def plugin = spy(new TerraformPlugin())
doReturn('someVersion').when(plugin).detectVersion()
doReturn(strategy).when(plugin).strategyFor('someVersion')

plugin.apply(formatCommand)

verify(strategy, times(1)).apply(formatCommand)
}
}

class ApplyTerraformPlanCommand {
@Test
void shouldApplyTheCorrectStrategyToTerraformPlanCommand() {
Expand Down

0 comments on commit ec7a105

Please sign in to comment.