Skip to content

Commit eb38bc3

Browse files
blinkagent[bot]blink-so[bot]DevelopmentCats
authored
ci: add variable naming lint to terraform validate (#766)
## Summary Terraform variable names should use underscores (`snake_case`), not hyphens. Hyphens are technically valid in HCL but are [deprecated and non-idiomatic](https://developer.hashicorp.com/terraform/language/values/variables). This PR adds a variable name check into the existing `terraform_validate.sh` script so it runs as part of the existing "Run Terraform Validate" CI step — no new scripts or workflow changes needed. ## Changes ### `scripts/terraform_validate.sh` — added `validate_variable_names()` - Scans `.tf` files in changed modules for `variable` declarations with hyphens - Fails with actionable fix suggestions (shows the snake_case alternative) - Runs after `terraform validate` in the same CI step ### Fix: `code-server` module — rename `machine-settings` → `machine_settings` - Renames the hyphenated variable and its reference in main.tf - Bumps version `1.4.2` → `1.4.3` - Updates all README examples --- Created on behalf of @matifali --------- Co-authored-by: blink-so[bot] <211532188+blink-so[bot]@users.noreply.github.com> Co-authored-by: DevCats <christofer@coder.com>
1 parent 93e6094 commit eb38bc3

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

registry/coder/modules/code-server/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Automatically install [code-server](https://github.com/coder/code-server) in a w
1414
module "code-server" {
1515
count = data.coder_workspace.me.start_count
1616
source = "registry.coder.com/coder/code-server/coder"
17-
version = "1.4.2"
17+
version = "1.4.3"
1818
agent_id = coder_agent.example.id
1919
}
2020
```
@@ -29,7 +29,7 @@ module "code-server" {
2929
module "code-server" {
3030
count = data.coder_workspace.me.start_count
3131
source = "registry.coder.com/coder/code-server/coder"
32-
version = "1.4.2"
32+
version = "1.4.3"
3333
agent_id = coder_agent.example.id
3434
install_version = "4.106.3"
3535
}
@@ -43,7 +43,7 @@ Install the Dracula theme from [OpenVSX](https://open-vsx.org/):
4343
module "code-server" {
4444
count = data.coder_workspace.me.start_count
4545
source = "registry.coder.com/coder/code-server/coder"
46-
version = "1.4.2"
46+
version = "1.4.3"
4747
agent_id = coder_agent.example.id
4848
extensions = [
4949
"dracula-theme.theme-dracula"
@@ -61,7 +61,7 @@ Configure VS Code's [settings.json](https://code.visualstudio.com/docs/getstarte
6161
module "code-server" {
6262
count = data.coder_workspace.me.start_count
6363
source = "registry.coder.com/coder/code-server/coder"
64-
version = "1.4.2"
64+
version = "1.4.3"
6565
agent_id = coder_agent.example.id
6666
extensions = ["dracula-theme.theme-dracula"]
6767
settings = {
@@ -78,7 +78,7 @@ Just run code-server in the background, don't fetch it from GitHub:
7878
module "code-server" {
7979
count = data.coder_workspace.me.start_count
8080
source = "registry.coder.com/coder/code-server/coder"
81-
version = "1.4.2"
81+
version = "1.4.3"
8282
agent_id = coder_agent.example.id
8383
extensions = ["dracula-theme.theme-dracula", "ms-azuretools.vscode-docker"]
8484
}
@@ -92,7 +92,7 @@ You can pass additional command-line arguments to code-server using the `additio
9292
module "code-server" {
9393
count = data.coder_workspace.me.start_count
9494
source = "registry.coder.com/coder/code-server/coder"
95-
version = "1.4.2"
95+
version = "1.4.3"
9696
agent_id = coder_agent.example.id
9797
additional_args = "--disable-workspace-trust"
9898
}
@@ -108,7 +108,7 @@ Run an existing copy of code-server if found, otherwise download from GitHub:
108108
module "code-server" {
109109
count = data.coder_workspace.me.start_count
110110
source = "registry.coder.com/coder/code-server/coder"
111-
version = "1.4.2"
111+
version = "1.4.3"
112112
agent_id = coder_agent.example.id
113113
use_cached = true
114114
extensions = ["dracula-theme.theme-dracula", "ms-azuretools.vscode-docker"]
@@ -121,7 +121,7 @@ Just run code-server in the background, don't fetch it from GitHub:
121121
module "code-server" {
122122
count = data.coder_workspace.me.start_count
123123
source = "registry.coder.com/coder/code-server/coder"
124-
version = "1.4.2"
124+
version = "1.4.3"
125125
agent_id = coder_agent.example.id
126126
offline = true
127127
}

registry/coder/modules/code-server/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ variable "settings" {
4444
default = {}
4545
}
4646

47-
variable "machine-settings" {
47+
variable "machine_settings" {
4848
type = any
4949
description = "A map of template level machine settings to apply to code-server. This will be overwritten at each container start."
5050
default = {}
@@ -167,7 +167,7 @@ resource "coder_script" "code-server" {
167167
INSTALL_PREFIX : var.install_prefix,
168168
// This is necessary otherwise the quotes are stripped!
169169
SETTINGS : replace(jsonencode(var.settings), "\"", "\\\""),
170-
MACHINE_SETTINGS : replace(jsonencode(var.machine-settings), "\"", "\\\""),
170+
MACHINE_SETTINGS : replace(jsonencode(var.machine_settings), "\"", "\\\""),
171171
OFFLINE : var.offline,
172172
USE_CACHED : var.use_cached,
173173
USE_CACHED_EXTENSIONS : var.use_cached_extensions,

scripts/terraform_validate.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@ set -euo pipefail
1111
#
1212
# This script only validates changed modules. Documentation and template changes are ignored.
1313

14+
# Validates that Terraform variable names use underscores (snake_case) instead
15+
# of hyphens. Hyphens are technically valid but deprecated and non-idiomatic.
16+
# See: https://developer.hashicorp.com/terraform/language/values/variables
17+
validate_variable_names() {
18+
local dir="$1"
19+
local found_issues=0
20+
21+
while IFS= read -r tf_file; do
22+
while IFS= read -r match; do
23+
local line_num
24+
line_num=$(echo "$match" | cut -d: -f1)
25+
local line_content
26+
line_content=$(echo "$match" | cut -d: -f2-)
27+
local var_name
28+
var_name=$(echo "$line_content" | sed -n 's/.*variable "\([^"]*\)".*/\1/p')
29+
30+
if [[ -n "$var_name" ]]; then
31+
echo " ERROR: $tf_file:$line_num"
32+
echo " Variable \"$var_name\" contains a hyphen."
33+
echo " Rename to \"${var_name//-/_}\" (use underscores instead of hyphens)."
34+
found_issues=$((found_issues + 1))
35+
fi
36+
done < <(grep -n 'variable "[^"]*-[^"]*"' "$tf_file" 2> /dev/null || true)
37+
done < <(find "$dir" -name '*.tf' -type f | sort)
38+
39+
return "$found_issues"
40+
}
41+
1442
validate_terraform_directory() {
1543
local dir="$1"
1644
echo "Running \`terraform validate\` in $dir"
@@ -91,6 +119,16 @@ main() {
91119
fi
92120
done
93121

122+
echo ""
123+
echo "==> Validating Terraform variable names use snake_case..."
124+
for dir in $subdirs; do
125+
if test -f "$dir/main.tf"; then
126+
if ! validate_variable_names "$dir"; then
127+
status=1
128+
fi
129+
fi
130+
done
131+
94132
exit $status
95133
}
96134

0 commit comments

Comments
 (0)