lib/update.utils
author Tero Marttila <terom@paivola.fi>
Fri, 23 Mar 2012 15:10:23 +0200
changeset 69 88a7683efc54
parent 66 482d06935d96
child 82 26a307558602
permissions -rw-r--r--
update: list_files to copy all DHCP_CONFS..
#!/bin/bash
# vim: set ft=sh :
#
# 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"
}

### Command execution
## 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.
# Also logs the executed command at log_cmd level..
function indent () {
    local indent=$1; shift

    log_cmd "$@"

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

    return ${PIPESTATUS[0]}
}


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

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

## Output absolute path from $ROOT:
#
#   abspath $path
#
function abspath () {
    local path=$1

    echo "$ROOT/$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
        # strip prefix
        name=${file#$dir/}
        name=${name%$glob}

        echo -n "$name "
    done
}

### HG wrappers
# Run `hg ...` within $REPO.
function hg {
    local repo=$REPO

    cmd $HG -R $ROOT/$repo "${HG_ARGS[@]}" "$@"
}

# Does the repo have local modifications?
function hg_modified {
    hg id | grep -q '+'
}

# Output possible -u flag for commit.
function hg_user {
    if [ ${SUDO_USER:-} ]; then
        echo '-u' "$SUDO_USER"

    elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
        debug "using .hgrc user"
        echo ''

    else
        echo '-u' "$USER"
    fi
}

# Show changes in repo
function hg_diff {
    # just stat hidden files, but show the rest
    hg diff --stat -I "$REPO/$REPO_HIDE"
    hg diff -X "$REPO/$REPO_HIDE"
}

## Commit changes in repo, with given message:
#
#   hg_commit   $msg
#
function hg_commit {
    local msg=$1
    local user_opt=$(hg_user)
    
    debug "$user_opt: $msg"
    hg commit $user_opt -m "$msg"
}