From 0078e7e9e1e1ab46833cb108df11dd4f4aa8c4b9 Mon Sep 17 00:00:00 2001 From: typebrook Date: Wed, 15 Jan 2020 18:39:36 +0800 Subject: update --- gist | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/gist b/gist index 80996b1..9a869f9 100755 --- a/gist +++ b/gist @@ -1,12 +1,12 @@ #!/usr/bin/env bash # -# Author: Hsieh Chin Fan +# Author: Hsieh Chin Fan (typebrook) # License: MIT # https://gist.github.com/typebrook/b0d2e7e67aa50298fdf8111ae7466b56 # # # This script host your gists as local Github repo -# It works with jq and curl, which are easy to get in most cases +# It works under GNU with jq and curl, both are easy to get in most cases # # Use the following commands to manage your gists: # @@ -38,7 +38,11 @@ # * clean removed gists in local # gist [clean | C] +# * show this help message +# gist [help | h] + # define your environmemnts here +# TODO support auth prompt #------------------- github_api_token=$(cat $SETTING_DIR/tokens/github) user=typebrook @@ -59,14 +63,14 @@ _show_list() { } # get the list of gists +# TODO support secret gist _update() { curl -s -H "$auth_header" $github_api/users/$user/gists |\ - tee jojo |\ jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ tac | nl |\ - while read line_num link blobs file_num comment_num description; do - blob_code=$(echo $blobs | jq -r '.[]' | sed -r 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') - echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -80) | tr -d '"' + while read line_num link file_url_array file_num comment_num description; do + blob_code=$(echo $file_url_array | jq -r '.[]' | sed -r 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') + echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -70) | tr -d '"' done > $index && \ _show_list } @@ -88,6 +92,7 @@ _sync_repos() { [[ $(git rev-parse origin/master) == $(git rev-parse master) ]] && \ git pull done + echo Everything is fine! } _gist_id() { @@ -100,7 +105,7 @@ _goto_gist() { echo Not a valid gist number: $1 echo Use the number in the first column instead: echo - show_list + _show_list return 0 fi @@ -119,9 +124,12 @@ _delete_gist() { _clean_repos() { comm -23 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ - xargs -I{} rm -rf $folder/{} + while read dir; do + mv $folder/$dir /tmp && echo move $folder/$dir to /tmp + done } +# TODO star count _show_detail() { GIST_ID=$(_gist_id $1) curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ @@ -132,7 +140,6 @@ _show_detail() { } # create a new gist with files -# TODO support folder of files _create_gist() { echo -n 'description: ' read DESC @@ -142,7 +149,7 @@ _create_gist() { FILE=$(basename $file) jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file done |\ - jq -s --arg DESC "$DESC" '{ + jq --slurp --arg DESC "$DESC" '{ public: true, files: add, description: ($DESC) @@ -160,6 +167,10 @@ _edit_gist() { _update } +_help_message() { + sed -r -n ' /^$/ q; 8,$ s/^#//p' $0 +} + case "$1" in "") _show_list @@ -186,6 +197,9 @@ case "$1" in clean | C) _clean_repos ;; + help | h) + _help_message + ;; *) _goto_gist "$1" ;; -- cgit v1.2.3-70-g09d2