forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshlink-create-short-url.template.sh
executable file
·64 lines (54 loc) · 1.83 KB
/
shlink-create-short-url.template.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Dependency: This script requires `jq` cli installed: https://stedolan.github.io/jq/
# Install via homebrew: `brew install jq`
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Create Short URL
# @raycast.mode compact
# Optional parameters:
# @raycast.icon ./images/shlink.png
# @raycast.packageName Shlink
# @raycast.argument2 { "type": "text", "placeholder": "URL", "optional": true, "percentEncoded": false }
# Documentation:
# @raycast.author Eliot Hertenstein
# @raycast.description Create a short URL using Shlink
# @raycast.authorURL https://github.com/eIiot
## * CONFIGURE - REPLACE THESE VALUES * ##
BASEURL="<-- BASE URL -->" # Base URL for the short url. Remove any trailing slash or http(s)://
PROTOCOL="https://$BASEURL" # Use http or https
APIKEY="<-- API KEY -->" # API key for the short url. See https://shlink.io/documentation/api-docs/authentication/
TAG="raycast" # Tag for the short url. Set to "" to disable
## * END CONFIG * ##
if ! command -v jq &> /dev/null; then
echo "jq is required (https://stedolan.github.io/jq/).";
exit 1;
fi
if [ -z "$1" ]; then
URL=$(pbpaste)
else
URL=$1
fi
if [[ $URL =~ ^https?://[a-zA-Z0-9./?=_-]*$ ]]; then
SHORT_URL=$(curl --location --silent --request POST ''"$PROTOCOL"'/rest/v1/short-urls' \
--header 'Content-Type: application/json' \
--header 'X-Api-Key: '"$APIKEY"'' \
--data-raw '{
"longUrl": "'"$URL"'",
"validateUrl": false,
"tags": [
"'$TAG'"
],
"findIfExists": true,
"domain": "'"$BASEURL"'"
}' | jq -r '.shortUrl')
if [ -n "$SHORT_URL" ]; then
echo "$SHORT_URL" | pbcopy
echo "Copied $SHORT_URL to your clipboard"
else
echo "Error: Could not create short URL"
exit 1
fi
else
echo "Invalid URL \"$URL\""
exit 1
fi