common.shell 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#!/bin/false /bin/bash
##
# Common functions used by the Shell code.
#

set -eo pipefail


##
# Is shell active for this action?
#
# @param $1     the action we are attempting to perform
function shell_active() {
    local action="$1"

    local files=''
    if [[ "$action" == 'test' ]] ; then
        return 1
    elif [[ "$action" == 'lint' ]] ; then
        files="$(config -e lint_shell_files '{shell}')"
    elif [[ "$action" == 'coverage' ]] ; then
        return 1
    elif [[ "$action" == 'docs' ]] ; then
        return 1
    elif [[ "$action" == 'clean' ]] ; then
        return 1
    else
        # Unrecognised type
        return 1
    fi

    files="$(trim "$files")"

    if [[ -n "$files" ]] ; then
        return 0
    fi

    return 1
}


##
# Matching functions for filename expansion.
function shell_match_magic() {
    echo -n 'Shell script'
}
function shell_match_extensions() {
    echo -n 'sh'
}


function shell_setup_lint() {
    # Do we have shelllcheck installed
    if ! shellcheck -V > /dev/null 2>&1 ; then
        echo "ShellCheck is not installed" >&1
        exit 1
    fi
}


function shell_run_lint() {
62
    echo "  Linting Shell files"
63 64 65

    for file in $(config -e lint_shell_files '{shell}') ; do
        if ! shellcheck "$file" 2>&1 \
66
                | output_filter "  $(report_failure "$file")"; then
67 68
            lint_failed=$(( lint_failed+1 ))
        else
69
            report_success "    $file"
70 71 72 73 74
            lint_passed=$(( lint_passed+1 ))
        fi
        lint_total=$(( lint_total+1 ))
    done
}