Nushell `assume` script?
Hello 👋🏻
Huge fan of granted, has massively simplified my workflow, thanks for the great tool!
I'm a nushell user and like some others, ran into the "your shell is not supported" error because there is no nushell assume alias auto-setup.
There is this discussion where one has been implemented. It didn't actually work for me with a straight copy paste but I made some local tweaks and it works great now.
Click me to see the patched script 👇🏻
# nushell support for https://github.com/common-fate/granted
# Save this file to `granted.nu` in your nushell config dir, then add `use granted.nu *` in your config.nu
# assume - https://granted.dev
export def --env --wrapped assume [...args: string] {
const var_names = [
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_PROFILE",
"AWS_REGION",
"AWS_SESSION_EXPIRATION",
"GRANTED_SSO",
"GRANTED_SSO_START_URL",
"GRANTED_SSO_ROLE_NAME",
"GRANTED_SSO_REGION",
"GRANTED_SSO_ACCOUNT_ID"
]
# Run assumego and collect its output
let output = with-env {GRANTED_ALIAS_CONFIGURED: "true"} {
assumego ...$args
}
let granted_output = $output | lines
let granted_status = $env.LAST_EXIT_CODE
# First line is the command
let command = $granted_output | get -i 0 | default "" | str trim | split row " "
let flag = $command | get 0
# Collect environment variables to set
let envvars = do {
let values = $command | skip 1
let vars = $var_names |
zip $values |
filter {|x| $x.1 != "None"} |
reduce --fold {} {|it, acc| $acc | insert $it.0 $it.1}
$vars
}
match $flag {
"GrantedOutput" => {
# The rest of the output (if any)
let to_display = $granted_output | skip 1
$to_display | str join "\n" | print
},
"GrantedAssume" => {
for v in $var_names {
hide-env -i $v
}
load-env $envvars
},
"GrantedDesume" => {
for v in $var_names {
hide-env -i $v
}
},
"GrantedExec" => {
let num_vars = ($var_names | length)
let cmd = $command | range ($num_vars + 1).. | str join " "
with-env $envvars { nu -c $"($cmd)"}
}
_ => {
# most likely no output, so nothing to do
}
}
if $granted_status != 0 {
error make -u {msg: $"command failed with code ($granted_status)"}
}
}
I was wondering if we could feasibly include this in the list of default supported shells? I'm not exactly familiar with how granted auto configures the shells but I'm guessing the process for nushell support would be something like:
- Save the above script to
$nu.default-config-dir/granted.nu - In
$nu.default-config-dir/config.nuadd the following to the bottom of the file:
# Use common fate granted with nu
use granted.nu *
I'm very familiar with Go so would be happy to see if I can add this in a PR. Is there any additional granted specific setup to do other than this "installation"? :)
Maybe even just adding a section on how to do this in nushell to the shell alias docs (https://docs.commonfate.io/granted/internals/shell-alias) is enough?
Thanks again!
Thanks for this script!
thinking this script should be pinned somewhere as part of the nushell install method
It would be awesome to get this scripted merged into granted, e.g. next to the /bin/assume and /bin/assume.fish scripts that are installed by default.