Class/UpdateContainersContainerPublishDefinition.Class.psm1

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

## Container Publish Definition Class.
class UpdateContainersContainerPublishDefinition
{
    <#
    .NAME
        UpdateContainersContainerPublishDefinition

    .SYNOPSIS
        Class defining a port publishing configuration for container engines.

    .DESCRIPTION
        This class represents the configuration details for publishing a port
        from a container to the host system, which can be applied to containers
        managed by container engines like Docker and Podman.

    .PROPERTIES
        Container
            The port inside the container to be published.

        Host
            The port on the host system to which the container port will be mapped.

        Protocol
            The protocol to use for the port mapping (e.g., tcp, udp).
    #>


    ## Container Property.
    [JsonPropertyName('container')]
    [JsonRequired()]
    [String] $Container;

    ## Host Property.
    [AllowNull()]
    [JsonPropertyName('host')]
    [String] $Host = $null;

    ## Protocol Property.
    [AllowNull()]
    [JsonPropertyName('protocol')]
    [String] $Protocol = $null;

    ## ToCommandLine Method.
    [String] ToCommandLine()
    {
        <#
        .NAME
            ToCommandLine

        .SYNOPSIS
            Generates the command line string for the port publishing configuration.

        .DESCRIPTION
            This method constructs the appropriate command line string to publish
            a port from a container to the host system using the properties defined
            in the ContainerPublishDefinition class.

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


        ## Reconcile the host port.
        $this.Host = ($null -eq $this.Host -or "" -eq $this.Host.Trim() ? $this.Container : $this.Host.Trim());

        ## Define our command line string.
        [String] $command = "--publish '$($this.Host.Trim()):$($this.Container.Trim())";

        ## Check for a provided protocol.
        if ($null -ne $this.Protocol -and "" -ne $this.Protocol.Trim())
        {
            ## Append the protocol to the command line string.
            $command = "${command}/$($this.Protocol.ToLower().Trim())";
        }

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