36 lines
1.3 KiB
Plaintext
36 lines
1.3 KiB
Plaintext
|
#!/usr/bin/env nu
|
||
|
|
||
|
# Ensure that the input starts with a slash
|
||
|
def ensure_leading_slash [] {
|
||
|
if ($in | str starts-with "/") { echo $in } else { echo $"/($in)" }
|
||
|
}
|
||
|
# Apply the given `func` if `ok` is true and just pass the input if not
|
||
|
def maybe_apply [ ok: bool func: closure ] {
|
||
|
if $ok { do $func $in } else { $in }
|
||
|
}
|
||
|
# Main function
|
||
|
def main [ path: string ...select: cell-path --token (-t): string --explore (-x) --host: string = "http://localhost:8000" --raw (-r) ] {
|
||
|
# Extract token from environment if not passed explicitely
|
||
|
let token = if $token != null {
|
||
|
echo $token
|
||
|
} else if "TOKEN_L2" in $env {
|
||
|
echo $env.TOKEN_L2
|
||
|
} else {
|
||
|
error make { msg: "TOKEN_L2 environment variable is not set. Provide a token with `--token TOKEN`" label: { span: (metadata $token).span, text: "--token TOKEN not provided" }}
|
||
|
}
|
||
|
# Construct the url
|
||
|
let url = $"($host)/api($path | ensure_leading_slash)"
|
||
|
# Fetch from the api!
|
||
|
http get $url --allow-errors --full --headers [
|
||
|
"Accept" "application/json"
|
||
|
"Authorization" $"Bearer ($token)"
|
||
|
]
|
||
|
| if $in.status == 200 {
|
||
|
$in.body.data | maybe_apply ($select != null) { select ...$select }
|
||
|
} else {
|
||
|
$in.body.message
|
||
|
}
|
||
|
| maybe_apply $explore { explore }
|
||
|
| maybe_apply $raw {to json}
|
||
|
}
|