Skip to content

Instantly share code, notes, and snippets.

@MarkWalters-dev
Last active August 28, 2023 18:32
Show Gist options
  • Select an option

  • Save MarkWalters-dev/992616d92e2cfca8b8e999490f16c1fc to your computer and use it in GitHub Desktop.

Select an option

Save MarkWalters-dev/992616d92e2cfca8b8e999490f16c1fc to your computer and use it in GitHub Desktop.

Revisions

  1. MarkWalters-dev revised this gist Aug 28, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ghgist
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ l=$(grep "^#${gist_icon}" $f)
    gist_id=${gist_id% *}
    [ -n "$gist_id" ] || crash "Could not extract gist_id"
    [ -n "$gist_filename" ] || crash "Could not extract gist_id"
    echo existing gist
    echo Updating gist
    gh gist edit $gist_id -f $gist_filename $f || crash "gh update error"
    } || {
    shift
  2. MarkWalters-dev revised this gist Aug 28, 2023. 1 changed file with 24 additions and 10 deletions.
    34 changes: 24 additions & 10 deletions ghgist
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,21 @@
    #!/usr/bin/env bash
    #🌩️ gist 992616d92e2cfca8b8e999490f16c1fc ghgist
    f=$1

    # Uses gh to create and update gists


    # TODO:
    # If .gist used then setup symlinks
    # Need method to clone without using a .gist folder
    # Need method to gh gist clone without using a .gist folder
    # look for short description in file and use it
    # if short description is provided then insert in file
    # Short description always directly after gist id comment and an empty line
    # Complex gists might need a readme.
    # If .gist not used then .filename.readme.md or maybe .filename.md
    # actual gist filename of readme would be README.md
    # if used could also look for short description in readme
    # this might be useful if --help is used. You could mdcat the readme


    crash() {
    local err=$?
    @@ -17,15 +28,17 @@ command_exists() {
    return $?
    }

    command_exists gh || crash "gh not installed"
    [ -e "$HOME/.config/gh/hosts.yml" ] || crash "gh not configured\nRun gh auth login"
    f=$1

    [ -f "$f" ] || { echo "Usage: $(basename ${BASH_SOURCE[0]}) FILENAME [DESCRIPTION IF NEW GIST]"; exit; }
    l=$(grep "^#🌩️" $f)
    gist_id=${l#*🌩️ gist }
    gist_filename=${gist_id#* } # Does not support filenames with spaces
    gist_id=${gist_id% *}
    command_exists gh || crash "gh not installed"
    [ -e "$HOME/.config/gh/hosts.yml" ] || crash "gh not configured\nRun gh auth login"
    gist_icon="🌩️" # Unlikely I will find a line beginning with # and this symbol
    l=$(grep "^#${gist_icon}" $f)
    [ -n "$l" ] && {
    gist_id=${l#*${gist_icon} gist }
    gist_filename=${gist_id#* } # Does not support filenames with spaces
    gist_id=${gist_id% *}
    [ -n "$gist_id" ] || crash "Could not extract gist_id"
    [ -n "$gist_filename" ] || crash "Could not extract gist_id"
    echo existing gist
    @@ -35,14 +48,15 @@ gist_id=${gist_id% *}
    description="$*"
    [ -n "$description" ] || { echo "New gist requires a description"; exit; }
    gist_filename=${f##*/} # Same as using basename
    [ -n "$gist_filename" ] || crash "Error getting gist_filename"
    gist_id=$(gh gist create --public -d "$description" -f $f $f) || crash "gh create error"
    gist_id=${gist_id##*/}

    [ -n "$gist_id" ] || { gh gist list; crash "Error getting gist_id\nDo you see anything new?"; }
    # nix-script with nix-shell can use multiple shebang lines.
    last_shebang=$(grep -n "^#!" $f | tail -1 | cut -f1 -d:)
    [ -z "$last_shebang" ] && last_shebang=0
    last_shebang=$(( last_shebang + 1 ))
    sed -i "${last_shebang}i\#🌩️ gist $gist_id $gist_filename" $f
    sed -i "${last_shebang}i\#${gist_icon} gist $gist_id $gist_filename" $f || crash "sed error"
    gh gist edit $gist_id -f $gist_filename $f || crash "gh update error"
    }

  3. MarkWalters-dev revised this gist Aug 28, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ghgist
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #!/usr/bin/env bash
    #🌩️ gist 992616d92e2cfca8b8e999490f16c1fc ghgist
    f=$1

    # TODO:
  4. MarkWalters-dev created this gist Aug 28, 2023.
    57 changes: 57 additions & 0 deletions ghgist
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    #!/usr/bin/env bash
    f=$1

    # TODO:
    # If .gist used then setup symlinks
    # Need method to clone without using a .gist folder

    crash() {
    local err=$?
    printf "%s\nCrashed at: %s:%s\n" "$*" "${FUNCNAME[1]}" "${BASH_LINENO[0]}"
    exit $err
    }

    command_exists() {
    command -v $1 > /dev/null 2>&1
    return $?
    }

    command_exists gh || crash "gh not installed"
    [ -e "$HOME/.config/gh/hosts.yml" ] || crash "gh not configured\nRun gh auth login"

    [ -f "$f" ] || { echo "Usage: $(basename ${BASH_SOURCE[0]}) FILENAME [DESCRIPTION IF NEW GIST]"; exit; }
    l=$(grep "^#🌩️" $f)
    gist_id=${l#*🌩️ gist }
    gist_filename=${gist_id#* } # Does not support filenames with spaces
    gist_id=${gist_id% *}
    [ -n "$l" ] && {
    [ -n "$gist_id" ] || crash "Could not extract gist_id"
    [ -n "$gist_filename" ] || crash "Could not extract gist_id"
    echo existing gist
    gh gist edit $gist_id -f $gist_filename $f || crash "gh update error"
    } || {
    shift
    description="$*"
    [ -n "$description" ] || { echo "New gist requires a description"; exit; }
    gist_filename=${f##*/} # Same as using basename
    gist_id=$(gh gist create --public -d "$description" -f $f $f) || crash "gh create error"
    gist_id=${gist_id##*/}

    # nix-script with nix-shell can use multiple shebang lines.
    last_shebang=$(grep -n "^#!" $f | tail -1 | cut -f1 -d:)
    [ -z "$last_shebang" ] && last_shebang=0
    last_shebang=$(( last_shebang + 1 ))
    sed -i "${last_shebang}i\#🌩️ gist $gist_id $gist_filename" $f
    gh gist edit $gist_id -f $gist_filename $f || crash "gh update error"
    }

    alternatemethod() {
    # Would require a dedicated .gist folder and symlinks to bin folder
    git init
    git remote add gist "$(gh gist create README.md)"
    git fetch gist
    git checkout -f master
    git add . && git commit -m "add the rest of the files"
    git push
    }