|
| 1 | +#!/bin/bash |
| 2 | + |
1 | 3 | # Logs in to Azure through AZD and AZ CLI
|
2 | 4 | # It does the following:
|
3 | 5 | # 1. Checks if the user is logged in to Azure
|
|
8 | 10 | # 6. Sets the active subscription to the selected subscription
|
9 | 11 | # 7. Exits if the subscription is not found
|
10 | 12 |
|
11 |
| -# $REPOSITORY_ROOT = git rev-parse --show-toplevel |
12 |
| -$REPOSITORY_ROOT = "$(Split-Path $MyInvocation.MyCommand.Path)/../.." |
| 13 | +set -e |
| 14 | + |
| 15 | +# REPOSITORY_ROOT=$(git rev-parse --show-toplevel) |
| 16 | +REPOSITORY_ROOT="$(dirname "$(realpath "$0")")/../.." |
13 | 17 |
|
14 | 18 | # Load the azd environment variables
|
15 |
| -& "$REPOSITORY_ROOT/infra/hooks/load_azd_env.ps1" |
| 19 | +"$REPOSITORY_ROOT/infra/hooks/load_azd_env.sh" |
16 | 20 |
|
17 | 21 | # AZD LOGIN
|
18 | 22 | # Check if the user is logged in to Azure
|
19 |
| -$login_status = azd auth login --check-status |
| 23 | +login_status=$(azd auth login --check-status) |
20 | 24 |
|
21 | 25 | # Check if the user is not logged in
|
22 |
| -if ($login_status -like "*Not logged in*") { |
23 |
| - Write-Host "Not logged in, initiating login process..." |
24 |
| - # Command to log in to Azure |
25 |
| - azd auth login |
26 |
| -} |
| 26 | +if [[ "$login_status" == *"Not logged in"* ]]; then |
| 27 | + echo "Not logged in, initiating login process..." |
| 28 | + # Command to log in to Azure |
| 29 | + azd auth login |
| 30 | +fi |
27 | 31 |
|
28 | 32 | # AZ LOGIN
|
29 |
| -$EXPIRED_TOKEN = az ad signed-in-user show --query 'id' -o tsv 2>$null |
| 33 | +EXPIRED_TOKEN=$(az ad signed-in-user show --query 'id' -o tsv 2>/dev/null || true) |
30 | 34 |
|
31 |
| -if ([string]::IsNullOrEmpty($EXPIRED_TOKEN)) { |
| 35 | +if [[ -z "$EXPIRED_TOKEN" ]]; then |
32 | 36 | az login --scope https://graph.microsoft.com/.default -o none
|
33 |
| -} |
| 37 | +fi |
34 | 38 |
|
35 |
| -if ([string]::IsNullOrEmpty($env:AZURE_SUBSCRIPTION_ID)) { |
36 |
| - $ACCOUNT = az account show --query '[id,name]' |
37 |
| - Write-Host "You can set the `AZURE_SUBSCRIPTION_ID` environment variable with `azd env set AZURE_SUBSCRIPTION_ID`." |
38 |
| - Write-Host $ACCOUNT |
| 39 | +if [[ -z "${AZURE_SUBSCRIPTION_ID:-}" ]]; then |
| 40 | + ACCOUNT=$(az account show --query '[id,name]') |
| 41 | + echo "You can set the \`AZURE_SUBSCRIPTION_ID\` environment variable with \`azd env set AZURE_SUBSCRIPTION_ID\`." |
| 42 | + echo $ACCOUNT |
39 | 43 |
|
40 |
| - $response = Read-Host "Do you want to use the above subscription? (Y/n) " |
41 |
| - $response = if ([string]::IsNullOrEmpty($response)) { "Y" } else { $response } |
42 |
| - switch ($response) { |
43 |
| - { $_ -match "^[yY](es)?$" } { |
44 |
| - # Do nothing |
45 |
| - break |
46 |
| - } |
47 |
| - default { |
48 |
| - Write-Host "Listing available subscriptions..." |
49 |
| - $SUBSCRIPTIONS = az account list --query 'sort_by([], &name)' --output json |
50 |
| - Write-Host "Available subscriptions:" |
51 |
| - Write-Host ($SUBSCRIPTIONS | ConvertFrom-Json | ForEach-Object { "{0} {1}" -f $_.name, $_.id } | Format-Table) |
52 |
| - $subscription_input = Read-Host "Enter the name or ID of the subscription you want to use: " |
53 |
| - $AZURE_SUBSCRIPTION_ID = ($SUBSCRIPTIONS | ConvertFrom-Json | Where-Object { $_.name -eq $subscription_input -or $_.id -eq $subscription_input } | Select-Object -exp id) |
54 |
| - if (-not [string]::IsNullOrEmpty($AZURE_SUBSCRIPTION_ID)) { |
55 |
| - Write-Host "Setting active subscription to: $AZURE_SUBSCRIPTION_ID" |
| 44 | + read -r -p "Do you want to use the above subscription? (Y/n) " response |
| 45 | + response=${response:-Y} |
| 46 | + case "$response" in |
| 47 | + [yY][eE][sS]|[yY]) |
| 48 | + ;; |
| 49 | + *) |
| 50 | + echo "Listing available subscriptions..." |
| 51 | + SUBSCRIPTIONS=$(az account list --query 'sort_by([], &name)' --output json) |
| 52 | + echo "Available subscriptions:" |
| 53 | + echo "$SUBSCRIPTIONS" | jq -r '.[] | [.name, .id] | @tsv' | column -t -s $'\t' |
| 54 | + read -r -p "Enter the name or ID of the subscription you want to use: " subscription_input |
| 55 | + AZURE_SUBSCRIPTION_ID=$(echo "$SUBSCRIPTIONS" | jq -r --arg input "$subscription_input" '.[] | select(.name==$input or .id==$input) | .id') |
| 56 | + if [[ -n "$AZURE_SUBSCRIPTION_ID" ]]; then |
| 57 | + echo "Setting active subscription to: $AZURE_SUBSCRIPTION_ID" |
56 | 58 | az account set -s $AZURE_SUBSCRIPTION_ID
|
57 |
| - } |
58 |
| - else { |
59 |
| - Write-Host "Subscription not found. Please enter a valid subscription name or ID." |
| 59 | + else |
| 60 | + echo "Subscription not found. Please enter a valid subscription name or ID." |
60 | 61 | exit 1
|
61 |
| - } |
62 |
| - break |
63 |
| - } |
64 |
| - } |
65 |
| -} |
66 |
| -else { |
67 |
| - az account set -s $env:AZURE_SUBSCRIPTION_ID |
68 |
| -} |
| 62 | + fi |
| 63 | + ;; |
| 64 | + *) |
| 65 | + echo "Use the \`az account set\` command to set the subscription you'd like to use and re-run this script." |
| 66 | + exit 0 |
| 67 | + ;; |
| 68 | + esac |
| 69 | +else |
| 70 | + az account set -s $AZURE_SUBSCRIPTION_ID |
| 71 | +fi |
0 commit comments