1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p jq ncurses
3# shellcheck shell=bash
4
5# Get a nixpkgs maintainer's metadata as a JSON object
6# see HELP_MESSAGE just below, or README.md.
7
8set -euo pipefail
9
10declare -A SELECTORS=( [handle]= [email]= [github]= [githubId]= [matrix]= [name]= )
11HELP_MESSAGE="usage: '$0' [selector] value
12examples:
13 get-maintainer.sh nicoo
14 get-maintainer.sh githubId 1155801
15
16\`selector\` defaults to 'handle', can be one of:
17 ${!SELECTORS[*]}
18"
19
20MAINTAINERS_DIR="$(dirname "$0")/.."
21
22die() {
23 tput setaf 1 # red
24 echo "'$0': $*"
25 tput setaf 0 # back to black
26 exit 1
27}
28
29listAsJSON() {
30 nix-instantiate --eval --strict --json "${MAINTAINERS_DIR}/maintainer-list.nix"
31}
32
33parseArgs() {
34 [ $# -gt 0 -a $# -lt 3 ] || {
35 echo "$HELP_MESSAGE"
36 die "invalid number of arguments (must be 1 or 2)"
37 }
38
39 if [ $# -eq 1 ]; then
40 selector=handle
41 else
42 selector="$1"
43 shift
44 fi
45 [ -z "${SELECTORS[$selector]-n}" ] || {
46 echo "Valid selectors are:" "${!SELECTORS[@]}" >&2
47 die "invalid selector '$selector'"
48 }
49
50 value="$1"
51 shift
52}
53
54query() {
55 # explode { a: A, b: B, ... } into A + {handle: a}, B + {handle: b}, ...
56 local explode="to_entries[] | .value + { \"handle\": .key }"
57
58 # select matching items from the list
59 # TODO(nicoo): Support approximate matching for `name` ?
60 local select
61 case "$selector" in
62 githubId)
63 select="select(.${selector} == $value)"
64 ;;
65 *)
66 select="select(.${selector} == \"$value\")"
67 esac
68
69 echo "$explode | $select"
70}
71
72parseArgs "$@"
73listAsJSON | jq -e "$(query)"