Function/Get-ContainerDefinitionFilePath.Function.psm1
using namespace System; using namespace System.IO; using namespace System.Collections.Generic; ## Get-ContainerDefinitionFilePath Function. function Get-ContainerDefinitionFilePath { <# .NAME Get-ContainerDefinitionFilePath .SYNOPSIS Retrieves the path to the container definition file. .SYNTAX Get-DefinitionFilePath [-File <String>] .DESCRIPTION This function checks for a user-provided definition file path. If none is provided, it searches for a default definition file in standard locations based on the operating system. .PARAMETERS -File Optional path to the definition file. If not provided, the function searches for a default file. Required? false Default $null .OUTPUTS String The path to the found definition file. .EXAMPLE [String] $definitionFilePath = Get-DefinitionFilePath -File "C:\path\to\definition.json"; #> param ( ## Definition File Parameter. [Parameter(Mandatory = $false, Position = 0)] [String] $File = $null ); ## Check for a provided definition file. if ($null -ne $File -and "" -ne $File.Trim()) { ## Check for an absolute path. if (-not [Path]::IsPathRooted($File.Trim())) { ## Reset the path to its absolute path. $File = [Path]::Combine((Get-Location).Path.Trim(), $File.Trim()).Trim(); } ## Resolve the path to its full path. $File = (Resolve-Path -Path $File.Trim()).Path.Trim(); ## Check to see if the file exists. if (Test-Path -Path "${File}" -PathType Leaf) { ## We found the provided definition file, return its path. return "${File}"; } ## The provided definition file does not exist. throw "Provided Definition File Not Found"; } ## Define our list of possible definition file paths based on the current operating system. [List[String]] $paths = [List[String]]::new(); ## Add the current working directory path to the list of paths to check. $paths.AddRange([List[String]]@( "$(Join-Path -Path "$(Get-Location)" -ChildPath 'container-definitions.json')" "$(Join-Path -Path "$(Get-Location)" -ChildPath 'container-definitions.example.json')" "$(Join-Path -Path "$(Get-Location)" -ChildPath 'container-definitions.jsonc')" "$(Join-Path -Path "$(Get-Location)" -ChildPath 'container-definitions.example.jsonc')" )) ## Check the operating system. if ([Environment]::OSVersion.Platform -eq [PlatformID]::Unix) { ## Add the Linux and MacOS Paths to the list of paths to check. $paths.AddRange([List[String]]@( '/host/container-definitions.json' '/host/container-definitions.example.json' '/host/srv/container-definitions.jsonc' '/host/srv/container-definitions.example.jsonc' '/host/etc/container-definitions.json' '/host/etc/container-definitions.example.json' '/host/usr/local/etc/container-definitions.jsonc' '/host/usr/local/etc/container-definitions.example.jsonc' '/host/opt/container-definitions.jsonc' '/host/opt/container-definitions.example.jsonc' "$(Join-Path -Path "${env:PWD}" -ChildPath 'container-definitions.json')" "$(Join-Path -Path "${env:PWD}" -ChildPath 'container-definitions.example.json')" "$(Join-Path -Path "${env:PWD}" -ChildPath 'container-definitions.jsonc')" "$(Join-Path -Path "${env:PWD}" -ChildPath 'container-definitions.example.jsonc')" "$(Join-Path -Path "${env:HOME}" -ChildPath '.container-definitions.json')" '/etc/container-definitions.json' '/usr/local/etc/container-definitions.json' '/srv/container-definitions.json' "$(Join-Path -Path "${env:HOME}" -ChildPath '.container-definitions.jsonc')" '/etc/container-definitions.jsonc' '/usr/local/etc/container-definitions.jsonc' '/srv/container-definitions.jsonc' )); } else { ## Add the Windows Paths to the list of paths to check. $paths.AddRange(@( "$(Join-Path -Path "${env:HOME}" -ChildPath '.container-definitions.json')" "$(Join-Path -Path "${env:USERPROFILE}" -ChildPath '.container-definitions.json')" "$(Join-Path -Path "${env:ProgramData}" -ChildPath 'container-definitions.json')" "$(Join-Path -Path "${env:ProgramFiles}" -ChildPath 'container-definitions.json')" "$(Join-Path -Path "${env:ProgramFiles(x86)}" -ChildPath 'container-definitions.json')" "$(Join-Path -Path "${env:USERPROFILE}" -ChildPath '.container-definitions.jsonc')" "$(Join-Path -Path "${env:ProgramData}" -ChildPath 'container-definitions.jsonc')" "$(Join-Path -Path "${env:ProgramFiles}" -ChildPath 'container-definitions.jsonc')" "$(Join-Path -Path "${env:ProgramFiles(x86)}" -ChildPath 'container-definitions.jsonc')" )); } ## Iterate over the possible definition file paths. foreach ($path in $paths) { ## Check for the existence of the definition file. if (Test-Path -Path $path.Trim() -PathType Leaf) { ## Write to the host that we found a definition file. Write-Host "Found Container Definition File: $($path.Trim())"; ## We found a definition file, return its path. return $path.Trim(); } } ## If we get here, no definition file could be found. throw "Definition File Not Found"; } |