Skip to content

Instantly share code, notes, and snippets.

@kougazhang
Created November 10, 2021 02:39
Show Gist options
  • Save kougazhang/981aae37acfc7d2413b109400e2520d7 to your computer and use it in GitHub Desktop.
Save kougazhang/981aae37acfc7d2413b109400e2520d7 to your computer and use it in GitHub Desktop.
#shell #render #template
#!/usr/bin/env bash
function exitIfEmpty() {
if [ -z "$2" ]; then
echo "$1 is empty"
exit 1
fi
}
function initJob() {
local jobName=$1
local config=script/fctl/$jobName.sh
local dest=job/$jobName
local templates=script/fctl/templates
mkdir -p $dest
cp -r $templates/* $dest
source $config
local files=($(find $dest -type f))
for file in "${files[@]}"; do
# render
for key in "${!confItems[@]}"; do
local value=${confItems[$key]}
gsed -i "s/{{$key}}/$value/g" $file
if [ $key == "job" ]; then
local capital=$(echo $value | gsed 's/\b\(.\)/\u\1/g')
gsed -i "s/{{job.capital}}/$capital/g" $file
fi
done
# rename file
local dirName=$(dirname $file)
local fileName=$(basename $file)
local firstName=$(echo $fileName | awk -F . '{print $1}')
local secondName=$(echo $fileName | awk -F . '{print $2}')
case $firstName in
'-job')
mv $file $dirName/$jobName.$secondName
;;
esac
done
}
exitIfEmpty "action" $1
case $1 in
init)
exitIfEmpty "jobName" $2
initJob $2
;;
esac
declare -A confItems
confItems=(
["host"]="192.168.33.16"
["job"]="yy"
["jobNum"]="07"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment