#!/bin/sh # shellcheck source=../lib/load-configuration . "${0%/*}/../lib/load-configuration" # TODO: deduplicate matching logic (with bin/return-assignment) # shellcheck disable=SC2016 usage() { >&2 echo '' >&2 echo 'manage-fail-reasons $action [parameters]: manage the list of fail reasons' >&2 echo '' >&2 echo 'possible actions:' >&2 echo ' list list installed fail reasons' >&2 echo ' check check a build log for fail reasons (file or URL)' exit 1 } if [ $# -eq 0 ]; then usage fi case "$1" in 'list') # shellcheck disable=SC2016 fail_reason_identifiers=$( { printf 'SELECT `fail_reasons`.`id`,name,`fail_reasons`.`severity`,replace(to_base64(`fail_reasons`.`identifier`),"\\n","")' printf ' FROM `fail_reasons` ORDER BY `fail_reasons`.`severity` ASC, `fail_reasons`.id ASC' } | \ mysql_run_query ) printf '%s\n' "${fail_reason_identifiers}" | \ while read -r reason_id name severity identifier; do ident=$(printf '%s' "${identifier}" | base64 -d -) printf "%s %s %s %s\n" "$reason_id" "$name" "$severity" "$ident" done ;; 'check') shift if [ $# -ne 1 ]; then >&2 printf '"check" expects 1 parameter (a path to a build log), %s were given\n' "$#" usage fi case "$1" in http*) tmp_file=$(mktemp 'tmp.manage-fail-reasons.XXXXXXXXXX' --tmpdir) trap 'rm "${tmp_file}"' EXIT wget -q -O "$tmp_file" "$1" filelog="$tmp_file" ;; *) filelog="$1" ;; esac fail_reason_identifiers=$( # shellcheck disable=SC2016 { printf 'SELECT id,severity,name,replace(to_base64(`fail_reasons`.`identifier`),"\\n","")' printf ' FROM `fail_reasons` ORDER BY `fail_reasons`.`severity` ASC, `fail_reasons`.id ASC' } | \ mysql_run_query ) printf '%s\n' "${fail_reason_identifiers}" | \ while read -r id severity name identifier; do if zgrep -qx "\s*$( printf '%s' "${identifier}" | \ base64 -d )\s*" \ "$filelog"; then echo 'match' ident=$(printf '%s' "${identifier}" | base64 -d -) printf '%s (%s,%s) matches with severity %s\n' "$name" "$id" "$ident" "$severity" fi done zcat "$filelog" | tail -n 4 ;; *) >&2 printf 'unknown action "%s"\n' "$1" usage ;; esac