Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AtlassianPS.Configuration/Private/Import-MqcnAlias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
)

begin {
Set-Alias -Name $Alias -Value $Command -Scope 1
Set-Alias -Name $Alias -Value $Command -Scope 1 -WhatIf:$false
}
}
15 changes: 10 additions & 5 deletions AtlassianPS.Configuration/Public/Add-ServerConfiguration.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Add-ServerConfiguration {
# .ExternalHelp ..\AtlassianPS.Configuration-help.xml
[CmdletBinding()]
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $true )]
[OutputType( [void] )]
param(
[Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )]
Expand Down Expand Up @@ -35,6 +35,8 @@
foreach ($server in @(Get-ServerConfiguration)) {
$serverList.Add($server)
}

$configurationChanged = $false
}

process {
Expand All @@ -54,7 +56,7 @@
}
WriteError @writeErrorSplat
}
else {
elseif ($PSCmdlet.ShouldProcess($entryName, "Add server configuration")) {
if (-not ($index = ($serverList.Id | Measure-Object -Maximum).Maximum)) {
$index = 0
}
Expand All @@ -73,13 +75,16 @@
Write-Verbose "Adding server #$($index): [$($config.Name)]"
Write-DebugMessage "Adding server `$config: $($config.Name) @ index $index" -BreakPoint
$serverList.Add($config)
$configurationChanged = $true
}
}

end {
Write-DebugMessage "Persisting ServerList"
$script:Configuration["ServerList"] = $serverList
Save-Configuration
if ($configurationChanged) {
Write-DebugMessage "Persisting ServerList"
$script:Configuration["ServerList"] = $serverList
Save-Configuration
}

Write-Verbose "Function ended"
}
Expand Down
12 changes: 8 additions & 4 deletions AtlassianPS.Configuration/Public/Remove-Configuration.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function Remove-Configuration {
# .ExternalHelp ..\AtlassianPS.Configuration-help.xml
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $false )]
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $true )]
[OutputType( [void] )]
[System.Diagnostics.CodeAnalysis.SuppressMessage( 'PSUseShouldProcessForStateChangingFunctions', '' )]
param(
[Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )]
[ValidateNotNullOrEmpty()]
Expand All @@ -24,6 +23,7 @@
Write-Verbose "Function started"

$reservedNames = @('ServerList')
$configurationChanged = $false
}

process {
Expand All @@ -45,12 +45,16 @@

Write-Verbose "Filtering for [name = $_name]"

$script:Configuration.Remove($_name)
if ($PSCmdlet.ShouldProcess($_name, "Remove configuration key")) {
$configurationChanged = $script:Configuration.Remove($_name) -or $configurationChanged
}
}
}

end {
Save-Configuration
if ($configurationChanged) {
Save-Configuration
}

Write-Verbose "Function ended"
}
Expand Down
30 changes: 19 additions & 11 deletions AtlassianPS.Configuration/Public/Remove-ServerConfiguration.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function Remove-ServerConfiguration {
# .ExternalHelp ..\AtlassianPS.Configuration-help.xml
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $false )]
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $true )]
[OutputType( [void] )]
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
param(
[Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )]
[ArgumentCompleter( {
Expand All @@ -26,6 +25,8 @@
foreach ($server in @(Get-ServerConfiguration)) {
$serverList.Add($server)
}

$configurationChanged = $false
}

process {
Expand All @@ -46,20 +47,27 @@
}
}

$remainingServers = [System.Collections.Generic.List[AtlassianPS.ServerData]]::new()
foreach ($server in $serverList) {
if ($server.Name -notin $Name) {
$remainingServers.Add($server)
foreach ($serverToRemove in $Name) {
if ($serverToRemove -in $serverList.Name -and $PSCmdlet.ShouldProcess($serverToRemove, "Remove server configuration")) {
$remainingServers = [System.Collections.Generic.List[AtlassianPS.ServerData]]::new()
foreach ($server in $serverList) {
if ($server.Name -ne $serverToRemove) {
$remainingServers.Add($server)
}
}
$serverList = $remainingServers
$configurationChanged = $true
}
}
$serverList = $remainingServers
}

end {
Write-DebugMessage "Persisting ServerList"
$script:Configuration.Remove("ServerList")
$script:Configuration.Add("ServerList", $serverList)
Save-Configuration
if ($configurationChanged) {
Write-DebugMessage "Persisting ServerList"
$script:Configuration.Remove("ServerList")
$script:Configuration.Add("ServerList", $serverList)
Save-Configuration
}

Write-Verbose "Function ended"
}
Expand Down
19 changes: 12 additions & 7 deletions AtlassianPS.Configuration/Public/Set-Configuration.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function Set-Configuration {
# .ExternalHelp ..\AtlassianPS.Configuration-help.xml
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $false )]
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $true )]
[OutputType( [PSCustomObject] )]
[System.Diagnostics.CodeAnalysis.SuppressMessage( 'PSUseShouldProcessForStateChangingFunctions', '' )]
param(
[Parameter( Mandatory, ValueFromPipelineByPropertyName )]
[ValidateNotNullOrEmpty()]
Expand Down Expand Up @@ -37,6 +36,7 @@
Write-Verbose "Function started"

$reservedNames = @('ServerList')
$configurationChanged = $false
}

