Skip to content

Commit 00ed04c

Browse files
committed
20231108A
1 parent 4a1b7e9 commit 00ed04c

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

hugoalh.GitHubActionsToolkit/module/parameter.psm1

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ Name of the input.
4444
Whether the input is mandatory; If mandatory but not exist, will throw an error.
4545
.PARAMETER MandatoryMessage
4646
Message when the input is mandatory but not exist.
47-
.PARAMETER NameFilterScript
48-
Filter script block of the name of the inputs.
47+
.PARAMETER NamePrefix
48+
Name of the inputs start with.
49+
.PARAMETER NameSuffix
50+
Name of the inputs end with.
4951
.PARAMETER All
5052
Whether to get all of the inputs.
5153
.PARAMETER Trim
@@ -57,13 +59,14 @@ Whether to trim the value of the input(s).
5759
Function Get-Input {
5860
[CmdletBinding(DefaultParameterSetName = 'One', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_getgithubactionsinput')]
5961
[OutputType([String], ParameterSetName = 'One')]
60-
[OutputType([Hashtable], ParameterSetName = ('All', 'Filter'))]
62+
[OutputType([Hashtable], ParameterSetName = ('All', 'Prefix', 'Suffix'))]
6163
Param (
6264
[Parameter(Mandatory = $True, ParameterSetName = 'One', Position = 0)][ValidatePattern('^(?:[\da-z][\da-z_-]*)?[\da-z]$', ErrorMessage = '`{0}` is not a valid GitHub Actions input name!')][Alias('Key')][String]$Name,
6365
[Parameter(ParameterSetName = 'One')][Alias('Require', 'Required')][Switch]$Mandatory,
6466
[Parameter(ParameterSetName = 'One')][Alias('RequiredMessage', 'RequireMessage')][String]$MandatoryMessage = 'Input `{0}` is not defined!',
6567
[Parameter(Mandatory = $True, ParameterSetName = 'All')][Switch]$All,
66-
[Parameter(Mandatory = $True, ParameterSetName = 'Filter')][Alias('Filter', 'KeyFilter', 'KeyFilterScript', 'NameFilter')][ScriptBlock]$NameFilterScript,
68+
[Parameter(Mandatory = $True, ParameterSetName = 'Prefix')][ValidatePattern('^[\da-z][\da-z_-]*$', ErrorMessage = '`{0}` is not a valid GitHub Actions input name prefix!')][Alias('KeyPrefix', 'KeyStartWith', 'NameStartWith', 'Prefix', 'PrefixKey', 'PrefixName', 'StartWith', 'StartWithKey', 'StartWithName')][String]$NamePrefix,
69+
[Parameter(Mandatory = $True, ParameterSetName = 'Suffix')][ValidatePattern('^[\da-z_-]*[\da-z]$', ErrorMessage = '`{0}` is not a valid GitHub Actions input name suffix!')][Alias('EndWith', 'EndWithKey', 'EndWithName', 'KeyEndWith', 'KeySuffix', 'NameEndWith', 'Suffix', 'SuffixKey', 'SuffixName')][String]$NameSuffix,
6770
[Switch]$Trim
6871
)
6972
Switch ($PSCmdlet.ParameterSetName) {
@@ -76,18 +79,6 @@ Function Get-Input {
7679
Write-Output
7780
Return
7881
}
79-
'Filter' {
80-
[Hashtable]$Result = @{}
81-
ForEach ($Item In (
82-
Get-ChildItem -Path 'Env:\INPUT_*' |
83-
Where-Object -FilterScript $NameFilterScript
84-
)) {
85-
$Result.($Item.Name -ireplace '^INPUT_', '') = $Trim.IsPresent ? ($Item.Value)?.Trim() : $Item.Value
86-
}
87-
$Result |
88-
Write-Output
89-
Return
90-
}
9182
'One' {
9283
$InputValueRaw = [System.Environment]::GetEnvironmentVariable("INPUT_$($Name.ToUpper())")
9384
[AllowEmptyString()][AllowNull()][String]$InputValue = $Trim.IsPresent ? ($InputValueRaw)?.Trim() : $InputValueRaw
@@ -102,6 +93,26 @@ Function Get-Input {
10293
Write-Output
10394
Return
10495
}
96+
'Prefix' {
97+
[String]$InputNameReplaceRegEx = "^INPUT_$([RegEx]::Escape($NamePrefix.ToUpper()))"
98+
[Hashtable]$Result = @{}
99+
ForEach ($Item In (Get-ChildItem -Path "Env:\INPUT_$($NamePrefix.ToUpper())*")) {
100+
$Result.($Item.Name -ireplace $InputNameReplaceRegEx, '') = $Trim.IsPresent ? ($Item.Value)?.Trim() : $Item.Value
101+
}
102+
$Result |
103+
Write-Output
104+
Return
105+
}
106+
'Suffix' {
107+
[String]$InputNameReplaceRegEx = "^INPUT_|$([RegEx]::Escape($NameSuffix.ToUpper()))$"
108+
[Hashtable]$Result = @{}
109+
ForEach ($Item In (Get-ChildItem -Path "Env:\INPUT_*$($NameSuffix.ToUpper())")) {
110+
$Result.($Item.Name -ireplace $InputNameReplaceRegEx, '') = $Trim.IsPresent ? ($Item.Value)?.Trim() : $Item.Value
111+
}
112+
$Result |
113+
Write-Output
114+
Return
115+
}
105116
}
106117
}
107118
<#
@@ -111,8 +122,10 @@ GitHub Actions - Get State
111122
Get state.
112123
.PARAMETER Name
113124
Name of the state.
114-
.PARAMETER NameFilterScript
115-
Filter script block of the name of the states.
125+
.PARAMETER NamePrefix
126+
Name of the states start with.
127+
.PARAMETER NameSuffix
128+
Name of the states end with.
116129
.PARAMETER All
117130
Whether to get all of the states.
118131
.PARAMETER Trim
@@ -124,11 +137,12 @@ Whether to trim the value of the state(s).
124137
Function Get-State {
125138
[CmdletBinding(DefaultParameterSetName = 'One', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_getgithubactionsstate')]
126139
[OutputType([String], ParameterSetName = 'One')]
127-
[OutputType([Hashtable], ParameterSetName = ('All', 'Filter'))]
140+
[OutputType([Hashtable], ParameterSetName = ('All', 'Prefix', 'Suffix'))]
128141
Param (
129142
[Parameter(Mandatory = $True, ParameterSetName = 'One', Position = 0)][ValidatePattern('^(?:[\da-z][\da-z_-]*)?[\da-z]$', ErrorMessage = '`{0}` is not a valid GitHub Actions state name!')][Alias('Key')][String]$Name,
130143
[Parameter(Mandatory = $True, ParameterSetName = 'All')][Switch]$All,
131-
[Parameter(Mandatory = $True, ParameterSetName = 'Filter')][Alias('Filter', 'KeyFilter', 'KeyFilterScript', 'NameFilter')][ScriptBlock]$NameFilterScript,
144+
[Parameter(Mandatory = $True, ParameterSetName = 'Prefix')][ValidatePattern('^[\da-z][\da-z_-]*$', ErrorMessage = '`{0}` is not a valid GitHub Actions state name prefix!')][Alias('KeyPrefix', 'KeyStartWith', 'NameStartWith', 'Prefix', 'PrefixKey', 'PrefixName', 'StartWith', 'StartWithKey', 'StartWithName')][String]$NamePrefix,
145+
[Parameter(Mandatory = $True, ParameterSetName = 'Suffix')][ValidatePattern('^[\da-z_-]*[\da-z]$', ErrorMessage = '`{0}` is not a valid GitHub Actions state name suffix!')][Alias('EndWith', 'EndWithKey', 'EndWithName', 'KeyEndWith', 'KeySuffix', 'NameEndWith', 'Suffix', 'SuffixKey', 'SuffixName')][String]$NameSuffix,
132146
[Switch]$Trim
133147
)
134148
Switch ($PSCmdlet.ParameterSetName) {
@@ -141,21 +155,29 @@ Function Get-State {
141155
Write-Output
142156
Return
143157
}
144-
'Filter' {
158+
'One' {
159+
$StateValueRaw = [System.Environment]::GetEnvironmentVariable("STATE_$($Name.ToUpper())")
160+
$Trim.IsPresent ? ($StateValueRaw)?.Trim() : $StateValueRaw |
161+
Write-Output
162+
Return
163+
}
164+
'Prefix' {
165+
[String]$StateNameReplaceRegEx = "^STATE_$([RegEx]::Escape($NamePrefix.ToUpper()))"
145166
[Hashtable]$Result = @{}
146-
ForEach ($Item In (
147-
Get-ChildItem -Path 'Env:\STATE_*' |
148-
Where-Object -FilterScript $NameFilterScript
149-
)) {
150-
$Result.($Item.Name -ireplace '^STATE_', '') = $Trim.IsPresent ? ($Item.Value)?.Trim() : $Item.Value
167+
ForEach ($Item In (Get-ChildItem -Path "Env:\STATE_$($NamePrefix.ToUpper())*")) {
168+
$Result.($Item.Name -ireplace $StateNameReplaceRegEx, '') = $Trim.IsPresent ? ($Item.Value)?.Trim() : $Item.Value
151169
}
152170
$Result |
153171
Write-Output
154172
Return
155173
}
156-
'One' {
157-
$StateValueRaw = [System.Environment]::GetEnvironmentVariable("STATE_$($Name.ToUpper())")
158-
$Trim.IsPresent ? ($StateValueRaw)?.Trim() : $StateValueRaw |
174+
'Suffix' {
175+
[String]$StateNameReplaceRegEx = "^STATE_|$([RegEx]::Escape($NameSuffix.ToUpper()))$"
176+
[Hashtable]$Result = @{}
177+
ForEach ($Item In (Get-ChildItem -Path "Env:\STATE_*$($NameSuffix.ToUpper())")) {
178+
$Result.($Item.Name -ireplace $StateNameReplaceRegEx, '') = $Trim.IsPresent ? ($Item.Value)?.Trim() : $Item.Value
179+
}
180+
$Result |
159181
Write-Output
160182
Return
161183
}

0 commit comments

Comments
 (0)