Skip to content

Instantly share code, notes, and snippets.

@pwm
Created November 4, 2020 20:28
Show Gist options
  • Select an option

  • Save pwm/7512d8b1c7c66214159b818612de9fc1 to your computer and use it in GitHub Desktop.

Select an option

Save pwm/7512d8b1c7c66214159b818612de9fc1 to your computer and use it in GitHub Desktop.

Revisions

  1. pwm created this gist Nov 4, 2020.
    28 changes: 28 additions & 0 deletions gisto.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/usr/bin/env bash

    set -euo pipefail

    cd "$(dirname "${BASH_SOURCE[0]}")"

    if [ "$#" -ne 1 ]; then
    echo "Usage: gisto.sh <user>"
    exit 1
    fi
    #TODO: pagination if more than 100 gists
    query_url="users/$1/gists?per_page=100&page=1"

    needs() {
    [[ "$(command -v "$1")" ]] || { echo "$1 is not installed" 1>&2; exit 1; }
    }
    needs git
    needs hub
    needs jq

    gists=$(hub api "$query_url" | jq -rc '.[] | {"id":.id, "url":.git_pull_url, "desc":(.description | tostring | sub("null"; "") | gsub("[/ ]"; "_") | ascii_downcase)}')

    for gist in $gists; do
    gist_id=$(echo "$gist" | jq -r '.id')
    gist_url=$(echo "$gist" | jq -r '.url')
    gist_desc=$(echo "$gist" | jq -r '.desc')
    git clone "$gist_url" "${gist_desc:-$gist_id}" || true
    done