From a97dbbc2852126a21596ecd05ed8ca2b6dc30abf Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Mon, 15 Dec 2025 08:01:11 +0100 Subject: [PATCH] Enhanced azure-cli with list command, and more robust volume handling arguments. --- azure-cli | 67 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/azure-cli b/azure-cli index b1214fc..66c3a7d 100755 --- a/azure-cli +++ b/azure-cli @@ -3,9 +3,15 @@ # Set default values ACCOUNT_NAME="$(id -un)" USER_NAME="ubuntu" +EXTRA_ARGS=() +LIST="" while [ $# -gt 0 ]; do case $1 in + --list|-l) + LIST=true + break + ;; --account|-a) ACCOUNT_NAME="$2" shift 2 @@ -14,6 +20,26 @@ while [ $# -gt 0 ]; do USER_NAME="$2" shift 2 ;; + --volume|-v) + read VOL_SRC VOL_DST < <(echo "$2" | tr ':' ' ') + EXTRA_ARGS+=("--mount" "type=bind,source=${VOL_SRC},target=${VOL_DST}") + shift 2 + ;; + --) + # Stop parsing arguments + shift + break + ;; + --help|-h) + cat < /dev/null; then CMD="podman" - HOSTNAME_ARG="--hostname $(hostname -s)" + EXTRA_ARGS+=("--hostname $(hostname -s)") elif command -v docker &> /dev/null; then CMD="docker" - HOSTNAME_ARG="--hostname $(hostname -s)" + EXTRA_ARGS+=("--hostname $(hostname -s)") elif command -v container &> /dev/null; then # Apple container command line tool CMD="container" - HOSTNAME_ARG="" else echo "Error: No usable container runtime was found." >&2 exit 1 fi +if [ -n "$LIST" ]; then + echo "Available accounts:" + $CMD volume ls --format json | jq -r '.[] | select(.labels | has("account")) | .labels.account' + exit 0 +fi + # Check, if the account volume exists, if not create it # This ensures persistent storage for the Azure CLI configuration -if ! $CMD volume inspect "account_$ACCOUNT_NAME" &> /dev/null; then - echo "Creating volume account_$ACCOUNT_NAME for Azure CLI configuration." - if ! $CMD volume create "account_$ACCOUNT_NAME" &> /dev/null; then - echo "Error: Failed to create volume account_$ACCOUNT_NAME." >&2 +if ! $CMD volume inspect "account_$SANITIZED_ACCOUNT_NAME" &> /dev/null; then + echo "Creating volume account_$SANITIZED_ACCOUNT_NAME for Azure CLI configuration." + if ! $CMD volume create "account_$SANITIZED_ACCOUNT_NAME" --label "account=$ACCOUNT_NAME" &> /dev/null; then + echo "Error: Failed to create volume account_$SANITIZED_ACCOUNT_NAME." >&2 exit 1 fi fi +EXTRA_ARGS+=( + "--mount" "type=volume,source=account_$SANITIZED_ACCOUNT_NAME,target=/home/${USER_NAME}" + "--mount" "type=bind,source=$(pwd),target=/workdir" +) + # Run the container -$CMD run \ - --rm \ - -it \ - --mount type=volume,source="account_$ACCOUNT_NAME",target=/home/${USER_NAME} \ - --mount type=bind,source=$(pwd),target=/workdir \ - --name "azure-cli" \ - $HOSTNAME_ARG \ +$CMD run --rm -it \ + ${EXTRA_ARGS[@]} \ + --name "azure-cli-$SANITIZED_ACCOUNT_NAME" \ --workdir /workdir \ - "$@" \ - $IMAGE_NAME --user "$USER_NAME" + $IMAGE_NAME --user "$USER_NAME" "$@"