Azure / trusted-signing-action

MIT License
21 stars 8 forks source link

Add extra option to the Powershell module and this action to filter the files that need to be signed. #26

Open dlemstra opened 1 month ago

dlemstra commented 1 month ago

This action now contains the following two options:

I would like an extra option called files that would allow me to specify the files that need to be signed. I know I can do that with files-catalog but that would require me to include an extra step in the pipeline to create this file.

An other option would extending the files-folder-filter option and allowing me to specify a filename or a filename filter of the files that need to be signed. That option now only filters the extensions but it would also be nice if I could use Magick.NET.*.dll or Magick.NET.Core.dll,Magick.NET.Core.dll,Magick.NET.SystemWindowsMedia.dll in that field.

japarson commented 1 month ago

Issue #9 already covers adding the files option, but I'll leave this open for the feature suggestion of:

An other option would extending the files-folder-filter option and allowing me to specify a filename or a filename filter of the files that need to be signed. That option now only filters the extensions but it would also be nice if I could use Magick.NET.*.dll or Magick.NET.Core.dll,Magick.NET.Core.dll,Magick.NET.SystemWindowsMedia.dll in that field.

which I think is a great idea!

dlemstra commented 1 month ago

Looks like I completely missed that issue. Thanks for considering this option. Would you be open to sending you patches for the PS module to make this possible?

japarson commented 1 month ago

@dlemstra That would be awesome, thank you!

dlemstra commented 1 month ago

I think we could do something like this @japarson. We can use the build-in -Like operator to check if the filename matches the filter. This also has backwards compatibility with the old syntax because it changes dll to *.dll.

function Test-FileFilterMatch {
    param (
        [Parameter(Mandatory)]
        [List[string]]$FileFilterList,

        [Parameter(Mandatory)]
        [string]$FileName
    )

    if ($FileFilterList.Count -eq 0) {
        return $true
    }

    foreach ($fileFilter in $FileFilterList) {
        if ($FileName -Like $fileFilter) {
            return $true
        }
    }
}

function Get-FilteredFileList {
    param (
        [Parameter(Mandatory)]
        [string]$Path,

        [Parameter()]
        [AllowEmptyString()]
        [string]$Filter,

        [Parameter()]
        [switch]$Recurse = $false,

        [Parameter()]
        [AllowNull()]
        [int]$Depth
    )

    $filteredFiles = [List[string]]::new()

    # Get the List of files from the specified folder path.
    $getChildItemParams = @{
        Path = $Path
        File = $true
    }

    if ($Recurse) {
        $getChildItemParams["Recurse"] = $Recurse
    }

    if ($Depth) {
        $getChildItemParams["Depth"] = $Depth
    }

    $fileList = Get-ChildItem @getChildItemParams

    $fileFilterList = [List[string]]::new()

    if ($Filter) {
        foreach ($fileFilter in $Filter.Split(",")) {
            if (-not $fileFilter.Contains(".")) {
                $fileFilter = "*." + $fileFilter
            }
            $fileFilterList.Add($fileFilter)
        }
    }

    foreach ($file in $fileList) {
        if (Test-FileFilterMatch -FileFilterList $fileFilterList -FileName $file.Name) {
            $fullPath = Join-Path -Path $file.DirectoryName -ChildPath $file.Name
            $filteredFiles.Add($fullPath)
        }
    }

    return , $filteredFiles
}