From c5353850b536e3aa0a5f8626e5cf7542cb890839 Mon Sep 17 00:00:00 2001 From: reinno Date: Wed, 11 Sep 2024 00:08:49 +0100 Subject: [PATCH] fix(aws): Fix select-aws-profile script functionality and style - Rename main function to 'main' and add --env flag for environment modification - Improve profile selection logic using reduce and string interpolation - Fix region handling to properly check for null values --- modules/aws/select-aws-profile.nu | 62 +++++++++++++++---------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/modules/aws/select-aws-profile.nu b/modules/aws/select-aws-profile.nu index 860273f25..06fdce400 100644 --- a/modules/aws/select-aws-profile.nu +++ b/modules/aws/select-aws-profile.nu @@ -1,40 +1,40 @@ # This alias lets you choose your aws environment variables with ease. # -# Dependencies -# * fzf +# Dependencies: +# * fzf # -# Installation -# 1. store in ~/.config/nushell/select-aws-profile.nu -# 2. add to your config.nu: `use ~/.config/nushell/select-aws-profile.nu *` +# Installation: +# 1. store in ~/.config/nushell/select-aws-profile.nu +# 2. add to your config.nu: `use ~/.config/nushell/select-aws-profile.nu *` # -# Usage -# select-aws-profile -export def select-aws-profile [] { - hide AWS_REGION; +# Usage: +# select-aws-profile +export def --env main [] { + hide AWS_REGION - (do { - let creds = (open ($env.HOME + "/.aws/credentials") | from toml) - let selectedProfile = (for it in ($creds | transpose name creds) { - echo $it.name - }) - - selectedProfile = selectedProfile | str join "\n" | fzf | str trim + do { + let creds = open ($env.HOME + "/.aws/credentials") | from toml + let selected_profile = $creds + | transpose name creds + | get name + | str join "\n" + | fzf - if $selectedProfile != "" { - let out = { - AWS_PROFILE: $selectedProfile, - AWS_ACCESS_KEY_ID: ($creds | get $selectedProfile | get "aws_access_key_id"), - AWS_SECRET_ACCESS_KEY: ($creds | get $selectedProfile | get "aws_secret_access_key"), - } - - let region = ($creds | get $selectedProfile | get -i "region") - if $region != "" { - $out | insert "AWS_REGION" $region - } else { - $out - } + if $selected_profile != "" { + let out = { + AWS_PROFILE: $selected_profile, + AWS_ACCESS_KEY_ID: ($creds | get $selected_profile | get "aws_access_key_id"), + AWS_SECRET_ACCESS_KEY: ($creds | get $selected_profile | get "aws_secret_access_key"), } - } | load-env); + + let region = ($creds | get $selected_profile | get -i "region") + if $region != null { + $out | insert AWS_REGION $region + } else { + $out + } + } + } | load-env { AWS_PROFILE: $env.AWS_PROFILE, @@ -42,4 +42,4 @@ export def select-aws-profile [] { AWS_SECRET_ACCESS_KEY: $env.AWS_SECRET_ACCESS_KEY, AWS_REGION: $env.AWS_REGION } -} \ No newline at end of file +}