Skip to content

Instantly share code, notes, and snippets.

@robindiddams
Created March 13, 2024 16:50
Show Gist options
  • Save robindiddams/6c019f3b5408c2d5d3564ae61800c28b to your computer and use it in GitHub Desktop.
Save robindiddams/6c019f3b5408c2d5d3564ae61800c28b to your computer and use it in GitHub Desktop.
# brew install gum
sdmreq () {
output=$(sdm access catalog)
array=()
if [[ $output =~ ^ID.*Name.*Healthy.*Type.*Authentication.*Access.*Tags ]]
then
while IFS= read -r line
do
local resource_id=$(echo "$line" | awk '{print $1}')
local resource_name=$(echo "$line" | awk '{print $2}')
array+=("$resource_name $resource_id")
done <<< "$(echo "$output" | tail -n +2)"
else
echo "Error: Unexpected output format from 'sdm access catalog' command." >&2
return 1
fi
choice=`gum choose "${array[@]}"`
if [ -z "$choice" ]
then
return 1
fi
resourceId=$(echo "$choice" | awk '{print $2}')
reason=$(gum input --value "I need access so that I can ")
if [ -z "$reason" ]
then
return 1
fi
echo $resourceId
if [ -z "$resourceId" ]
then
echo "No resource selected"
return 1
fi
sdm access to $resourceId --duration 2h --reason "$reason"
}
@yevhenii-horbenko
Copy link

Here are some suggestions for improving it:

  1. Error Handling: Add more robust error handling to catch unexpected errors or failures during the execution of commands.
  2. Comments: Add comments to explain the purpose of each section or function in the script.
  3. Input Validation: Validate user input to ensure it meets expected criteria.
  4. Consistency: Maintain consistent indentation and coding style throughout the script for better readability.
  5. Use Variables Wisely: Instead of calling commands multiple times (e.g., echo "$line" | awk '{print $1}'), store the result in a variable and reuse it.
  6. Avoid Unnecessary Commands: Minimize the use of unnecessary commands or piping by directly manipulating variables.

Here's an improved version of your script incorporating these suggestions:

#!/bin/bash`

# Function to request access to a resource
sdmreq() {
    local output
    output=$(sdm access catalog)

    local array=()

    if [[ $output =~ ^ID.*Name.*Healthy.*Type.*Authentication.*Access.*Tags ]]; then
        while IFS= read -r line; do
            local resource_id=$(awk '{print $1}' <<< "$line")
            local resource_name=$(awk '{print $2}' <<< "$line")
            array+=("$resource_name $resource_id")
        done <<< "$(tail -n +2 <<< "$output")"
    else
        echo "Error: Unexpected output format from 'sdm access catalog' command." >&2
        return 1
    fi

    local choice
    choice=$(gum choose "${array[@]}")

    if [ -z "$choice" ]; then
        return 1
    fi

    local resource_id
    resource_id=$(awk '{print $2}' <<< "$choice")

    local reason
    reason=$(gum input --value "I need access so that I can ")

    if [ -z "$reason" ]; then
        return 1
    fi

    if [ -z "$resource_id" ]; then
        echo "No resource selected"
        return 1
    fi

    sdm access to "$resource_id" --duration 2h --reason "$reason"
}

Please replace the comments with actual usage scenarios or integrate it into your script where appropriate. Also, make sure to test thoroughly after making changes.

@yevhenii-horbenko
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment