├── README.md └── cli.sh /README.md: -------------------------------------------------------------------------------- 1 | ![](https://raw.githubusercontent.com/b4b4r07/screenshots/master/cli/logo.png) 2 | 3 | [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] 4 | 5 | [license]: https://raw.githubusercontent.com/b4b4r07/dotfiles/master/doc/LICENSE-MIT.txt 6 | 7 | CLI is a simple tool that will simplify the download and install of binaries from GitHub Releases by common local installation (CLI). 8 | 9 | ## Description 10 | 11 | Recently, binaries of CLI tool written in Go language that is uploaded to GitHub Releases has been increasing. In addition, there is a tendency to upload the binaries in C and other compiled languages. That is where CLI come in. CLI allows you to simply and speedily install the binaries from GitHub Releases. It is quite easy! 12 | 13 | ***DEMO:*** 14 | 15 | ![](https://raw.githubusercontent.com/b4b4r07/screenshots/master/cli/demo.gif) 16 | 17 | ## Features 18 | 19 | - CLI want nothing but curl and wget 20 | - Easy and quick 21 | 22 | ## Usage 23 | 24 | ![](https://raw.githubusercontent.com/b4b4r07/screenshots/master/cli/installation.png) 25 | 26 | $ curl -L git.io/cli | L=user/repo sh 27 | 28 | Now finished! 29 | 30 | ## Installation 31 | 32 | To try **cli** on your local environment: 33 | 34 | $ cat ./cli.sh | sh 35 | 36 | ## License 37 | 38 | [MIT](https://raw.githubusercontent.com/b4b4r07/dotfiles/master/doc/LICENSE-MIT.txt) © BABAROT (a.k.a. b4b4r07) -------------------------------------------------------------------------------- /cli.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export PLATFORM 4 | 5 | has() { 6 | which "$1" >/dev/null 2>&1 7 | return $? 8 | } 9 | 10 | ink() { 11 | if [ "$#" -eq 0 -o "$#" -gt 2 ]; then 12 | return 1 13 | fi 14 | 15 | local open="\033[" 16 | local close="${open}0m" 17 | local black="0;30m" 18 | local red="1;31m" 19 | local green="1;32m" 20 | local yellow="1;33m" 21 | local blue="1;34m" 22 | local purple="1;35m" 23 | local cyan="1;36m" 24 | local gray="0;37m" 25 | local white="$close" 26 | 27 | local text="$1" 28 | local color="$close" 29 | 30 | if [ "$#" -eq 2 ]; then 31 | text="$2" 32 | case "$1" in 33 | black | red | green | yellow | blue | purple | cyan | gray | white) 34 | eval color="\$$1" 35 | ;; 36 | esac 37 | fi 38 | 39 | printf "${open}${color}${text}${close}" 40 | } 41 | 42 | log() { 43 | if [ "$#" -eq 0 -o "$#" -gt 2 ]; then 44 | return 1 45 | fi 46 | 47 | local color= 48 | local text="$2" 49 | 50 | case "$1" in 51 | TITLE) 52 | color=yellow 53 | ;; 54 | ERROR | WARN) 55 | color=red 56 | ;; 57 | INFO) 58 | color=green 59 | ;; 60 | SUCCESS) 61 | color=green 62 | ;; 63 | *) 64 | text="$1" 65 | esac 66 | 67 | timestamp() { 68 | ink gray "[" 69 | ink purple "$(date +%H:%M:%S)" 70 | ink gray "] " 71 | } 72 | 73 | timestamp; ink "$color" "$text"; echo 74 | } 75 | 76 | ok() { 77 | log SUCCESS "$1" 78 | } 79 | 80 | die() { 81 | log ERROR "$1" 1>&2 82 | } 83 | 84 | lower() { 85 | if [ $# -eq 0 ]; then 86 | cat <&0 87 | elif [ $# -eq 1 ]; then 88 | if [ -f "$1" -a -r "$1" ]; then 89 | cat "$1" 90 | else 91 | echo "$1" 92 | fi 93 | else 94 | return 1 95 | fi | tr "[:upper:]" "[:lower:]" 96 | } 97 | 98 | ostype() { 99 | uname | lower 100 | } 101 | 102 | # os_detect export the PLATFORM variable as you see fit 103 | os_detect() { 104 | export PLATFORM 105 | case "$(ostype)" in 106 | *'linux'*) PLATFORM='linux' ;; 107 | *'darwin'*) PLATFORM='darwin' ;; 108 | *'bsd'*) PLATFORM='bsd' ;; 109 | *) PLATFORM='unknown' ;; 110 | esac 111 | } 112 | 113 | extract() { 114 | if [ -f "$1" ] ; then 115 | case "$1" in 116 | *.tar.bz2) tar xvjf "$1" ;; 117 | *.tar.gz) tar xvzf "$1" ;; 118 | *.bz2) bunzip2 "$1" ;; 119 | *.rar) unrar x "$1" ;; 120 | *.gz) gunzip "$1" ;; 121 | *.tar) tar xvf "$1" ;; 122 | *.tbz2) tar xvjf "$1" ;; 123 | *.tgz) tar xvzf "$1" ;; 124 | *.zip) unzip "$1" ;; 125 | *.Z) uncompress "$1" ;; 126 | *.7z) 7z x "$1" ;; 127 | *) return 1 ;; 128 | esac 129 | else 130 | echo "missing file" 1>&2 131 | return 1 132 | fi 133 | } 134 | 135 | check_dependencies() { 136 | local err 137 | err=0 138 | 139 | sleep 1 140 | 141 | if ! has "wget"; then 142 | err=1 143 | die "wget: not found" 144 | fi 145 | 146 | if ! has "curl"; then 147 | err=1 148 | die "wget: not found" 149 | fi 150 | 151 | if [ "$err" -ne 0 ]; then 152 | exit 1 153 | fi 154 | } 155 | 156 | main() { 157 | log TITLE "== Bootstraping enhancd ==" 158 | 159 | # check dependencies 160 | check_dependencies 161 | 162 | log INFO "Installing dependencies..." 163 | sleep 1 164 | 165 | # include blank $L and check user/repo 166 | echo "$L" | grep -q "^[A-Za-z0-9_-]\+/[A-Za-z0-9_-]\+$" 167 | if [ $? -ne 0 ]; then 168 | die "sets the L environment variable to the repository URL (username/reponame) you want to install" 169 | exit 1 170 | fi 171 | 172 | USER="$(echo "$L" | awk -F"/" '{print $1}')" 173 | REPO="$(echo "$L" | awk -F"/" '{print $2}')" 174 | 175 | # detect os 176 | os_detect 177 | cd /tmp 178 | 179 | local releases i path bin re ok 180 | 181 | # Same as 182 | # curl --fail -X GET https://api.github.com/repos/b4b4r07/gomi/releases/latest | jq '.assets[0].browser_download_url' | xargs curl -L -O 183 | # http://stackoverflow.com/questions/24987542/is-there-a-link-to-github-for-downloading-a-file-in-the-latest-release-of-a-repo 184 | # http://stackoverflow.com/questions/18384873/how-to-list-the-releases-of-a-repository 185 | # http://stackoverflow.com/questions/5207269/releasing-a-build-artifact-on-github 186 | #if has "jq"; then 187 | # curl --fail -X GET https://api.github.com/repos/b4b4r07/gomi/releases/latest | jq -r '.assets[].browser_download_url' | grep $PLATFORM | wget -i - 188 | #fi 189 | 190 | log INFO "Getting releases information and scraping it...\n" 191 | releases="$(curl -sSf -L https://github.com/"${USER}"/"${REPO}"/releases/latest \ 192 | | egrep -o '/'"${USER}"'/'"${REPO}"'/releases/download/[^"]*' \ 193 | | grep $PLATFORM 194 | )" 195 | 196 | # github releases not available 197 | if [ -z "$releases" ]; then 198 | die "URL that can be used as Github releases was not found" 199 | exit 1 200 | fi 201 | 202 | # download github releases for USER's platform 203 | log INFO "downloading $releases\n" 204 | echo "$releases" | wget --base=http://github.com/ -i - 205 | 206 | # Main processing 207 | # 208 | # check machine architecture 209 | ok=0 210 | re=$(uname -m | grep -o "..$") 211 | for i in $releases 212 | do 213 | bin="$(basename "$i" | grep "$re")" 214 | if [ -n "$bin" -a -f "$bin" ]; then 215 | if extract "$bin" 2>/dev/null; then 216 | log INFO "extract ${bin}..." 217 | if [ -f "$REPO" -a -x "$REPO" ]; then 218 | # do nothing 219 | bin="" 220 | else 221 | dir="$(ls -1 -F | grep "$REPO" | grep "/$" | head)" 222 | if [ -z "$dir" ]; then 223 | die "$REPO: not found" 224 | exit 1 225 | fi 226 | 227 | if [ -f "$dir/$REPO" -a -x "$dir/$REPO" ]; then 228 | bin="$dir/$REPO" 229 | fi 230 | fi 231 | fi 232 | 233 | # Make a copy of REPO and rename to REPO 234 | cp "$bin" "$REPO" 2>/dev/null 235 | chmod 755 "$REPO" 236 | 237 | # Find the directory that you can install from $PATH 238 | for path in ${PATH//:/ } 239 | do 240 | log INFO "installing to $path..." 241 | install -m 0755 "$REPO" "$path" >/dev/null 2>&1 242 | if [ $? -eq 0 ]; then 243 | ok "installed $REPO to $path successfully" 244 | ok=1 245 | break 246 | fi 247 | done 248 | 249 | # One binary is enough to complete this installation 250 | break 251 | fi 252 | done 253 | 254 | # no binary can execute 255 | if [ $ok -eq 0 ]; then 256 | die "there is no binary that can execute on this platform" 257 | die "or, the all PATHs require sudoer; please add new path to the PATH" 258 | exit 1 259 | fi 260 | 261 | # Cleanup! 262 | # remove the intermediate files 263 | # thus complete the installation 264 | for i in $releases 265 | do 266 | rm -f "$(basename "$i")" 267 | done 268 | 269 | # Notification log 270 | if has "$REPO"; then 271 | ok "Now finished!" 272 | # cleanup 273 | rm -f "$REPO" 274 | else 275 | die "$REPO: incomplete or unsuccessful installations" 276 | echo "please put ./$REPO to somewhere you want" 1>&2 277 | echo "(on UNIX-ly systems, /usr/local/bin or the like)" 1>&2 278 | echo "you should run 'mv ./$REPO /usr/local/bin' now" 1>&2 279 | exit 1 280 | fi 281 | } 282 | 283 | if echo "$-" | grep -q "i"; then 284 | # -> source a.sh 285 | return 286 | 287 | else 288 | # three patterns 289 | # -> cat a.sh | bash 290 | # -> bash -c "$(cat a.sh)" 291 | # -> bash a.sh 292 | if [ -z "$BASH_VERSION" ]; then 293 | die "This installation requires bash" 294 | exit 1 295 | fi 296 | 297 | if [ "$0" = "${BASH_SOURCE:-}" ]; then 298 | # -> bash a.sh 299 | exit 300 | fi 301 | 302 | if [ -n "${BASH_EXECUTION_STRING:-}" ] || [ -p /dev/stdin ]; then 303 | trap "die 'terminated $0:$LINENO'; exit 1" INT ERR 304 | # -> cat a.sh | bash 305 | # -> bash -c "$(cat a.sh)" 306 | main 307 | fi 308 | fi 309 | --------------------------------------------------------------------------------