#url #command-line #html #url-path #encode #decode #query

app urlmatic

Slice and dice URLs on the command line

6 releases (breaking)

0.5.0 Jun 10, 2022
0.4.0 Jun 9, 2022
0.3.0 Jun 7, 2022
0.2.1 Jun 5, 2022
0.1.0 May 19, 2022

#19 in #url-path

BSD-3-Clause

12KB
273 lines

URL-matic

Perform some useful manipulation of URLs. This thing doesn't do much yet.

Resolve a URL Against a Base

Resolves a relative URL against the provided base URL and prints the result.

$ urlmatic resolve --base 'https://www.example.com/documents/letter.html?length=100' '../index.html?length=200'
> https://www.example.com/index.html?length=200

Trim Path Components from a URL

Removes the specified number of components from the end of a URL's path and prints the result.

$ urlmatic trim --count 2 'https://www.example.com/documents/letter.html?length=100'
> https://www.example.com/?length=100

Rewrite a URL by Replacing Components

Replace specific components in a URL and print the result.

$ urlmatic rewrite \
    --username admin \
    --host another.com \
    --path /cgi-bin/q \
    --query 'offset=0&length=100' \
    --fragment 'anchor-name' \
    https://example.com/query
> https://admin@another.com/cgi-bin/q?offset=0&length=100#anchor-name

Encode a Query String as application/x-www-form-urlencoded data

URL-encode form data form/query data.

$ urlmatic encode -k yep -v 👍 -k nope -v 👎
> yep=%F0%9F%91%8D&nope=%F0%9F%91%8E
$ urlmatic encode yep=👍 nope=👎
> nope=%F0%9F%91%8E&yep=%F0%9F%91%8D

Decode a Query String as application/x-www-form-urlencoded data

URL-decode form data and extract values.

$ urlmatic decode 'yep=%F0%9F%91%8D&nope=%F0%9F%91%8E'
>  yep: 👍
> nope: 👎
$ urlmatic decode --select yep 'yep=%F0%9F%91%8D&nope=%F0%9F%91%8E'
> 👍
$ urlmatic decode --select nope,yep 'yep=%F0%9F%91%8D&nope=%F0%9F%91%8E'
> 👍
> 👎

Put it All Together

Compose and modify a URL using a few commands.

$ echo 'https://example.com/path/to/query' |
    urlmatic rewrite --host another.com --query $(urlmatic encode -k yep -v 👍) |
    urlmatic trim --count 2
> https://another.com/path?yep=%F0%9F%91%8D

Dependencies

~4.5MB
~108K SLoC