Created
July 3, 2024 11:14
-
-
Save SOF3/ae2eaac0b6566f92ace3060daee94472 to your computer and use it in GitHub Desktop.
Parse k8s quantities with jq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_quantity: | |
{n: -3, u: -2, m: -1, k: 1, M: 2, G: 3} as $units | | |
if .[-1:] == "i" then | |
{base: 1024, str: .[:-1]} | |
else | |
{base: 1000, str: .} | |
fi | | |
.str[-1:] as $unit | | |
if $units | has($unit) then | |
pow(10; $units[$unit]) * (.str[:-1] | tonumber) | |
elif .base == 1000 then | |
tonumber | |
else | |
error("unknown unit in \(.str\)") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment