diff options
| author | typebrook <typebrook@gmail.com> | 2020-02-09 13:40:56 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-02-09 13:40:56 +0800 |
| commit | 43e3832e6ff4d35e25fec9174efe7c40e24a0dc4 (patch) | |
| tree | 46a214eaed40906cbb68ad4142934847ec68472d | |
| parent | 998cc09f486a6d097ba8573a9ccad96324cfbf7f (diff) | |
update
| -rwxr-xr-x | scripts/gist | 84 |
1 files changed, 67 insertions, 17 deletions
diff --git a/scripts/gist b/scripts/gist index c4ed57b..8bdb2c2 100755 --- a/scripts/gist +++ b/scripts/gist | |||
| @@ -8,22 +8,23 @@ | |||
| 8 | # Description: Manage your gists with git and Github API v3 | 8 | # Description: Manage your gists with git and Github API v3 |
| 9 | # Usage: gist [command] [<args>] | 9 | # Usage: gist [command] [<args>] |
| 10 | # | 10 | # |
| 11 | # [star | s] list your gists with format below, star for your starred gists: | 11 | # [star | s] List your gists with format below, star for your starred gists: |
| 12 | # [index_of_gist] [url] [file_num] [comment_num] [short description] | 12 | # [index_of_gist] [url] [file_num] [comment_num] [short description] |
| 13 | # fetch, f [star | s] update the local list of your gists, star for your starred gists | 13 | # fetch, f [star | s] Update the local list of your gists, star for your starred gists |
| 14 | # <index_of_gist> [--no-action] show the path of local gist repo and do custom actions | 14 | # <index_of_gist> [--no-action] Show the path of local gist repo and do custom actions |
| 15 | # new, n [-d | --desc <description>] [-p] <files>... create a new gist with files | 15 | # new, n [-d | --desc <description>] [-p] <files>... create a new gist with files |
| 16 | # new, n [-d | --desc <description>] [-p] [-f | --file <file_name>] create a new gist from STDIN | 16 | # new, n [-d | --desc <description>] [-p] [-f | --file <file_name>] create a new gist from STDIN |
| 17 | # detail, d <index_of_gist> show the detail of a gist | 17 | # detail, d <index_of_gist> Show the detail of a gist |
| 18 | # edit, e <index_of_gist> edit a gist's description | 18 | # edit, e <index_of_gist> Edit a gist's description |
| 19 | # delete, D <index_of_gist>... delete a gist | 19 | # delete, D <index_of_gist>... Delete a gist |
| 20 | # clean, C clean removed gists in local | 20 | # clean, C Clean removed gists in local |
| 21 | # config, c [token | user | folder | auto_sync | EDITOR | action [value] ] do configuration | 21 | # config, c [token | user | folder | auto_sync | EDITOR | action [value] ] Do configuration |
| 22 | # user, U <user> get gists from a given Github user | 22 | # user, U <user> Get gists from a given Github user |
| 23 | # grep, g <pattern> grep gists by a given pattern | 23 | # grep, g <pattern> Grep gists by a given pattern |
| 24 | # push, p <pattern> push changes by git (well, better to make commit by youself) | 24 | # push, p <pattern> Push changes by git (well, better to make commit by youself) |
| 25 | # github, G <index_of_gist> Import this gist as a new Github repo | 25 | # github, G <index_of_gist> Import selected gist as a new Github repo |
| 26 | # help, h show this help message | 26 | # help, h Show this help message |
| 27 | # update Update Bash-Snippet Tools | ||
| 27 | # | 28 | # |
| 28 | # Example: | 29 | # Example: |
| 29 | # gist (Show your gists) | 30 | # gist (Show your gists) |
| @@ -81,6 +82,7 @@ http_method() { | |||
| 81 | http -b $METHOD "$@" "$header" $extra2 ;; | 82 | http -b $METHOD "$@" "$header" $extra2 ;; |
| 82 | esac | 83 | esac |
| 83 | } | 84 | } |
| 85 | alias httpGet='http_method GET' | ||
| 84 | 86 | ||
| 85 | # parse JSON from STDIN with string of commands | 87 | # parse JSON from STDIN with string of commands |
| 86 | _process_json() { | 88 | _process_json() { |
| @@ -89,9 +91,52 @@ _process_json() { | |||
| 89 | return "$?" | 91 | return "$?" |
| 90 | } | 92 | } |
| 91 | 93 | ||
| 92 | # Displays version number | 94 | checkInternet() { |
| 93 | version() { | 95 | httpGET github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request |
| 94 | echo "Version $currentVersion" | 96 | } |
| 97 | |||
| 98 | update() | ||
| 99 | { | ||
| 100 | # Author: Alexander Epstein https://github.com/alexanderepstein | ||
| 101 | # Update utility version 2.2.0 | ||
| 102 | # To test the tool enter in the defualt values that are in the examples for each variable | ||
| 103 | repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite | ||
| 104 | githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein | ||
| 105 | nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one | ||
| 106 | latestVersion=$(httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option | ||
| 107 | |||
| 108 | if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]; then | ||
| 109 | echo "Error: update utility has not been configured correctly." >&2 | ||
| 110 | exit 1 | ||
| 111 | elif [[ $latestVersion == "" ]]; then | ||
| 112 | echo "Error: no active internet connection" >&2 | ||
| 113 | exit 1 | ||
| 114 | else | ||
| 115 | if [[ "$latestVersion" != "$currentVersion" ]]; then | ||
| 116 | echo "Version $latestVersion available" | ||
| 117 | echo -n "Do you wish to update $repositoryName [Y/n]: " | ||
| 118 | read -r answer | ||
| 119 | if [[ "$answer" == [Yy] ]]; then | ||
| 120 | cd ~ || { echo 'Update Failed'; exit 1; } | ||
| 121 | if [[ -d ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi | ||
| 122 | echo -n "Downloading latest version of: $repositoryName." | ||
| 123 | git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } & | ||
| 124 | while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done | ||
| 125 | rm -f .BSnippetsHiddenFile | ||
| 126 | echo "Success!" | ||
| 127 | cd $repositoryName || { echo 'Update Failed'; exit 1; } | ||
| 128 | git checkout "v$latestVersion" 2> /dev/null || git checkout "$latestVersion" 2> /dev/null || echo "Couldn't git checkout to stable release, updating to latest commit." | ||
| 129 | chmod a+x install.sh #this might be necessary in your case but wasnt in mine. | ||
| 130 | ./$nameOfInstallFile "update" || exit 1 | ||
| 131 | cd .. | ||
| 132 | rm -r -f $repositoryName || { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; } | ||
| 133 | else | ||
| 134 | exit 1 | ||
| 135 | fi | ||
| 136 | else | ||
| 137 | echo "$repositoryName is already the latest version" | ||
| 138 | fi | ||
| 139 | fi | ||
| 95 | } | 140 | } |
| 96 | 141 | ||
| 97 | # handle configuration cases | 142 | # handle configuration cases |
| @@ -491,7 +536,7 @@ usage() { | |||
| 491 | } | 536 | } |
| 492 | 537 | ||
| 493 | _apply_config "$@" || exit 1 | 538 | _apply_config "$@" || exit 1 |
| 494 | getConfiguredClient | 539 | getConfiguredClient || exit 1 |
| 495 | if [[ $init ]]; then _fetch_gists; exit 0; fi | 540 | if [[ $init ]]; then _fetch_gists; exit 0; fi |
| 496 | case "$1" in | 541 | case "$1" in |
| 497 | "") | 542 | "") |
| @@ -533,6 +578,11 @@ case "$1" in | |||
| 533 | version) | 578 | version) |
| 534 | echo "Version $currentVersion" | 579 | echo "Version $currentVersion" |
| 535 | exit 0 ;; | 580 | exit 0 ;; |
| 581 | update | u) | ||
| 582 | checkInternet || exit 1 | ||
| 583 | update | ||
| 584 | exit 0 | ||
| 585 | ;; | ||
| 536 | help | h) | 586 | help | h) |
| 537 | usage ;; | 587 | usage ;; |
| 538 | *) | 588 | *) |