Skip to content

Commit

Permalink
Merge pull request #1 from braun-daniel/feature/improv
Browse files Browse the repository at this point in the history
Bugfix: could not parse whitespaces in sub; Bad time parsing of expiration; Removed unused stuff;
  • Loading branch information
braun-daniel authored Jul 4, 2024
2 parents 9810b7d + 6f4130f commit d84c51f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Ensure the following commands are installed and accessible in your environment:
## Usage

```bash
./pim.sh [OPTIONS]
./pim [OPTIONS]
```

### Required Parameters
Expand All @@ -34,13 +34,12 @@ Ensure the following commands are installed and accessible in your environment:
- `--message, -m`: Justification message
- `--role, -r`: Role name (default: Contributor)
- `--time, -t`: Duration (default: 8H). Format: 8H (hours) or 8M (minutes)
- `--verbose, -v`: Enable verbose output
- `--help`: Show help message

### Example

```bash
./pim.sh --subscription "My Subscription" --resource-group "MyResourceGroup" --message "Access required for deployment" --role "Contributor" --time "4H" --verbose
./pim --subscription "My Subscription" --resource-group "MyResourceGroup" --message "Access required for deployment" --role "Contributor" --time "4H" --verbose
```

### Fuzzy Search
Expand All @@ -52,7 +51,7 @@ If you do not provide `--subscription` or `--resource-group`, the script will in
You can run the script without specifying `--subscription` or `--resource-group`, and you will be prompted to select them using `fzf`.

```bash
./pim.sh
./pim
```

## Example Workflow
Expand All @@ -63,7 +62,7 @@ You can run the script without specifying `--subscription` or `--resource-group`
```
2. Run the script:
```bash
./pim.sh --verbose
./pim --verbose
```
3. Select the subscription and resource group using `fzf`.
4. Provide the required justification message when prompted.
Expand Down
27 changes: 12 additions & 15 deletions pim.sh → pim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Optional parameters:
--message, -m Justification message
--role, -r Role name (default: Contributor)
--time, -t Duration (default: 8H). Format: 8H (hours) or 8M (minutes)
--verbose, -v Enable verbose output
--help Show this help message
EOF
exit 1
Expand Down Expand Up @@ -71,9 +70,6 @@ parse_arguments() {
time="$2"
shift
;;
-v|--verbose)
verbose=true
;;
--help)
print_help
;;
Expand All @@ -88,12 +84,11 @@ parse_arguments() {

set_defaults() {
role=${role:-"Contributor"}
time=${time:-"8H"}
verbose=${verbose:-false}
time=${time:-"1H"}
}

fuzzy_select_subscription() {
subscription=$(az account list --query "[].{name:name, id:id}" -o tsv | fzf --prompt="Select Subscription: " | awk '{print $1}')
subscription=$(az account list --query "[].{name:name, id:id}" -o tsv | fzf --prompt="Select Subscription: " | awk '{$NF=""; print $0}' | sed 's/ $//')
if [[ -z "$subscription" ]]; then
echo "No subscription selected. Exiting."
exit 1
Expand Down Expand Up @@ -172,14 +167,15 @@ EOF

if [[ $(echo "$response" | jq -r '.properties.status') == "Provisioned" ]]; then
duration=$(echo "$response" | jq -r '.properties.scheduleInfo.expiration.duration')
# Extract the numeric value from the duration string, assuming it's always in minutes for simplicity
minutes=$(echo "$duration" | grep -o '[0-9]\+')

# Convert minutes to seconds for the date command
seconds=$((minutes * 60))

# Add the duration to the current time and format the output to HH:MM
expiration_time=$(date -v +${seconds}S +"%H:%M")
if [[ $duration == *"H" ]]; then
# Extract hours and convert to minutes for the date command
hours=$(echo "$duration" | grep -o '[0-9]\+')
expiration_time=$(date -v +${hours}H +"%Y-%m-%d %H:%M:%S")
elif [[ $duration == *"M" ]]; then
# Extract minutes directly for the date command
minutes=$(echo "$duration" | grep -o '[0-9]\+')
expiration_time=$(date -v +${minutes}M +"%Y-%m-%d %H:%M:%S")
fi

echo "PIM assignment active. Expires: $expiration_time"
else
Expand All @@ -188,3 +184,4 @@ EOF
}

main "$@"

0 comments on commit d84c51f

Please sign in to comment.