Skip to content

Commit

Permalink
Tpl sed heredoc (#22)
Browse files Browse the repository at this point in the history
* replace sed with heredoc

* add dry_run to log-vars
  • Loading branch information
basejump authored Sep 20, 2021
1 parent 97af011 commit 18a130b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bin/github
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function github.create_release {
local body=$(cat "$BUILD_DIR/CHANGELOG_RELEASE.md")
local body_esc=`escape_json_string "$body"`

## LEAVE THIS INDENT, her docs needs to to look this way
## LEAVE THIS INDENT, heredoc needs to to look this way
local api_data=$(cat <<EOF
{
"tag_name": "v$1",
Expand Down
28 changes: 28 additions & 0 deletions bin/heredoc_tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

###
# basic tempalate variable replacement using heredoc
# runs on a .tpl. file in form some_file.tpl.yml for example
# replaces variables with values
#
# $1 - the tpl.yml file
# $2 - the output dir to put the processed files with the .tpl. stripped from the name
#
# @stdout the processed tpl build file name
#
function heredoc_tpl {
mkdir -p "$2"
# parse just the file name
local tplFile=${1##*/}
# replace .tpl.yml with .yml or .tpl.any with .any is what `/.tpl./.` is doing
local processedTpl="$2/${tplFile/.tpl./.}"

## LEAVE THIS INDENT, heredoc needs to to look this way
eval "cat << EOF
$(<"$1")
EOF
" > "$processedTpl"

echo "$processedTpl"
}

6 changes: 3 additions & 3 deletions bin/kube_tools
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
set -e
source "$(dirname "${BASH_SOURCE[0]}")"/core/main
core.import "init_env"
core.import "sed_tpl"
core.import "heredoc_tpl"
core.import "dotenv"

# ---
Expand All @@ -14,8 +14,8 @@ core.import "dotenv"
# ---
function kube.process_tpl {
# set the variables in BUILD_ENV so we can build the sed replacement for templates
dotenv.load "build/make/makefile${MAKELEVEL}.env"
sed_tpl "$1" "build/kube"
# dotenv.load "build/make/makefile${MAKELEVEL}.env"
heredoc_tpl "$1" "build/kube"
}

###
Expand Down
2 changes: 1 addition & 1 deletion k8s/docmark-pages-deploy.tpl.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# kubernetes deployment for pages generated with docmark.
# container uses env variables to download git project, build the docs and then start a nginx webserver
# sets up ingress-nginx services and uses default tls cert that was setup for it.
# use the sed_tpl to process this template for example `apply_tpl build/bin/kube_templates/.tpl.yml`
# use the heredoc_tpl to process this template for example `apply_tpl build/bin/kube_templates/.tpl.yml`
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down
2 changes: 2 additions & 0 deletions makefiles/Shipkit-main.make
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export BUILD_DIR ?= build
# make a unique makefile using MAKELEVEL, which is incrmented for each make subprocess.
# so if make calls a make target it doesn't collide, they can be different based on whats passed for DBMS for example
export MAKE_ENV_FILE ?= $(BUILD_DIR)/make/makefile$(MAKELEVEL).env
# $(info MAKE_ENV_FILE: $(MAKE_ENV_FILE))

SHELL_VARS += VERBOSE_LOG BUILD_DIR MAKE_ENV_FILE DBMS env dry_run
#shell doesn't get the exported vars so we need to spin the ones we want, which should be in BUILD_VARS
Expand Down Expand Up @@ -67,6 +68,7 @@ FORCE:

## list the BUILD_VARS in the build/make env
log-vars: FORCE
printf "$(ccyan)dry_run$(creset) = $(dry_run)\n"
printf "$(culine)Variable:\n\n$(creset)"
for v in $(sort $(BUILD_VARS)); do
printf "$(ccyan)$$v $(creset)=$(cbold) $${!v:-} $(creset)\n"
Expand Down
6 changes: 2 additions & 4 deletions tests/sandbox/heredoc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# kubernetes deployment for pages generated with docmark.
# container uses env variables to download git project, build the docs and then start a nginx webserver
# sets up ingress-nginx services and uses default tls cert that was setup for it.
# use the sed_tpl to process this template for example `echo foo`
# Sample stuff

`tags=$(git tag -l --sort=-version:refname)
echo "${tags[@]}"
echo buzz
Expand Down

0 comments on commit 18a130b

Please sign in to comment.