process {
Expand Down Expand Up @@ -81,16 +81,21 @@
else { $dataType = "null" }
Write-Verbose "Storing value [$dataType] to [name = $Name]"

$script:Configuration.Remove($Name)
$script:Configuration.Add($Name, $Value)
if ($PSCmdlet.ShouldProcess($Name, "Set configuration value")) {
$script:Configuration.Remove($Name)
$script:Configuration.Add($Name, $Value)
$configurationChanged = $true

if ($Passthru) {
Get-Configuration -Name $Name
if ($Passthru) {
Get-Configuration -Name $Name
}
}
}

end {
Save-Configuration
if ($configurationChanged) {
Save-Configuration
}

Write-Verbose "Function ended"
}
Expand Down
17 changes: 11 additions & 6 deletions AtlassianPS.Configuration/Public/Set-ServerConfiguration.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function Set-ServerConfiguration {
# .ExternalHelp ..\AtlassianPS.Configuration-help.xml
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $false )]
[CmdletBinding( ConfirmImpact = 'Low', SupportsShouldProcess = $true )]
[OutputType( [void] )]
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
param(
[Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )]
[ValidateRange(1, [UInt32]::MaxValue)]
Expand Down Expand Up @@ -55,6 +54,7 @@
'WhatIf'
'Confirm'
)
$configurationChanged = $false
}

process {
Expand All @@ -75,10 +75,13 @@
return
}

foreach ($property in ($PSBoundParameters.Keys | Where-Object { $_ -notin $parametersToIgnore } )) {
Write-Verbose "Changing [$property] of entry #$Id"
if ($PSCmdlet.ShouldProcess("#$Id ($($serverEntry.Name))", "Update server configuration")) {
foreach ($property in ($PSBoundParameters.Keys | Where-Object { $_ -notin $parametersToIgnore } )) {
Write-Verbose "Changing [$property] of entry #$Id"

$serverEntry.$property = Get-Variable $property -ValueOnly
$serverEntry.$property = Get-Variable $property -ValueOnly
$configurationChanged = $true
}
}
}
else {
Expand All @@ -94,7 +97,9 @@
}

end {
Save-Configuration
if ($configurationChanged) {
Save-Configuration
}

Write-Verbose "Function ended"
}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Improvements

- Added `-WhatIf` and `-Confirm` support to mutating configuration and server configuration commands.
- Migrated `Tools/setup.ps1` to shared `AtlassianPS.Standards` bootstrap/dependency commands with synchronized ScriptAnalyzer settings.
- Migrated `Tools/update.dependencies.ps1` to shared `AtlassianPS.Standards\Update-AtlassianPSDependencyReference` with `ShouldProcess` and fail-fast behavior.
- Aligned workflow setup pins and build/release standards version references to `AtlassianPS.Standards` `0.1.6`.
Expand Down
8 changes: 8 additions & 0 deletions Tests/Functions/Add-ServerConfiguration.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ Describe "Add-ServerConfiguration" -Tag Unit {
(Get-ServerConfiguration).Name | Should -Contain "New Server"
}

It "does not add or save a server when WhatIf is used" {
Add-ServerConfiguration -Name "New Server" -Uri "https://atlassianps.org" -Type Jira -WhatIf

Get-ServerConfiguration | Should -HaveCount 2
(Get-ServerConfiguration).Name | Should -Not -Contain "New Server"
Should -Invoke "Save-Configuration" -ModuleName "AtlassianPS.Configuration" -Exactly -Times 0 -Scope It
}

