Skip to content
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

chore: new module script #255

Merged
merged 2 commits into from
Aug 22, 2024
Merged
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
102 changes: 102 additions & 0 deletions hacks/create-new-module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

# Get module name
if [ $# -ne 1 ]; then
echo "Usage: $0 <module-name>"
exit 1
fi

MODULE_NAME="$1"

# Validate module name
if ! [[ $MODULE_NAME =~ ^[a-zA-Z0-9-]+$ ]]; then
echo "Error: Module name can only contain lower case alphanumeric characters and dashes."
exit 1
fi

# Get the git repo root
REPO_ROOT=$(git rev-parse --show-toplevel)
if [ $? -ne 0 ]; then
echo "Error: Not in a git repository."
exit 1
fi

# Check if the module directory already exists
MODULE_PATH="$REPO_ROOT/modules/$MODULE_NAME"
if [ -d "$MODULE_PATH" ]; then
echo "Error: Module directory already exists: $MODULE_PATH"
exit 1
fi

# Create the directory
mkdir -p "$MODULE_PATH"
if [ $? -ne 0 ]; then
echo "Error: Failed to create directory $MODULE_PATH"
exit 1
fi

# Create versions.tf
cat <<EOF >"$MODULE_PATH/versions.tf"
terraform {
required_version = ">= 1.0.0"
}

# TODO: add your providers
EOF
if [ $? -ne 0 ]; then
echo "Error: Failed to create file versions.tf"
exit 1
fi

# Create .tflint.hcl
cat <<EOF >"$MODULE_PATH/.tflint.hcl"
plugin "terraform" {
enabled = true
preset = "recommended"
}

# TODO: setup tflint rulesets for your providers
# for example: https://github.com/terraform-linters/tflint-ruleset-aws
EOF
if [ $? -ne 0 ]; then
echo "Error: Failed to create file .tflint.hcl"
exit 1
fi

# Update release-please-config.json
CONFIG_FILE="$REPO_ROOT/release-please-config.json"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: release-please-config.json not found at $CONFIG_FILE"
exit 1
fi

# Use jq to add the new module to the config file
jq --arg module "$MODULE_NAME" \
'.packages += {
"modules/\($module)": {
"component": $module,
"release-type": "terraform-module",
"changelog-path": "CHANGELOG.md",
"bump-minor-pre-major": false,
"bump-patch-for-minor-pre-major": false,
"draft": false,
"prerelease": false
}
}' \
"$CONFIG_FILE" >"$CONFIG_FILE.tmp" &&
mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
if [ $? -ne 0 ]; then
echo "Error: Failed to update release-please-config.json"
exit 1
fi

echo "Module $MODULE_NAME created, you should create a new branch with these changes and open a pull request"
echo "\
From the repo root:

git switch -c new-module/$MODULE_NAME
git add modules/$MODULE_NAME
git add release-please-config.json
git commit -m \"feat: add new module $MODULE_NAME\"
git push -u origin new-module/$MODULE_NAME
"