Files
common/dotnet/dotnet-10/nuget_utils.sh
2025-07-07 22:11:35 -07:00

54 lines
1.9 KiB
Bash

#!/bin/bash
# Function to add or remove a single NuGet source
add_or_remove_nuget_source() {
local action="$1"
local source="$2"
local username="$3"
local password="$4"
local source_number="$5"
if [[ -n "$source" ]]; then
# Extract name from URL or use fallback
local name
if [[ "$source" =~ /api/packages/([^/]+)/nuget/ ]]; then
name="${BASH_REMATCH[1]}"
else
name="Source $source_number"
fi
if [[ "$action" == "remove" ]]; then
echo "Removing NuGet source: $source (name: $name)"
dotnet nuget remove source "$name" 2>/dev/null || true
else
echo "Adding NuGet source: $source (name: $name)"
if [[ -n "$username" && -n "$password" ]]; then
dotnet nuget add source "$source" --name "$name" --username "$username" --password "$password" --store-password-in-clear-text
else
dotnet nuget add source "$source" --name "$name"
fi
fi
fi
}
# Function to add/remove NuGet sources
handle_nuget_sources() {
local action="${1:-add}" # Default to 'add', can be 'remove'
echo "Updating NuGet sources with action: $action"
if [[ -n "$NUGET_SOURCES" ]]; then
# Split sources, usernames, and passwords on newlines
IFS=$'\n' read -rd '' -a sources <<< "$NUGET_SOURCES"
IFS=$'\n' read -rd '' -a usernames <<< "$NUGET_USERNAMES"
IFS=$'\n' read -rd '' -a passwords <<< "$NUGET_PASSWORDS"
# Loop through sources
for i in "${!sources[@]}"; do
# Trim whitespace
source=$(echo "${sources[$i]}" | xargs)
username=$(echo "${usernames[$i]:-}" | xargs)
password=$(echo "${passwords[$i]:-}" | xargs)
add_or_remove_nuget_source "$action" "$source" "$username" "$password" "$((i + 1))"
done
fi
}