Public/Remove-DClaudeImage.ps1

<#
.SYNOPSIS
    Removes a Docker image entry from the dclaude user configuration.

.DESCRIPTION
    Removes an image or a specific platform entry from ~/.dclaude/settings.json.
    If removing a specific platform leaves no platforms, the entire image entry
    is removed.

.PARAMETER Name
    Name of the image entry to remove.

.PARAMETER Platform
    Remove only this platform (Windows or Linux). If omitted, removes the
    entire image entry with all platforms.

.EXAMPLE
    Remove-DClaudeImage -Name 'pwsh'

    Removes the 'pwsh' image entry entirely (all platforms).

.EXAMPLE
    Remove-DClaudeImage -Name 'pwsh' -Platform Linux

    Removes only the Linux platform entry for 'pwsh'.
#>

function Remove-DClaudeImage {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter()]
        [ValidateSet('Windows', 'Linux')]
        [string]$Platform
    )

    $directory = Join-Path $HOME '.dclaude'
    $mergedConfig = Get-DClaudeUserConfig
    $config = Read-SettingsFile -Directory $directory

    # Check the merged view (base + local override) for existence
    if (-not $mergedConfig -or -not $mergedConfig.PSObject.Properties['images'] -or -not $mergedConfig.images) {
        Write-Error "Image '$Name' not found in user config."
        return
    }

    if (-not $mergedConfig.images.PSObject.Properties[$Name]) {
        Write-Error "Image '$Name' not found in user config."
        return
    }

    # Image exists in merged view; check if it exists in base settings.json for actual removal
    $existsInBase = $config -and
        $config.PSObject.Properties['images'] -and
        $config.images -and
        $config.images.PSObject.Properties[$Name]

    if ($Platform) {
        $platformKey = $Platform.ToLower()
        if (-not $mergedConfig.images.$Name.PSObject.Properties[$platformKey]) {
            Write-Error "Image '$Name' does not have a '$platformKey' platform entry in user config."
            return
        }
        # Platform exists in merged view; check if removal is possible from base
        if (-not $existsInBase -or -not $config.images.$Name.PSObject.Properties[$platformKey]) {
            Write-Error "Image '$Name' platform '$platformKey' exists in settings.local.json but not in settings.json. Edit settings.local.json directly to remove it."
            return
        }
        $target = "Image '$Name' ($platformKey)"
    }
    else {
        if (-not $existsInBase) {
            Write-Error "Image '$Name' exists in settings.local.json but not in settings.json. Edit settings.local.json directly to remove it."
            return
        }
        $target = "Image '$Name' (all platforms)"
    }

    if ($PSCmdlet.ShouldProcess($target, 'Remove')) {
        if ($Platform) {
            $config.images.$Name.PSObject.Properties.Remove($platformKey)

            # If no platforms remain, remove the entire image entry
            if (@($config.images.$Name.PSObject.Properties).Count -eq 0) {
                $config.images.PSObject.Properties.Remove($Name)
            }
        }
        else {
            $config.images.PSObject.Properties.Remove($Name)
        }

        Save-SettingsFile -Directory $directory -Config $config
    }
}