This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New pattern to update module (#5)
Co-authored-by: Morgante Pell <[email protected]>
- Loading branch information
1 parent
a5a0e85
commit 0fe322d
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: Edit module | ||
--- | ||
|
||
Update a module by specifying its old `source` and the new one. | ||
|
||
It will update the `source` attribute of the modules and remove all variables that | ||
are not found on another module `path`. | ||
|
||
```grit | ||
engine marzano(0.1) | ||
language hcl | ||
pattern fix_module($old_source, $new_source, $allow_variables) { | ||
`module $_ { | ||
$args | ||
}` where { | ||
// Make sure we're looking at a module with $old_source | ||
$args <: contains `source = $old_source` => `source = $new_source`, | ||
// Check all attributes | ||
$args <: maybe contains bubble($allow_variables) `$key = $value` as $attr where { | ||
$key <: or { | ||
// Remap some keys | ||
`identifier` => `db_identifier`, | ||
// Keep source | ||
`source`, | ||
// Keep meta-arguments | ||
`count`, | ||
`depends_on`, | ||
`for_each`, | ||
`lifecycle`, | ||
`provider`, | ||
$_ where { | ||
$allow_variables <: some bubble($key) $candidate where { $candidate <: $key } | ||
}, | ||
// Finally, delete others we don't recognize | ||
$_ where { $attr => .} | ||
} | ||
// Remove others | ||
// or { | ||
// `version = $_`, | ||
// `storage_encrypted = $_`, | ||
// `apply_immediately = $_` | ||
// } => . | ||
} until attribute() | ||
} | ||
} | ||
pattern collect_variables($var_names) { | ||
`variable $name { | ||
$_ | ||
}` where { | ||
$name <: string_lit($content), | ||
if ($var_names <: undefined) { | ||
$var_names = [] | ||
}, | ||
$var_names += $content, | ||
} | ||
} | ||
pattern edit_module($old_source, $new_source, $module_path) { | ||
$var_names = [], | ||
some bubble($var_names) file($name, $body) where { | ||
$name <: r"\./variables/.*", // Path to get variables from | ||
$body <: contains collect_variables($var_names), | ||
}, | ||
some bubble($old_source, $new_source, $var_names) file($name, $body) where { | ||
$body <: contains fix_module($old_source, $new_source, allow_variables=$var_names) | ||
} | ||
} | ||
files(files = edit_module(old_source=`"old_source"`, new_source=`"new_source"`, module_path="")) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module "test_module1" { | ||
source = "new_source" | ||
variable1 = "variable1" | ||
variable2 = "variable2" | ||
|
||
|
||
} | ||
|
||
module "test_module2" { | ||
source = "new_source" | ||
variable1 = "variable1" | ||
variable2 = "variable2" | ||
|
||
|
||
} | ||
|
||
module "test_module3" { | ||
source = "another_source" | ||
variable1 = "variable1" | ||
variable2 = "variable2" | ||
variable3 = "variable3" | ||
variable4 = "variable4" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module "test_module1" { | ||
source = "old_source" | ||
variable1 = "variable1" | ||
variable2 = "variable2" | ||
variable3 = "variable3" | ||
variable4 = "variable4" | ||
} | ||
|
||
module "test_module2" { | ||
source = "old_source" | ||
variable1 = "variable1" | ||
variable2 = "variable2" | ||
variable3 = "variable3" | ||
variable4 = "variable4" | ||
} | ||
|
||
module "test_module3" { | ||
source = "another_source" | ||
variable1 = "variable1" | ||
variable2 = "variable2" | ||
variable3 = "variable3" | ||
variable4 = "variable4" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
|
||
# test.sh should be run from the current directory: hcl/.grit/patterns/test_edit_module | ||
|
||
set -e | ||
cp input/main.tf input/main.tf.bak # 1. Backup input.tf | ||
grit apply ../edit_module.md --force --suppress-output # 2. Apply pattern | ||
diff input/main.tf expected/main.tf # 3. Check that the modified input.tf equals expect.tf | ||
mv input/main.tf.bak input/main.tf # 4. Restore input.tf from the backup | ||
echo "Test successful" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "variable1" {} | ||
variable "variable2" { | ||
description = "description" | ||
} |
4 changes: 4 additions & 0 deletions
4
.grit/patterns/test_edit_module/variables_not_to_use/variables.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "variable3" {} | ||
variable "variable4" { | ||
description = "description" | ||
} |