Open onedr0p opened 2 years ago
For a bit more clarity, I am trying to script the creation of short URLs on the CLI. My shlink is running on another server, is it true that the current CLI needs to ran where the app is deployed? If true then it seems the only way to interact with shlink on the CLI is via the REST API with curl or something.
Unfortunately yes. Shlink's CLI, the way it is created right now, does not interact with Shlink's API, but its internals, so it cannot be extracted as it is into a separated tool.
Also, there's no way to statically build a binary with the technologies currently used for Shlink.
If you are an expert on go/rust or similar, it would be amazing to have such tool. I would be able to support on the API integration as much as needed.
It would be nice to have such a project under the shlinkio org even if you are the maintainer or not. I would love to contribute if I had free time. I'll add this to the long list of projects on my TODO 😄
If you just need a CLI tool to create, list and delete short URLs, this will do just fine:
SHLINK_HOST=https://example.com
SHLINK_API_KEY=foobar
shlink () {
BASE_URL="${SHLINK_HOST}/rest/v3/short-urls"
if [[ "$1" == "delete" ]] && [[ -n "$2" ]]
then
curl -s -X 'DELETE' -H 'accept: application/problem+json' -H "X-Api-Key: ${SHLINK_API_KEY}" \
"${BASE_URL}/${2}" | \
jq
elif [[ -z "$1" ]]
then
curl -s -X 'GET' -H 'accept: application/json' -H "X-Api-Key: ${SHLINK_API_KEY}" \
"${BASE_URL}?orderBy=dateCreated-DESC" | \
jq -r '.shortUrls.data[] | .shortUrl + " -> " + .longUrl'
elif [[ "$1" != "help" ]] && [[ "$1" != "--help" ]]
then
curl -s -H "Content-Type: application/json" \
"${BASE_URL}/shorten?apiKey=${SHLINK_API_KEY}&format=txt&longUrl=${1}"
echo ""
else
echo "shlink"
echo "shlink <target-url>"
echo "shlink delete <uuid>"
return 2
fi
}
Just put it in your .bashrc
or whatever. Needs jq
and curl
.
% shlink https://example.com/test
https://example.com/NuyIj
% shlink https://example.com/test2
https://example.com/lPKI5
% shlink
https://example.com/lPKI5 -> https://example.com/test2
https://example.com/NuyIj -> https://example.com/test
% shlink delete NuyIj
% shlink
https://example.com/lPKI5 -> https://example.com/test2
Summary
Hi 👋🏼
It would be great if the shlink CLI (https://shlink.io/documentation/command-line-interface/entry-point/) could be written as such that it would be compiled and packaged as a static binary.
The main benefits would be for the CLI to be packaged for various OS distributions. Imagine being able to run
brew install shlink-cli
orpacman -S shlink-cli
or whatever.I don't know if this would be possible in its current form and maybe a Go / Rust based CLI tool to interact with the shlink API might be better suited for static binaries.
Anyways, thanks for hearing me out.