It "uses the [Uri]::Authority as default for server's name" {
Get-ServerConfiguration | Should -HaveCount 2
(Get-ServerConfiguration).Name | Should -Not -Contain "atlassianps.org"
Expand Down
38 changes: 24 additions & 14 deletions Tests/Functions/Remove-Configuration.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ Describe "Remove-Configuration" -Tag Unit {
}

InModuleScope "AtlassianPS.Configuration" {
#region Mocking
Mock Write-DebugMessage -ModuleName "AtlassianPS.Configuration" {}
Mock Write-Verbose -ModuleName "AtlassianPS.Configuration" {}
Mock Save-Configuration -ModuleName "AtlassianPS.Configuration" {}

Mock Get-Configuration {
$tempConfig = $script:Configuration.Clone()
$tempConfig.Keys |
ForEach-Object {
[PSCustomObject]@{
Name = $_
Value = $tempConfig[$_]
BeforeEach {
#region Mocking
Mock Write-DebugMessage -ModuleName "AtlassianPS.Configuration" {}
Mock Write-Verbose -ModuleName "AtlassianPS.Configuration" {}
Mock Save-Configuration -ModuleName "AtlassianPS.Configuration" {}

Mock Get-Configuration {
$tempConfig = $script:Configuration.Clone()
$tempConfig.Keys |
ForEach-Object {
[PSCustomObject]@{
Name = $_
Value = $tempConfig[$_]
}
}
}
}
#endregion Mocking
}
#endregion Mocking

Context "Sanity checking" {
BeforeAll {
Expand Down Expand Up @@ -73,6 +75,14 @@ Describe "Remove-Configuration" -Tag Unit {
Get-Configuration | Where-Object Name -eq "Bar" | Should -Not -BeNullOrEmpty
}

It "does not remove or save a configuration key when WhatIf is used" {
Remove-Configuration -Name "Foo" -WhatIf

Get-Configuration | Should -HaveCount 4
Get-Configuration | Where-Object Name -eq "Foo" | Should -Not -BeNullOrEmpty
Should -Invoke "Save-Configuration" -ModuleName "AtlassianPS.Configuration" -Exactly -Times 0 -Scope It
}

It "removes multiple entries at once" {
Get-Configuration | Should -HaveCount 4
Get-Configuration | Where-Object Name -eq "Foo" | Should -Not -BeNullOrEmpty
Expand Down
26 changes: 18 additions & 8 deletions Tests/Functions/Remove-ServerConfiguration.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ Describe "Remove-ServerConfiguration" -Tag Unit {
}

InModuleScope "AtlassianPS.Configuration" {
#region Mocking
Mock Write-DebugMessage -ModuleName "AtlassianPS.Configuration" {}
Mock Write-Verbose -ModuleName "AtlassianPS.Configuration" {}
Mock Save-Configuration -ModuleName "AtlassianPS.Configuration" {}

Mock Get-ServerConfiguration {
$script:Configuration["ServerList"]
BeforeEach {
#region Mocking
Mock Write-DebugMessage -ModuleName "AtlassianPS.Configuration" {}
Mock Write-Verbose -ModuleName "AtlassianPS.Configuration" {}
Mock Save-Configuration -ModuleName "AtlassianPS.Configuration" {}

Mock Get-ServerConfiguration {
$script:Configuration["ServerList"]
}
#endregion Mocking
}
#endregion Mocking

Context "Sanity checking" {
BeforeAll {
Expand Down Expand Up @@ -70,6 +72,14 @@ Describe "Remove-ServerConfiguration" -Tag Unit {
Get-ServerConfiguration | Should -HaveCount 1
}

It "does not remove or save a server when WhatIf is used" {
Remove-ServerConfiguration -Name "Google" -WhatIf

Get-ServerConfiguration | Should -HaveCount 2
(Get-ServerConfiguration).Name | Should -Contain "Google"
Should -Invoke "Save-Configuration" -ModuleName "AtlassianPS.Configuration" -Exactly -Times 0 -Scope It
}

It "removes multiple entries of the servers" {
Get-ServerConfiguration | Should -HaveCount 2

Expand Down
Loading
Loading