Skip to content

Commit 17898c7

Browse files
nikcorghalostatue
authored andcommitted
feat: reverse listing order option
npryce#128
1 parent d995b9b commit 17898c7

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

src/_adr_generate_toc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@ eval "$("$(dirname "$0")"/adr-config)"
99
## Options:
1010
##
1111
## -s include the state of each decision next to its link.
12-
## -i INTRO precede the table of contents with text from the INTRO file.
13-
## -o OUTRO follow the table of contents with text from the given OUTRO file.
12+
## -i INTRO precede the table of contents with the given INTRO text.
13+
## -o OUTRO follow the table of contents with the given OUTRO text.
1414
## -p LINK_PREFIX
1515
## prefix each decision file link with LINK_PREFIX.
16+
## -r list the records in reverse order
1617
##
1718
## Both INTRO and OUTRO must be in Markdown format.
1819

1920
link_prefix=
2021
output_status=
22+
list_args=()
2123

22-
while getopts si:o:p: arg; do
24+
while getopts rsi:o:p: arg; do
2325
case "$arg" in
2426
s)
2527
output_status=1
2628
shift
2729
;;
30+
r)
31+
list_args+=("-r")
32+
;;
2833
i)
2934
intro="$OPTARG"
3035
;;
@@ -42,13 +47,12 @@ cat <<EOF
4247
4348
EOF
4449

45-
if [ -n "$intro" ]
46-
then
47-
cat "$intro"
48-
echo
50+
if [ -n "$intro" ]; then
51+
cat "$intro"
52+
echo
4953
fi
5054

51-
for f in $("$adr_bin_dir/adr-list"); do
55+
for f in $("$adr_bin_dir/adr-list" "${list_args[@]}"); do
5256
title=$("$adr_bin_dir/_adr_title" "$f")
5357
link=${link_prefix}$(basename "$f")
5458
status=
@@ -68,4 +72,3 @@ if [ ! -z "$outro" ]; then
6872
echo
6973
cat "$outro"
7074
fi
71-

src/adr-list

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,37 @@
22
set -e
33
eval "$("$(dirname "$0")"/adr-config)"
44

5-
## usage: adr list
5+
## usage: adr list [-r]
66
##
77
## Lists the architecture decision records
8+
##
9+
## Options:
10+
##
11+
## -r list decision records in reverse order
812

913
adr_dir="$("$adr_bin_dir/_adr_dir")"
1014

15+
sort_args=()
16+
17+
if (( $# > 0 )); then
18+
while getopts ":r" arg; do
19+
case "${arg}" in
20+
r)
21+
sort_args+=("-r")
22+
;;
23+
24+
*)
25+
echo "invalid argument: $OPTARG" >&2
26+
exit 1
27+
;;
28+
esac
29+
done
30+
shift $((OPTIND-1))
31+
fi
32+
1133
if [ -d "$adr_dir" ]
1234
then
13-
find "$adr_dir" -maxdepth 1 -type f | grep -E "^.{${#adr_dir}}/[0-9]*[1-9][0-9]*-[^/]*\\.md$" | sort
35+
find "$adr_dir" | grep -E "^$adr_dir/[0-9]+-[^/]*\\.md" | sort "${sort_args[@]}"
1436
else
1537
echo "The $adr_dir directory does not exist"
1638
exit 1

tests/generate-contents.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ adr generate toc
1010
* [1. First Decision](0001-first-decision.md)
1111
* [2. Second Decision](0002-second-decision.md)
1212
* [3. Third Decision](0003-third-decision.md)
13+
adr generate toc -r
14+
# Architecture Decision Records
15+
16+
* [3. Third Decision](0003-third-decision.md)
17+
* [2. Second Decision](0002-second-decision.md)
18+
* [1. First Decision](0001-first-decision.md)

tests/generate-contents.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ adr new First Decision
22
adr new Second Decision
33
adr new Third Decision
44
adr generate toc
5+
adr generate toc -r

tests/list-records.expected

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ adr list
1515
doc/adr/0001-first.md
1616
doc/adr/0002-second.md
1717
doc/adr/0003-third.md
18-
18+
adr list -r
19+
doc/adr/0003-third.md
20+
doc/adr/0002-second.md
21+
doc/adr/0001-first.md

tests/list-records.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ adr new second
55
adr list
66
adr new third
77
adr list
8-
8+
adr list -r

0 commit comments

Comments
 (0)