lib/pvl/apply/cmd.sh
author Tero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 21:38:09 +0200
changeset 627 a81206440be2
child 628 b10ad946d01d
permissions -rw-r--r--
(none)
## Generate updated output file from given command's stdout:
#
#   do_update $out $BIN/cmd --args
#
# Writes output to a temporary .new file, optionally shows a diff of changes, and commits
# the new version to $out (unless noop'd).
function cmd_apply {
    local out="$1"
    local tmp="$out.new"

    debug "$out"
    cmd "${@:1}" > "$tmp"

    # compare
    if [ -e "$out" -a -z "$APPLY_DIFF" ]; then
        debug "  changes:"

        # terse
        indent "        " diff --unified=1 "$out" "$tmp" || true
    fi
    
    # deploy
    if [ "$APPLY" = 0 ]; then
        # cleanup
        debug "  no-op"

        cmd rm "$tmp"
    else
        # commit
        debug "  deploy"

        cmd mv "$tmp" "$out"
    fi
}