Skip to content
This repository was archived by the owner on Jul 23, 2022. It is now read-only.

Commit 1ceaace

Browse files
authored
Add persistence path override, tests, and update documentation (#8)
* Import: Add persistence for just API key and code improvements * Persistence: Add path override using environment variable * Tests: Add tests for Enable-XRefPersistence * Docs: Update documentation * Legacy compatibility of hiding Warning stream * Improve persistence path override behavior
1 parent 142f74a commit 1ceaace

11 files changed

+606
-156
lines changed
Lines changed: 16 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function Import-XRefData {
3535

3636
$MappingWarning = $false
3737
$ConflictingData = $false
38+
$NewKeyAdded = $false
3839

3940
}
4041

@@ -63,85 +64,21 @@ function Import-XRefData {
6364
# Process ApiKey (Check parameter set first since ConfigStaging won't exist in the ApiKey set)
6465
if ($PSCmdlet.ParameterSetName -eq "ApiKey" -or ($ImportStaging.psobject.Properties.Name -contains "ApiKey" -and -not [string]::IsNullOrWhiteSpace($ImportStaging.ApiKey))) {
6566

66-
# Check if current API key is not set
67-
if ([string]::IsNullOrWhiteSpace($script:TwitchData.ApiKey)) {
68-
# API key is not set
67+
if ($PSCmdlet.ShouldProcess("API key", "Import")) {
6968

70-
# Specify "Import" since there's nothing being replaced
71-
if ($PSCmdlet.ShouldProcess("API key", "Import")) {
69+
# Get key from parameter or input object
70+
$script:TwitchData.ApiKey = ($PSCmdlet.ParameterSetName -eq "ApiKey") ? $ApiKey : $ImportStaging.ApiKey
71+
$NewKeyAdded = $true
7272

73-
if ($PSCmdlet.ParameterSetName -eq "ApiKey") {
73+
if (-not $Quiet) {
7474

75-
# Handling import via ApiKey parameter
76-
$script:TwitchData.ApiKey = $ApiKey
77-
78-
if (-not $Quiet) {
79-
80-
Write-Host "API key imported."
81-
82-
}
83-
84-
return
85-
86-
}
87-
else {
88-
89-
# Import API key from input object
90-
$script:TwitchData.ApiKey = $ImportStaging.ApiKey
91-
92-
if (-not $Quiet) {
93-
94-
Write-Host "API key imported."
95-
96-
}
97-
98-
}
75+
Write-Host "API key imported."
9976

10077
}
10178

102-
}
103-
else {
104-
# API key already exists
105-
10679
if ($PSCmdlet.ParameterSetName -eq "ApiKey") {
10780

108-
# Get key via ApiKey parameter
109-
$NewApiKey = $ApiKey
110-
111-
}
112-
else {
113-
114-
# Get key from input object
115-
$NewApiKey = $ImportStaging.ApiKey
116-
117-
}
118-
119-
# Check if new key is different
120-
if ($script:TwitchData.ApiKey -ine $NewApiKey) {
121-
122-
# Specify "Replace" since previous value will be replaced
123-
if ($PSCmdlet.ShouldProcess("API key", "Replace")) {
124-
125-
$script:TwitchData.ApiKey = $NewApiKey
126-
127-
if (-not $Quiet) {
128-
129-
Write-Host "API key replaced."
130-
131-
}
132-
133-
if ($PSCmdlet.ParameterSetName -eq "ApiKey") {
134-
135-
return
136-
137-
}
138-
139-
}
140-
141-
}
142-
elseif (-not $Quiet) {
143-
144-
Write-Host "API key is unchanged."
81+
return
14582

14683
}
14784

@@ -435,7 +372,7 @@ function Import-XRefData {
435372
if ($PSCmdlet.ParameterSetName -eq "General") {
436373

437374
if ($ConflictingData) {
438-
Write-Error "Some data conflicts with existing values. Run with -Force to overwrite."
375+
Write-Error "Some lookup data conflicts with existing values. Run with -Force to overwrite."
439376
}
440377

441378
if ($MappingWarning) {
@@ -448,7 +385,7 @@ function Import-XRefData {
448385

449386
}
450387

451-
if ($Persist -and $Counters.AllImported -gt 0) {
388+
if ($Persist -and ($Counters.AllImported -gt 0 -or $NewKeyAdded)) {
452389

453390
if (Get-EventSubscriber -SourceIdentifier XRefNewDataAdded -Force -ErrorAction Ignore) {
454391

@@ -463,9 +400,13 @@ function Import-XRefData {
463400
return $Counters
464401

465402
}
466-
else {
467403

468-
return
404+
}
405+
elseif ($Persist -and $NewKeyAdded) {
406+
407+
if (Get-EventSubscriber -SourceIdentifier XRefNewDataAdded -Force -ErrorAction Ignore) {
408+
409+
[void] (New-Event -SourceIdentifier XRefNewDataAdded -Sender "Import-XRefData")
469410

470411
}
471412

0 commit comments

Comments
 (0)