diff --git a/mp-check/mp-check.sh b/mp-check/mp-check.sh index 5a751fb..10d6b28 100755 --- a/mp-check/mp-check.sh +++ b/mp-check/mp-check.sh @@ -65,9 +65,25 @@ bad_vars=( #################### ## util functions ## #################### +# prints the help info +help_cmd() { + info "MatterLinux package check script (mtsc ${MTSC_VERSION})" # sourced from mtsc-common + info "Usage: ${0} [archive file/source dir]" + info "Options:" + echo_color " $BOLD--fail-warn$RESET: fail on a warning" + echo_color " $BOLD--no-warn$RESET: ignore warnings" + echo + info "Licensed under GPLv3, see for more information" +} + # adds a new warning to the counter add_warning(){ + [ $OPT_NO_WARN -eq 1 ] && return + + warn "${1}" warnc=$((warnc+1)) + + [ $OPT_FAIL_WARN -eq 1 ] && fail_check } # cleans up the temp directoru @@ -123,7 +139,6 @@ check_archive(){ check_ret_fail "Failed to extract the archive" info "Checking archive files" - set_indent for f in "${required_files[@]}"; do if [ ! -f "${tmpdir}/${f}" ]; then @@ -132,9 +147,7 @@ check_archive(){ fi done - unset_indent info "Checking DATA file" - set_indent for k in "${required_keys[@]}"; do local line_1="$(grep "^${k}=" "${tmpdir}/DATA")" @@ -171,11 +184,7 @@ check_archive(){ filename="${name}_${version}.mpf" - success "Check was completed" - unset_indent - info "Checking HASHES file" - set_indent while read l; do if [ -z "${l}" ]; then @@ -196,11 +205,7 @@ check_archive(){ fi done < "${tmpdir}/HASHES" - success "Check was completed" - unset_indent - info "Checking files.tar.gz archive" - set_indent while read p; do if [ "${p:0:1}" == "." ] || [ "${p:0:1}" == "/" ]; then @@ -224,49 +229,31 @@ check_archive(){ fi done < <(tar tf "${tmpdir}/files.tar.gz") - success "Check was completed" - unset_indent - info "Checking INSTALL file" - set_indent if [ -f "${tmpdir}/INSTALL" ] && ! grep -q . "${tmpdir}/INSTALL"; then - warn "Package contains an empty install script" - add_warning + add_warning "Package contains an empty install script" fi - success "Check was completed" - unset_indent - info "Checking CHANGES file" - set_indent if ! grep -q . "${tmpdir}/CHANGES"; then - warn "Changes file is empty" - add_warning + add_warning "Changes file is empty" fi - success "Check was completed" - unset_indent - info "Checking archive name" - set_indent archivefile="$(basename "${archivepath}")" if [ "${archivefile}" != "${filename}" ]; then - warn "Package archive name is not ideal (${archivefile} -> ${filename})" - add_warning + add_warning "Package archive name is not ideal (${archivefile} -> ${filename})" fi - success "Check was completed" - unset_indent clean_tempdir } check_source(){ info "Checking the package script" - set_indent if [ ! -f "${sourcepath}/pkg.sh" ]; then error "Package script does not exist" @@ -288,8 +275,7 @@ check_source(){ local line_num=0 if [[ "${DESC}" == *"contains"* ]] || [[ "${DESC}" == *"provides"* ]]; then - warn "Avoid using words such as \"contains\" or \"provides\" in the package description" - add_warning + add_warning "Avoid using words such as \"contains\" or \"provides\" in the package description" fi if type INSTALL &>/dev/null; then @@ -306,22 +292,16 @@ check_source(){ for v in "${bad_vars[@]}"; do if echo "${l}" | grep "${v}" &> /dev/null; then - warn "${v} used without parenthesis on line ${line_num}" - add_warning + add_warning "${v} used without parenthesis on line ${line_num}" fi done if echo "${l}" | grep '&&' | grep -v 'cd ..' &> /dev/null; then - warn "Unreliable use of \"&&\" on line ${line_num}" - add_warning + add_warning "Unreliable use of \"&&\" on line ${line_num}" fi done < "${sourcepath}/pkg.sh" - success "Check was completed" - unset_indent - info "Checking the changes file" - set_indent if [ ! -f "${sourcepath}/changes.md" ]; then error "Package does not contain a changes file" @@ -329,33 +309,73 @@ check_source(){ fi if ! grep -q . "${sourcepath}/changes.md"; then - warn "Changes file is empty" - add_warning + add_warning "Changes file is empty" fi - - success "Check was completed" - unset_indent } ################# ## main script ## ################# -if [ -z "${1}" ]; then - error "Please specify a package archive or source directory" +OPT_FAIL_WARN=0 +OPT_NO_WARN=0 +OPT_TARGET=() + +for arg in "$@"; do + case $arg in + "--help") + help_cmd + exit 0 ;; + "--fail-warn") + OPT_FAIL_WARN=1 ;; + "--no-warn") + OPT_NO_WARN=1 ;; + --*) + error "Unknown option: ${arg}" + exit 1 ;; + *) + OPT_TARGET+=("${arg}") ;; + esac +done + +if [ -z "${OPT_TARGET}" ]; then + error "Please specify at least one package archive or a source directory, run --help for more info" exit 1 fi -if [ ! -f "${1}" ] && [ ! -d "${1}" ]; then - error "Specified path is invalid" +if [ $OPT_FAIL_WARN -eq 1 ] && [ $OPT_NO_WARN -eq 1 ]; then + error "Cannot use both of the --fail-warn and --no-warn options" exit 1 fi -if [ -f "${1}" ]; then - archivepath="$(realpath "${1}")" - check_archive -elif [ -d "${1}" ]; then - sourcepath="$(realpath "${1}")" - check_source -fi +info "Running mp-check with the options:" +print " $BOLD FAIL_WARN = $(itoyn $OPT_FAIL_WARN)" +print " $BOLD NO_WARN = $(itoyn $OPT_NO_WARN)" + +for target in "${OPT_TARGET[@]}"; do + if [ ! -f "${target}" ] && [ ! -d "${target}" ]; then + error "Specified path is invalid: ${target}" + exit 1 + fi +done + +tc="${#OPT_TARGET[@]}" +ti=0 + +for target in "${OPT_TARGET[@]}"; do + unset_indent + ti=$((ti + 1)) + + if [ -f "${target}" ]; then + info "(${ti}/${tc}) Checking the archive: ${target}" + set_indent + archivepath="$(realpath "${target}")" + check_archive + elif [ -d "${target}" ]; then + info "(${ti}/${tc}) Checking the source directory: ${target}" + set_indent + sourcepath="$(realpath "${target}")" + check_source + fi +done success_check