Skip to content

Instantly share code, notes, and snippets.

@ueokande
Last active March 21, 2019 00:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ueokande/0b37a673b63781e65ed3 to your computer and use it in GitHub Desktop.
Save ueokande/0b37a673b63781e65ed3 to your computer and use it in GitHub Desktop.
Bachef - Chef like bash DSL
abort() {
>&2 echo $@
exit 1
}
declare_resources() {
for r in $@; do
eval ${r}'() { echo "['${r}']=${1:?}"; }'
done
}
_cp() { echo " - cp $@"; }
change_mode() { echo " - chmod $@"; }
change_owner() { echo " - chown $@"; }
change_group() { echo " - chgrp $@"; }
remote_file() {
target="$1"
shift
declare -A params='('"$@"')'
required_params="source"
optional_params="mode owner group"
echo "remote_file[${target}]"
for p in $required_params; do
test -z ${params[$p]} && abort "no ${p} parameter"
done
_cp "${params[source]}" "${target}"
for p in $optional_params; do
val=${params[${p}]}
test -z $val && continue
change_${p} "$val" "$target"
done
}
declare_resources source mode owner group
#!/bin/bash
source 'bachef.sh'
remote_file '/etc/nginx/nginx.conf' $(
source 'templates/nginx.conf'
mode '755'
)
remote_file '/etc/ssh/sshd_config' $(
source 'templates/nginx.conf'
mode '700'
owner 'root'
group 'root'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment