Skip to content

Commit 80607ba

Browse files
author
Cerem Cem ASLAN
committed
resolves #7
1 parent 28b2abd commit 80607ba

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,34 @@ A utility to create gists from command line
44

55
usage:
66

7-
$(basename $0) /path/to/file Github_user_name
7+
create-gist.sh /path/to/file your_token
88

9-
or
9+
or
10+
11+
lsusb | create-gist.sh your_token
1012

11-
lsusb | $(basename $0) Github_user_name
13+
If no token is passed and /home/ceremcem/.create-gist.cfg is found,
14+
contents of config file is used as the token, so
15+
the usage becomes:
16+
17+
lsusb | create-gist.sh
1218

1319
or
1420

15-
lsusb | $(basename $0) your_token
21+
create-gist.sh /path/to/file
1622

17-
If no credential is passed and $config_file is found,
18-
contents of config file is used.
1923

2024
# Creating OAuth token
2125

22-
1. Go to https://github.com/settings/tokens/new and generate a new token with `create gist` permission.
23-
2. Use the token for authentication:
26+
1. Go to https://github.com/settings/tokens/new
27+
2. Generate a new token with `gist` (`create gist`) permission.
28+
3. Use the token for authentication:
29+
30+
31+
lsusb | create-gist.sh your-token-here
2432

25-
```
26-
lsusb | create-gist.sh your-token-here
27-
```
2833

34+
4. Optionally save your token to `~/.create-gist.cfg` file.
2935

3036
# Dependencies
3137

create-gist.sh

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ print_usage(){
1313
1414
usage:
1515
16-
$(basename $0) /path/to/file Github_user_name
17-
18-
or
19-
20-
lsusb | $(basename $0) Github_user_name
16+
$(basename $0) /path/to/file your_token
2117
2218
or
2319
2420
lsusb | $(basename $0) your_token
2521
26-
If no credential is passed and $config_file is found,
27-
contents of config file is used.
22+
If no token is passed and $config_file is found,
23+
contents of config file is used as the token, so
24+
the usage becomes:
25+
26+
lsusb | $(basename $0)
27+
28+
or
29+
30+
$(basename $0) /path/to/file
2831
2932
USAGE
3033
}
@@ -33,24 +36,28 @@ USAGE
3336
FNAME="${1:-}"
3437
if [[ -f "$FNAME" ]]; then
3538
CONTENT=$(cat "$FNAME")
36-
CREDENTIAL="${2:-}"
39+
TOKEN="${2:-}"
3740
else
3841
CONTENT=$(timeout 2 cat -)
39-
CREDENTIAL="${1-}"
42+
TOKEN="${1-}"
4043
FNAME="stdin"
4144
if [[ "$CONTENT" == "" ]]; then
45+
echo "ERROR: Content is empty"
46+
echo
4247
print_usage
4348
exit 2
4449
fi
4550
fi
4651

47-
if [[ -z $CREDENTIAL ]] && [[ -f $config_file ]]; then
52+
if [[ -z $TOKEN ]] && [[ -f $config_file ]]; then
4853
echo "Using $config_file for credentials"
49-
CREDENTIAL=$(cat $config_file)
54+
TOKEN=$(cat $config_file)
5055
fi
5156

5257
# Github does not permit anonymous uploads since April 2018
53-
if [[ -z $CREDENTIAL ]]; then
58+
if [[ -z $TOKEN ]]; then
59+
echo "ERROR: Token is missing."
60+
echo
5461
print_usage
5562
exit 2
5663
fi
@@ -82,15 +89,9 @@ cat > $tmp_file <<EOF
8289
EOF
8390

8491
# 4. Use curl to make a POST request
85-
if [[ ${#CREDENTIAL} -eq 40 ]]; then
86-
echo "Using token authentication."
87-
OUTPUT=$(curl -H "Authorization: token ${CREDENTIAL}" \
88-
-X POST -d @$tmp_file "https://api.github.com/gists")
89-
else
90-
echo "Using username/password authentication."
91-
OUTPUT=$(curl -u ${CREDENTIAL} \
92-
-X POST -d @$tmp_file "https://api.github.com/gists")
93-
fi
92+
OUTPUT=$(curl -H "Authorization: token ${TOKEN}" \
93+
-X POST -d @$tmp_file "https://api.github.com/gists")
94+
9495
uploaded_url=$(echo "$OUTPUT" | grep 'html_url' | grep 'gist')
9596

9697
# 5. cleanup the tmp file

0 commit comments

Comments
 (0)