Class/UpdateContainersContainerMountDefinition.Class.psm1

## Namespaces.
using namespace System.Collections.Generic;
using namespace System.IO;
using namespace System.Text.Json;
using namespace System.Text.Json.Serialization;

## Custom Modules.
using module '.\UpdateContainersReplacementDefinition.Class.psm1';

## Container Mount Definition Class.
class UpdateContainersContainerMountDefinition
{
    <#
    .NAME
        UpdateContainersContainerMountDefinition

    .SYNOPSIS
        Class defining a mount configuration for container engines.

    .DESCRIPTION
        This class represents the configuration details for a mount that can be
        applied to containers managed by container engines like Docker and Podman.

    .PROPERTIES
        Destination
            The path inside the container where the mount will be located.

        Source
            The path on the host system to be mounted into the container.

        ReadOnly
            A boolean indicating whether the mount should be read-only.

        Type
            The type of mount (e.g., bind, volume, tmpfs).
    #>


    ## Destination Property.
    [JsonPropertyName('destination')]
    [String] $Destination = $null;

    ## Source Property.
    [JsonPropertyName('source')]
    [JsonRequired()]
    [String] $Source;

    ## Read-Only Property.
    [JsonPropertyName('ro')]
    [Boolean] $ReadOnly = $false;

    ## Type Property.
    [JsonPropertyName('type')]
    [String] $Type = "bind";

    ## ToCommandLine Method.
    [String] ToCommandLine([List[UpdateContainersReplacementDefinition]] $replacements = [List[UpdateContainersReplacementDefinition]]::new())
    {
        <#
        .NAME
            ToCommandLine

        .SYNOPSIS
            Generates the command line string for the mount configuration.

        .DESCRIPTION
            This method constructs the appropriate command line string to define
            a mount for a container using the properties defined in the
            ContainerMountDefinition class. It also applies any provided path
            replacements to the source and destination paths.

        .PARAMETERS
            replacements
                A list of DefinitionReplacement objects to apply to the source
                and destination paths.

                Required? false
                Default @()

        .OUTPUTS
            String
                The command line string for the mount configuration.
        #>


        ## Ensure we have a destination.
        if ($null -eq $this.Destination -or "" -eq $this.Destination.Trim())
        {
            ## Reset the destination to the source path.
            $this.Destination = $this.Source.Trim();
        }

        ## Iterate over the replacements.
        foreach ($replacement in $replacements)
        {
            ## Make the replacement in the destination path.
            $this.Destination = $replacement.Replace($this.Destination);

            ## Make the replacement in the source path.
            $this.Source = $replacement.Replace($this.Source);
        }

        ## Define our command line string.
        [String] $command = "--mount 'destination=$($this.Destination.Trim())";

        ## Check the read-only flag.
        if ($this.ReadOnly)
        {
            ## Append the read-only flag to the command line string.
            $command = "${command},ro=true";
        }

        ## Add the source to the command line string.
        $command = "${command},source=$($this.Source.Trim())";

        ## Check for a provided type.
        if ($null -ne $this.Type -and "" -ne $this.Type.Trim())
        {
            ## Append the type to the command line string.
            $command = "${command},type=$($this.Type.ToLower().Trim())";
        }
        else
        {
            ## Default the type to bind in the command line string.
            $command = "${command},type=bind";
        }

        ## We're done, return the command line string.
        return "${command}'".Trim();
    }
};