~tardypad/dotfiles

ref: HEAD dotfiles/utilities/report-versions -rwxr-xr-x 2.0 KiB
2a7e6d96Damien Tardy-Panis Add missing backup-dir in backup script for quickmarks 5 days ago
                                                                                
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/sh
# commands used:
# - column
# - pacman

init_variables() {
  COMMAND=${0##*/}

  ONLY_DIFF=0

  TYPE=
}

show_usage() {
  cat <<- EOF
	usage: ${COMMAND} [<options>]

	Check and report packages versions:
	- version installed on the system
	- version listed in the docs

	Options:
	  -d    only list packages where versions are different
	  -h    show this message only
	EOF
}

parse_command_line() {
  while getopts dh OPT; do
    case "${OPT}" in
      d) ONLY_DIFF=1 ;;
      h) show_usage; exit 0 ;;
      ?) exit_error ;;
    esac
  done

  shift $(( OPTIND - 1 ))
}

# shellcheck disable=SC2120
exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  echo "Try '${COMMAND} -h' for more information."
  exit 1
} >&2

docs_version() {
  ITEM="$1"

  sed -n "s#^| ${ITEM} .*| \[\(.*\)\].*#\1#p" "doc/${TYPE}.md"
}

pacman_version() {
  PACKAGE="$1"

  PACMAN_INFO=$(
    pacman -Qi "${PACKAGE}" 2> /dev/null
  )

  if [ -z "${PACMAN_INFO}" ]; then
    # try with -git suffix if package couldn't be found
    PACMAN_INFO=$(
      pacman -Qi "${PACKAGE}-git" 2> /dev/null
    )
  fi

  if [ -z "${PACMAN_INFO}" ]; then
    # try with -bin suffix if package couldn't be found
    PACMAN_INFO=$(
      pacman -Qi "${PACKAGE}-bin" 2> /dev/null
    )
  fi

  if [ -z "${PACMAN_INFO}" ]; then
    echo 'unknown'
    return
  fi

  # extract version without the release number
  echo "${PACMAN_INFO}" \
    | sed -n 's/^Version *: \(.*\)\-[0-9]*/\1/p'
}

report_item() {
  while read -r ITEM; do
    VERSION_INSTALLED=$( pacman_version "${ITEM}" )

    VERSION_DOCS=$( docs_version "${ITEM}" )

    if [ "${ONLY_DIFF}" -eq 1 ] \
       && [ "${VERSION_INSTALLED}" = "${VERSION_DOCS}" ]; then
      continue
    fi

    printf '%s %s %s\n' \
      "${ITEM}" \
      "${VERSION_DOCS}" \
      "${VERSION_INSTALLED}"
  done
}

list_items() {
  sed -n 's#^| \([a-z][^ ]*\) .*#\1#p' "doc/${TYPE}.md"
}

report_all() {
  {
    for TYPE in PACKAGES PLUGINS; do
      list_items | report_item
    done
  } \
    | sort \
    | column -t
}

init_variables
parse_command_line "$@"

report_all