nelsontky / gh-pages-url-shortener

Minimal URL shortener that can be entirely hosted on GitHub pages.
https://ccb.wtf/1
GNU General Public License v3.0
1.34k stars 506 forks source link

Shorten from your CLI #49

Closed eexit closed 4 years ago

eexit commented 4 years ago

Hey,

I created a small bash script to automate this, based on GitHub CLI:

#!/bin/bash
[[ ! -z "$@" ]] && (gh issue create --repo eexit/s --label "cli" --title "$@" --body "" | sed -e 's/github.com\/eexit\/s\/issues/eexit.github.io\/s/g') || (echo "Forgot URL?"; exit 1)

Screenshot 2020-11-17 at 11 18 35 PM

Note: my repo is https://github.com/eexit/s

nelsontky commented 4 years ago

Great stuff! Added your fork and bash script to README!

rasa commented 3 years ago

Cool hack! Here are a few tweaks to consider:

[[ ! -z "$@" ]] &&

could be simplified to:

(($#)) &&

Also,

sed -e 's/github.com\/eexit\/s\/issues/eexit.github.io\/s/g'

could be simplified to:

sed -e 's|github\.com/eexit/s/issues|eexit.github.io/s|g'

Also Why #!/usr/bin/env bash is superior to #!/bin/bash

So:


#!/usr/bin/env bash
(($#)) && {gh issue create --repo eexit/s --label "cli" --title "$@" --body "" | sed -e 's|github\.com/eexit/s/issues|eexit.github.io/s|g'} || {echo "Forgot URL?"; exit 1}
eexit commented 3 years ago

Thanks for your input @rasa. I'll test this and let you know how better this works.

chadmayfield commented 3 years ago

@eexit & @rasa great ideas! I've done the same thing, but I like interact with the API directly directly using curl rather than installing anything else like gh. That way I can use it anywhere curl is installed (which should be everywhere). Hopefully this will help someone in the future looking for an alternative method;

Assumption: You have created a label in GitHub issues named url.

#!/usr/bin/env bash

if [ $# -ne 1 ]; then
  echo "You must pass a URL to shorten as an argument!"
  exit 1
fi

curl -X POST \
  -u $USERNAME:$TOKEN \
  -d "{\"title\": \"$1\",\"labels\": [\"url\"],\"assignees\": [\"$USERNAME\"]}" \
  https://api.github.com/repos/$USERNAME/$REPO/issues

(Just replace $USERNAME, $TOKEN, & $REPO with your GitHub username, Personal Access Token, and repo name and save to a script and make it executable.)

And finally use it: ./shorten.sh https://google.com/

P.S. @nelsontky great work on the original code!