lib/pvl/apply/cmd.sh
author Tero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 22:53:26 +0200
changeset 629 7214fe5c6fac
parent 628 b10ad946d01d
child 671 72143af5afbd
permissions -rw-r--r--
lib/pvl: fixfix
## 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 apply_cmd {
    local out="$1"
    local tmp="$out.new"

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

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

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

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

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