From 1efe6f913e890554cad1f58efc291942727290db Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 9 Jun 2017 22:00:56 +0100 Subject: [PATCH] Update tldr --- tldr | 75 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/tldr b/tldr index 7934924..22c82e2 100755 --- a/tldr +++ b/tldr @@ -1,8 +1,8 @@ #!/bin/bash set +vx -o pipefail [[ $- = *i* ]] && echo "Don't source this script!" && return 1 -version='0.3' -# tldr-bash-client version 0.3 +version='0.32' +# tldr-bash-client version 0.32 # Bash client for tldr: community driven man-by-example # - forked from Ray Lee, http://github.com/raylee/tldr # - modified and expanded by pepa65: http://github.com/pepa65/tldr-bash-client @@ -176,7 +176,7 @@ Recent(){ find "$1" -mtime -"${TLDR_EXPIRY// /}" >/dev/null 2>&1;} # Download index.json; Uses: index index_url base_url zip_url dl Update_index(){ $dl "$index" "$index_url" && Inf "Index file $I$index$XI freshly downloaded" || { - Err "Could not download index from $I$index_url$XI" + Err "Could not download index from $I$index_url$XI with $dl" exit 2 } } @@ -196,8 +196,8 @@ Config(){ version="tldr-bash-client version $version $XB$URL http://github.com/pepa65/tldr-bash-client$XURL" # Select download method - dl="$(type -p curl) -sfo" || { - dl="$(type -p wget) -qNO" || { + dl="$(type -p curl) -sLfo" || { + dl="$(type -p wget) --max-redirect=20 -qNO" || { Err "tldr requires$I curl$XI or$I wget $XI installed in your path" exit 3 } @@ -224,7 +224,7 @@ Config(){ # $1: error message; Uses: md line ln Unlinted(){ Err "Page $I$md$XI not properly linted!$N${ERRSP}${ERR}Line $I$ln$XI [$XERR$U$line$XU$ERR]$N$ERRSP$ERR$1" - exit 4 + exit 5 } # $1: page; Uses: index index_url cachedir base_url platform os dl cached md @@ -238,10 +238,10 @@ Get_tldr(){ [[ $desc ]] || return # just not found - err=0 + error=0 if [[ $platform ]] then # platform given on commandline - [[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && err=1 || md=$platform/$1.md + [[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && error=1 || md=$platform/$1.md else # check common [[ $desc =~ \"common\" ]] && md=common/$1.md || { # not in common either [[ $notfound ]] && notfound+=" or " @@ -253,17 +253,17 @@ Get_tldr(){ [[ $desc =~ \"$os\" ]] && md=$os/$1.md } || { notfound+=" or $I$os$XI" - err=1 + error=1 } # if still no page found, get the first entry in index [[ $md ]] || md=$(cut -d "$Q" -f 8 <<<"$desc")/"$1.md" - ((err)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead" + ((error)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead" # return the local cached copy of the tldrpage, or retrieve and cache from github cached=$cachedir/$md Recent "$cached" || { mkdir -p "${cached%/*}" - $dl "$cached" "$base_url/$md" || Err "Could not download page $I$cached$XI from index $U$index_url$XU" + $dl "$cached" "$base_url/$md" || Err "Could not download page $I$cached$XI from index $U$index_url$XU with $dl" } } @@ -324,7 +324,7 @@ Display_tldr(){ Out "$ENL$EPS$E$line$XE" ;; esac done <"$1" - trap 'less +Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT + trap 'less -Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT } # $1: exit code; Uses: platform index @@ -340,18 +340,21 @@ List_pages(){ # $1: exit code; Uses: dl cachedir zip_url Cache_fill(){ local tmp unzip + unzip="$(type -p unzip) -q" || { + Err "Unzip is necessary to fill the cache" + exit 6 + } tmp=$(mktemp -d) $dl "$tmp/pages.zip" "$zip_url" || { rm -- "$tmp" - Err "Could not download pages archive from $U$zip_url$XU" - exit 6 - } - unzip="$(type -p unzip) -q" || { - rm -- "$tmp" - Err "Unzip is necessary to fill the cache" + Err "Could not download pages archive from $U$zip_url$XU with $dl" exit 7 } - $unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' + $unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' || { + rm -- "$tmp" + Err "Couldn't unzip the cache archive on $tmp/pages.zip" + exit 8 + } rm -rf -- "${cachedir:?}/"* mv -- "$tmp/pages/"* "${cachedir:?}/" rm -rf -- "$tmp" @@ -361,7 +364,7 @@ Cache_fill(){ # $@: commandline parameters; Uses: version cached; Sets: platform page Main(){ - local markdown err + local markdown err nomore='No more command line arguments allowed' Config markdown=0 err=0 case "$1" in @@ -369,48 +372,48 @@ Main(){ platform=$2 [[ ,common,linux,osx,sunos, = *,$platform,* ]] || { Err "Unknown platform $I$platform$XI" - Usage 8 + Usage 9 } - [[ $3 ]] && Err "No more command line arguments allowed" && err=9 + [[ $3 ]] && Err "$nomore" && err=10 } List_pages "$err" ;; - -c|--cache) [[ $2 ]] && Err "No more command line arguments allowed" && err=10 + -c|--cache) [[ $2 ]] && Err "$nomore" && err=11 Cache_fill "$err" ;; - -v|--version) [[ $2 ]] && Err "No more command line arguments allowed" && err=11 + -v|--version) [[ $2 ]] && Err "$nomore" && err=12 Inf "$version" exit "$err" ;; - -u|--update) [[ $2 ]] && Err "No more command line arguments allowed" && err=12 + -u|--update) [[ $2 ]] && Err "$nomore" && err=13 Update_index exit "$err" ;; - -r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 13 - [[ $3 ]] && Err "No more command line arguments allowed" && err=14 + -r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 14 + [[ $3 ]] && Err "$nomore" && err=15 [[ -f "$2" ]] && { Display_tldr "$2" && exit "$err" Err "A file error occured" - exit 15 - } || Err "No file:$I $2$XI" && exit 16 ;; + exit 16 + } || Err "No file:$I $2$XI" && exit 17 ;; -m|--markdown) shift page=$* - [[ -z $page ]] && Err "Specify a page to display" && Usage 17 + [[ -z $page ]] && Err "Specify a page to display" && Usage 18 [[ -f "$page" && ${page: -3:3} = .md ]] && Out "$(cat "$page")" && exit 0 markdown=1 ;; - ''|-h|-\?|--help) [[ $2 ]] && Err "No more command line arguments allowed" && err=18 + ''|-h|-\?|--help) [[ $2 ]] && Err "$nomore" && err=19 Usage "$err" ;; - -*) Err "Unrecognized option $I$1$XI"; Usage 19 ;; + -*) Err "Unrecognized option $I$1$XI"; Usage 20 ;; *) page=$* ;; esac - [[ -z $page ]] && Err "No command specified" && Usage 20 - [[ $page =~ ' -' || ${page:0:1} = '-' ]] && Err "Only one option allowed" && Usage 21 + [[ -z $page ]] && Err "No command specified" && Usage 21 + [[ $page =~ ' -' || ${page:0:1} = '-' ]] && Err "Only one option allowed" && Usage 22 [[ $page = */* ]] && platform=${page%/*} && page=${page##*/} [[ $platform && ,common,linux,osx,sunos, != *,$platform,* ]] && { Err "Unknown platform $I$platform$XI" - Usage 22 + Usage 23 } Get_tldr "${page// /-}" [[ ! -s $cached ]] && Err "tldr page for command $I$page$XI not found" \ - && Inf "Contribute new pages at:$XB$URL https://github.com/tldr-pages/tldr$XURL" && exit 23 + && Inf "Contribute new pages at:$XB$URL https://github.com/tldr-pages/tldr$XURL" && exit 24 ((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached" }