jw-pkg: Add command rm-template-output

Also add lots of small fixes and beautifications all over the place.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2018-11-19 12:34:36 +00:00
commit 7ee054b9e8

View file

@ -1,5 +1,10 @@
#!/bin/bash
log()
{
echo "$myname: $@"
}
channel_present()
{
if smart channel --show $1 2>&1 | grep -q baseurl >/dev/null; then
@ -32,6 +37,7 @@ cat << EOT
list-templates: list templates
list-template-tables: list templates tables
list-template-output: list template output files
rm-template-output: remove template output files
compile-templates: compile templates
global options are
@ -211,6 +217,33 @@ cmd_list_template_output()
done
}
cmd_rm_template_output()
{
local restore=0
while [ "${1:0:1}" = '-' ]; do
case "$1" in
-r)
restore=1
;;
*)
usage 1
;;
esac
shift
done
local f
cmd_list_template_output $@ | while read f; do
log "removing $f"
rm $f
bak=$f$template_bak_ext
if [ "$restore" = 1 -a -f "$bak" ]; then
log "restoring $f from $bak"
mv $bak $f
fi
done
}
cmd_compile_templates()
{
local ext_from="$template_exts" # TODO: support more than one
@ -222,10 +255,11 @@ cmd_compile_templates()
local mode=600
local conf_patt="^[ ]*# *conf:"
local missing_table=0
local backup=0
umask 0077
while [ ${1:0:1} = '-' ]; do
while [ "${1:0:1}" = '-' ]; do
case "$1" in
-o)
owner="$2"
@ -239,6 +273,9 @@ cmd_compile_templates()
group="$2"
shift
;;
-b)
backup=1
;;
*)
usage 1
;;
@ -257,23 +294,28 @@ cmd_compile_templates()
[ -f "$table" ] && break
done
[ -f $table ] || {
echo "WARNING: No key-value table found for template $f, not compiling." >&2
log "WARNING: No key-value table found for template $f, not compiling." >&2
((missing_table++))
continue
}
# TODO: use mktemp -d and keep temporary files in read-only dir
local tmp=$to.tmp
echo "Applying macros in $table to $f."
log "Applying macros in $table to $f."
eval `sed "/$conf_patt/ !d; s/$conf_patt//" $table`
sed 's|^[ ]*#.*||; s|\([^ =]\+\)[ =]\+\(.*\)|s/\1/\2/g|' $table | sed -f - $f > $tmp
chmod $mode $tmp
chown $owner $tmp
chgrp $group $tmp
local bak=$to$template_bak_ext
if [ "$backup" = 1 -a -f $to ] && ! diff -q $to $bak >/dev/null 2>&1; then
log "Saving backup to $to to $bak"
cp -p $to $bak
fi
mv $tmp $to
done <<< $(cmd_list_templates "$@")
if [ "$missing_table" != 0 ]; then
echo "WARNING: $missing_table missing tables found. You might want to add them and run sudo $cmdline again."
log "WARNING: $missing_table missing tables found. You might want to add them and run sudo $cmdline again."
fi
}
@ -291,8 +333,9 @@ longname=$0
opts="v"
table_exts=".jw-secret .jw-vars"
template_exts=".jw-tmpl"
template_bak_ext=".jw-pkg.bak"
while [ ${1:0:1} = '-' ]; do
while [ "${1:0:1}" = '-' ]; do
case "$1" in
-v)
opt_verbose=true
@ -366,7 +409,7 @@ cpp_glib)
ftp://dspadm@ftp.jannet.de/pub/packages/linux/suse/10.1/inst-source/rpm/i586/glib2-2.8.5-19.i586.rpm
check_ldconfig "$@"
;;
compile_templates|list_templates|list_template_tables|list_template_output|diff|build_date|built_today|rpmnew)
compile_templates|list_templates|list_template_tables|list_template_output|rm_template_output|diff|build_date|built_today|rpmnew)
cmd_$cmd "$@"
;;
help)