Function/Update-ContainersWithDefinitionFile.Function.psm1

## Namespaces.
using namespace System.Text.Json;
using namespace System.Text.Json.Serialization;

## Custom Modules.
using module '..\Function\Get-ContainerDefinitionFilePath.Function.psm1';
using module '..\Function\Get-ContainerDefinitionJsonOptions.Function.psm1';
using module '..\Class\Definition.Class.psm1';

## Update-Containers Function.
function Update-ContainersWithDefinitionFile
{
    <#
    .NAME
        Update-ContainersWithDefinitionFile

    .SYNOPSIS
        Docker and Podman Container Management Script.

    .SYNTAX
        .\Update-Containers.ps1 `
            [-DefinitionFile <String>] `
            [-DryRun] `
            [-PrintCommands] `
            [-PurgeImages] `
            [-Replacements <HashTable>];

    .DESCRIPTION
        This script manages Docker and Podman containers based on a JSON definition file.

    .PARAMETERS
        -DefinitionFile
            Optional path to the JSON definition file, if not provided,
            the script searches for a default file in standard locations.

            Required? false
            Default $null

        -DryRun
            If specified, the script will output the commands it would execute without actually running them.

            Required? false
            Default $false

        -NoStart
            If specified, the script will not start the containers after creation.

            Required? false
            Default $false

        -Only
            Optional array of container names to limit the operation to specific containers.

            Required? false
            Default @()

        -PrintCommands
            If specified, the script will print each command before executing it.

            Required? false
            Default $false

        -PurgeImages
            If specified, the script will remove all images before pulling new ones.

            Required? false
            Default $false

        -Replacements
            Optional hashtable of string replacements to apply to paths in the definition file.
            [NOTE: Replacement values here will override any replacement values in the definition file.]

            Required? false
            Default @{}

    .EXAMPLE
        .\Update-Containers.ps1 `
            [-DefinitionFile "C:\path\to\definition.json"] `
            [-DryRun] `
            [-PrintCommands] `
            [-PurgeImages] `
            [-Replacements @{
                "VAR1" = "Value1";
                "VAR2" = "Value2";
            }];
    #>


    param (
        ## Definition File Parameter.
        [Parameter(Mandatory = $false, Position = 0)]
        [String] $DefinitionFile = $null,

        ## Dry-Run Parameter.
        [Parameter(Mandatory = $false)]
        [Switch] $DryRun = $false,

        ## NoStart Parameter.
        [Parameter(Mandatory = $false)]
        [Switch] $NoStart = $false,

        ## Only Parameter.
        [Parameter(Mandatory = $false)]
        [String[]] $Only = @(),

        ## PrintCommands Parameter.
        [Parameter(Mandatory = $false)]
        [Switch] $PrintCommands = $false,

        ## Purge Images Parameter.
        [Parameter(Mandatory = $false)]
        [Switch] $PurgeImages = $false,

        ## Replacements Parameter.
        [Parameter(Mandatory = $false)]
        [HashTable] $Replacements = @{}
    );

    ## Localize the container definition file.
    [String] $containerDefinitionFile = (Get-ContainerDefinitionFilePath -File "${DefinitionFile}");

    ## Read the container definition file.
    [String] $containerDefinitionJson = (Get-Content -Path "$(Resolve-Path -Path "${containerDefinitionFile}")" -Raw);

    ## Deserialize our definition file.
    [Definition] $definition = [JsonSerializer]::Deserialize("${containerDefinitionJson}",
            [UpdateContainersDefinition], $(Get-ContainerDefinitionJsonSerializerOptions));

    ## Run the definition.
    $definition.Run($DryRun.ToBool(), $NoStart, $Only, $PrintCommands.ToBool(),
            $PurgeImages.ToBool(), $Replacements);
};