## 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 -n "$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
}

