Skip to content

Instantly share code, notes, and snippets.

@tst32
Created August 31, 2021 12:21
Show Gist options
  • Save tst32/b02ee3f6532c18a3f6b9ee8500d4de51 to your computer and use it in GitHub Desktop.
Save tst32/b02ee3f6532c18a3f6b9ee8500d4de51 to your computer and use it in GitHub Desktop.
./allgistsclone.sh <repo owner name> -- clone all (visible to your scope public and secret gists)
#!/usr/bin/env bash
args=("$@")
if [ $# -eq 0 ]; then
echo "No arguments supplied, allgistclone.sh is a bash script to clone all available gists given github owner name"
echo "call wuth argument <ownername> or -h|--help option to show usage"
echo "comment to @tst32"
exit 0
fi
repowner=${args[0]}
if [[ $repowner == *"--help"* || $repowner == *"-h"* ]]; then
echo 'usage: ./allgistsclone.sh <repo owner name> -- clone all (visible to your scope public and secret gists)'
echo 'as rest of you got rep of clonned gistlistdesc.txt id + desc (if avail)'
exit 0
fi
if [ -e "gistlist.txt" ]; then
rm -f "gistlist.txt"
fi
command -v gh >/dev/null 2>&1 || { echo >&2 "require gh tool (https://cli.github.com/) , but it's not installed. Aborting."; exit 1; }
# repstr='"'
gh api -X GET /users/$repowner/gists?per_page=200 | jq '.[] | { id, description }' |jq '.description+" - "+.id' >gistlistdesc.txt
gh api -X GET /users/$repowner/gists?per_page=200 | jq '.[] | { id }' |jq '.id' | sed 's/"//g' >gistlist.txt
while read gist; do gh gist clone "$gist"; done <gistlist.txt
rm -f gistlist.txt
# sanitize() {
# local s="${1?need a string}" # receive input in first argument
# s="${s//[^[:alnum:]]/-}" # replace all non-alnum characters to -
# s="${s//+(-)/-}" # convert multiple - to single -
# s="${s/#-}" # remove - from start
# s="${s/%-}" # remove - from end
# echo "${s,,}" # convert to lowercase
# }
# set -e
# unset x
# shopt -s lastpipe
# set +m
@tst32
Copy link
Author

tst32 commented Feb 27, 2022

[-] option to sync if there are changes either locally or remotely
ok, one side sync is possible out of box for repos (not for gists) see https://cli.github.com/manual/gh_repo_sync so a halfway possible +for local to remote it is more complicated solution. will try for new version.
[ ] add new gists if added remotely.
it would take a lot of time to create snapshot and compare it to remotes. so complicated solution this way. its time is equal as to delete old and redownload
[ ] create alias to name of gist in same directory.
Good option for clone single gist or repo but how to know the names for batch? possible option is to take gist desc not often available or create by timedata stamp?
[ ] option to delete local gist if deleted remotely. the same task as add new gists if added remotely

to operate write/delete for remote repo user nedeed to have both tokens for git and gist, without these only public repos/gists are avail
check it:

gh auth status
github.com
  ✓ Logged in to github.com as tst32 (/home/user/.config/gh/hosts.yml)
  ✓ Git operations for github.com configured to use https protocol.
  ✓ Token: *******************
  
gist.github.com
  ✓ Logged in to gist.github.com as tst32 (/home/user/.config/gh/hosts.yml)
  ✓ Git operations for gist.github.com configured to use https protocol.
  ✓ Token: *******************

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment