Skip to content

Commit fd1a875

Browse files
author
Cerem Cem ASLAN
committed
Merge branch 'test-oauth'
2 parents 97da6d1 + 5137e69 commit fd1a875

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ A utility to create gists from command line
1010

1111
lsusb | create-gist.sh Github_user_name
1212

13+
or
14+
15+
lsusb | create-gist.sh your_token
16+
17+
18+
# Creating OAuth token
19+
20+
1. Go to https://github.com/settings/tokens/new and generate a new token with `create gist` permission.
21+
2. Use the token for authentication:
22+
23+
```
24+
lsusb | create-gist.sh your-token-here
25+
```
26+
27+
1328
# Dependencies
1429

1530
* `curl`

create-gist.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ print_usage(){
1717
1818
lsusb | $(basename $0) Github_user_name
1919
20+
or
21+
22+
lsusb | $(basename $0) your_token
23+
2024
USAGE
2125
}
2226

2327
# 0. Your file name
2428
FNAME="${1:-}"
2529
if [[ -f "$FNAME" ]]; then
2630
CONTENT=$(cat "$FNAME")
27-
GITHUB_USERNAME="${2:-}"
31+
CREDENTIAL="${2:-}"
2832
else
2933
CONTENT=$(timeout 2 cat -)
30-
GITHUB_USERNAME="${1-}"
34+
CREDENTIAL="${1-}"
3135
FNAME="stdin"
3236
if [[ "$CONTENT" == "" ]]; then
3337
print_usage
@@ -36,7 +40,7 @@ else
3640
fi
3741

3842
# Github does not permit anonymous uploads since April 2018
39-
if [[ -z $GITHUB_USERNAME ]]; then
43+
if [[ -z $CREDENTIAL ]]; then
4044
print_usage
4145
exit 2
4246
fi
@@ -68,7 +72,15 @@ cat > $tmp_file <<EOF
6872
EOF
6973

7074
# 4. Use curl to make a POST request
71-
OUTPUT=$(curl -u ${GITHUB_USERNAME} -X POST -d @$tmp_file "https://api.github.com/gists")
75+
if [[ ${#CREDENTIAL} -eq 40 ]]; then
76+
echo "Using token authentication."
77+
OUTPUT=$(curl -H "Authorization: token ${CREDENTIAL}" \
78+
-X POST -d @$tmp_file "https://api.github.com/gists")
79+
else
80+
echo "Using username/password authentication."
81+
OUTPUT=$(curl -u ${CREDENTIAL} \
82+
-X POST -d @$tmp_file "https://api.github.com/gists")
83+
fi
7284
uploaded_url=$(echo "$OUTPUT" | grep 'html_url' | grep 'gist')
7385

7486
# 5. cleanup the tmp file

0 commit comments

Comments
 (0)