Skip to content

Commit

Permalink
Add template initialization script (patch)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukiffer committed Dec 23, 2022
1 parent 142ce94 commit 517ed49
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .init-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

# This script initializes a repository deployed from this template.
# Skip prompts by setting environment variables SHORT_DESCRIPTION and/or LONG_DESCRIPTION
# If LONG_DESCRIPTION is not set then it will default to the value in SHORT_DESCRIPTION

set -e

SED_COMMAND="sed -i"
if [[ $OSTYPE == "darwin"* ]]; then
# macOS doesn't use GNU sed and has a slightly different syntax
SED_COMMAND="sed -i '' -E"
fi

function update_package_json() {
local -r github_org="$1"
local -r repository_name="$2"
local -r description="$3"

eval "$SED_COMMAND 's|cncsc/template-stacks|$github_org/$repository_name|g' package.json"
eval "$SED_COMMAND 's/{{module_description}}/$description/g' package.json"
rm package-lock.json
npm install
}

function update_github_env_file() {
local -r github_org="$1"
eval "$SED_COMMAND 's/cncsc/$github_org/g ./github/env.yaml"
}

function update_remote_state_config() {
local tfc_org
echo 'Enter the name of the Terraform Cloud org (e.g. "cncsc-dev"):'
read -r tfc_org
eval "$SED_COMMAND 's/cncsc/$tfc_org/g' .remote-state-config.yaml"
}

function main() {
local -r repository_name=$(git remote -v | grep push | sed -e 's|.*/||' | sed -e 's/\.git.*//')
local description

echo "Initializing repository from template..."
echo "Using repository name as the module name ($repository_name)..."

until test -n "${description:=${SHORT_DESCRIPTION}}"; do
echo "Enter a short description for the repository (package.json):"
echo ' (e.g. "Monorepo for declaratively managing the infrastructure and configuration of <group>.")'
read -r description
done

remote_origin="$(git config --get remote.origin.url || true)"
github_org="$(dirname "$remote_origin")"

update_package_json "$github_org" "$repository_name" "$description"
update_github_env_file "$github_org"
update_remote_state_config

rm -rf .init-template.sh

echo ""
echo "Initialization complete. Committing to source control..."
git add -A
git commit -m "Initialize repository from template"
git push -u origin main
}

main "$@"
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"name": "@cncsc/template-stacks",
"version": "0.0.0-development",
"description": "{{module_description}}",
"main": "terragrunt.hcl",
"repository": {
"type": "git",
"url": "[email protected]:cncsc/template-stacks.git"
Expand Down

0 comments on commit 517ed49

Please sign in to comment.