summaryrefslogtreecommitdiff
path: root/src/shared/feedback.sh
blob: 9582ad8b90e5dfc571588d6526cb8a1969bef8e7 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
 ##############################################################################
 #                      parabola-riscv64-bootstrap                            #
 #                                                                            #
 #    Copyright (C) 2018  Andreas Grapentin                                   #
 #                                                                            #
 #    This program is free software: you can redistribute it and/or modify    #
 #    it under the terms of the GNU General Public License as published by    #
 #    the Free Software Foundation, either version 3 of the License, or       #
 #    (at your option) any later version.                                     #
 #                                                                            #
 #    This program is distributed in the hope that it will be useful,         #
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
 #    GNU General Public License for more details.                            #
 #                                                                            #
 #    You should have received a copy of the GNU General Public License       #
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.   #
 ##############################################################################

# error codes
export ERROR_UNSPECIFIED=1
export ERROR_INVOCATION=2
export ERROR_MISSING=3
export ERROR_BUILDFAIL=4
export ERROR_KEYFAIL=5

# messaging functions
notify() {
  if type -p notify-send >/dev/null; then
    machinectl -q shell --uid="$SUDO_USER" .host \
      "$(command -v notify-send)" "$@" >/dev/null
  fi
}

msg() {
  local OPTIND o n='' d=()
  while getopts "d:n" o; do
    case "$o" in
      n) n=yes ;;
      d) d=(-h string:document:"$OPTARG") ;;
      *) die -e "$ERROR_INVOCATION" "Usage: ${FUNCNAME[0]} [-n] [-d file] msg ..." ;;
    esac
  done
  shift $((OPTIND-1))

  [ "x$n" == "xyes" ] && notify "${d[@]}" "${@//_/\\_}"
  echo "$(tput bold)$(tput setf 2)==>$(tput setf 7) $*$(tput sgr0)";
}

error() {
  local OPTIND o n='' d=()
  while getopts "d:n" o; do
    case "$o" in
      n) n=yes ;;
      d) d=(-h string:document:"$OPTARG") ;;
      *) die -e "$ERROR_INVOCATION" "Usage: ${FUNCNAME[0]} [-n] [-d file] msg ..." ;;
    esac
  done
  shift $((OPTIND-1))

  [ "x$n" == "xyes" ] && notify -c error "${d[@]}" "${@//_/\\_}"
  echo "$(tput bold)$(tput setf 4)==> ERROR:$(tput setf 7) $*$(tput sgr0)" 1>&2
}

die() {
  local OPTIND o e="$ERROR_UNSPECIFIED" d=()
  while getopts "e:d:" o; do
    case "$o" in
      e) e="$OPTARG" ;;
      d) d=(-d "$OPTARG") ;;
      *) die -e "$ERROR_INVOCATION" "Usage: ${FUNCNAME[0]} [-e status] [-d file] msg ..." ;;
    esac
  done
  shift $((OPTIND-1))

  error -n "${d[@]}" "$@"
  trap - ERR
  exit "$e"
}
trap 'die "unknown error"' ERR