shlinkio / shlink

The definitive self-hosted URL shortener
https://shlink.io
MIT License
3.09k stars 251 forks source link

Consider building and packaging the Shlink CLI as a static binary #1527

Open onedr0p opened 2 years ago

onedr0p commented 2 years ago

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 or pacman -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.

onedr0p commented 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.

acelaya commented 2 years ago

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.

onedr0p commented 2 years ago

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 😄

hashworks commented 1 year ago

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