aboutsummaryrefslogtreecommitdiff
path: root/bin/rfc
diff options
context:
space:
mode:
Diffstat (limited to 'bin/rfc')
-rwxr-xr-xbin/rfc124
1 files changed, 82 insertions, 42 deletions
diff --git a/bin/rfc b/bin/rfc
index a483595..168c59e 100755
--- a/bin/rfc
+++ b/bin/rfc
@@ -1,12 +1,21 @@
#!/bin/sh
set -eu
-TARBALL_URL='https://www.rfc-editor.org/in-notes/tar/RFC-all.tar.gz'
+D="${XDG_DATA_HOME:-$HOME/.local/share}/doc/rfc"
+PROMPT_DB="$(
+ cat <<-EOF
+ RFC directory does not exist:
+ $D/
+
+ Do you want to download the files to create it?
+ EOF
+)"
usage() {
cat <<-'EOF'
Usage:
rfc [-w] RFC_NUMBER
+ rfc -u
rfc -h
EOF
}
@@ -15,8 +24,9 @@ help() {
cat <<-'EOF'
Options:
- -w Show the path to the RFC file instead of displaying
- its contents.
+ -w show the path to the RFC file instead of displaying
+ its contents
+ -u update the local RFC database
-h, --help show this message
Lookup the given RFC
@@ -25,14 +35,66 @@ help() {
$ $PAGER $XDG_DATA_HOME/doc/rfc/rfc$RFC_NUMBER.txt
- If the $XDG_DATA_HOME/doc/rfc/ directory doesn't exist, it tries to
- create it by downloading the latest RFC tarball [0] and placing all .txt
+ If the $XDG_DATA_HOME/doc/rfc/ directory doesn't exist, it gets
+ created it by downloading the latest RFC files and placing all .txt
files there.
+
+
+ Examples:
+
+
+ Show RFC 1234 in $PAGER:
+
+ $ rfc 1234
+
+
+ Print path to RFC 2222:
+
+ $ rfc 2222
+
+
+ Download the latest RFCs:
+
+ $ rfc -u
EOF
+}
- printf '\n[0]: %s\n' "$TARBALL_URL"
+view() {
+ if [ -t 1 ]; then
+ ${PAGER:-cat}
+ else
+ cat
+ fi
+}
+
+update() {
+ rsync -avzP --delete ftp.rfc-editor.org::rfcs-text-only "$D"
+ STATUS=$?
+ if [ "$STATUS" != 0 ]; then
+ exit "$STATUS"
+ fi
}
+check_local_db() {
+ if [ ! -e "$D" ]; then
+ if prompt "$PROMPT_DB"; then
+ update
+ else
+ echo 'No local RFC database to operate on.' >&2
+ exit 1
+ fi
+ fi
+}
+
+assert_arg() {
+ if [ -z "$1" ]; then
+ printf 'Missing %s.\n\n' "$2" >&2
+ usage >&2
+ exit 2
+ fi
+}
+
+
for flag in "$@"; do
case "$flag" in
--)
@@ -48,11 +110,14 @@ for flag in "$@"; do
esac
done
-while getopts 'wh' flag; do
+while getopts 'wuh' flag; do
case "$flag" in
w)
WHERE=true
;;
+ u)
+ UPDATE=true
+ ;;
h)
usage
help
@@ -67,34 +132,16 @@ done
shift $((OPTIND - 1))
RFC_NUMBER="${1:-}"
-if [ -z "$RFC_NUMBER" ]; then
- echo 'Missing argument RFC_NUMBER' >&2
- usage >&2
- exit 2
-fi
+F="$D/rfc${RFC_NUMBER}.txt"
-D="${XDG_DATA_HOME:-$HOME/.local/share}/doc/rfc"
-if [ ! -e "$D" ]; then
- printf 'RFC directory does not exist:\n\t%s/\n\n' "$D"
- printf 'Do you want to download the files to create it? [Y/n] '
- read -r yesno
- if [ "$yesno" != 'n' ] && [ "$yesno" != 'N' ]; then
- CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/rfc-cli"
- mkdir -p "$CACHE_DIR"
- wget -cO "$CACHE_DIR"/RFC-all.tar.gz "$TARBALL_URL"
- rm -rf "$CACHE_DIR/tmp"
- mkdir -p "$CACHE_DIR/tmp"
- tar \
- -C "$CACHE_DIR/tmp" \
- -xvf "$CACHE_DIR"/RFC-all.tar.gz \
- --wildcards \
- 'rfc*.txt'
- mkdir -p "$(dirname "$D")"
- mv "$CACHE_DIR/tmp" "$D"
- fi
+
+if [ "${UPDATE:-}" = true ]; then
+ update
+ exit
fi
-F="$D/rfc${RFC_NUMBER}.txt"
+assert_arg "$RFC_NUMBER"
+
if [ ! -e "$F" ]; then
printf 'Given RFC_NUMBER "%s" does not exist at:\n%s\n' \
"$RFC_NUMBER" "$F" >&2
@@ -104,14 +151,7 @@ fi
if [ "${WHERE:-}" = true ]; then
printf '%s\n' "$F"
exit
+else
+ view < "$F"
+ exit
fi
-
-view() {
- if [ -t 1 ]; then
- ${PAGER:-cat}
- else
- cat
- fi
-}
-
-view < "$F"