lib/update.utils
author Tero Marttila <terom@paivola.fi>
Tue, 17 Dec 2013 00:04:00 +0200
branchdns-new
changeset 82 26a307558602
parent 69 88a7683efc54
child 83 5a83f2abc0dd
permissions -rw-r--r--
update update
#!/bin/bash
#
# Utility functions

### Command execution
## Execute command, possibly logging its execution.
#
#   cmd     $cmd...
#
# Fails if the command returns an error exit code.
function cmd {
    log_cmd "$@"

    "$@" || die "Failed"
}

## Execute command as a test, logging its execution at log_cmd
#
#   cmd_test $cmd... && ... || ...
#
# Fails if the command returns an error exit code.
function cmd_test {
    log_cmd "$@"

    "$@"
}
## Execute command, prefixing its output on stdout with given indent prefix.
#
#   indent  "    " $cmd...
#
# Output is kept on stdout, exit status is that of the given command.
function indent () {
    local indent=$1; shift

    "$@" | sed "s/^/$indent/"

    return ${PIPESTATUS[0]}
}


### FS utils
# Create dir if not exists.
function ensure_dir {
    local dir=$1

    if [ ! -d $dir ]; then
        log_warn "Creating output dir: $dir"
        cmd mkdir $dir
    fi
}

## Output absolute path
#
#   abspath $path
#
# XXX: improve...?
function abspath () {
    local path=$1

    echo "$SRV/$path"
}

## List names of files in dir:
#
#   list_files $dir $glob
#
function list_files {
    local dir=$1
    local glob=${2:-*}
    local name=

    for file in $dir/$glob; do
        # only files
        [ -f $file ] || continue

        # strip prefix
        name=${file#$dir/}
        name=${name%$glob}

        echo -n "$name "
    done
}

## List names of files in dirs..
function list_dirs {
    for dir in "$@"; do
        for file in $dir/*; do
            echo -n "${file#$dir/}"
        done
    done
}