Skip to content

Commit

Permalink
Add initial settings value validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jvlflame committed Jul 12, 2020
1 parent a34f0f0 commit d7d5b34
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
140 changes: 140 additions & 0 deletions src/Javinizer/Private/Validate-Settings.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
function Test-Settings {
[CmdletBinding()]
param (
[string]$Path
)

$boolSettings = @(
'scrape-r18',
'scrape-r18zh',
'scrape-dmm',
'scrape-javlibrary',
'scrape-javlibraryja',
'scrape-javlibraryzh',
'scrape-javbus',
'scrape-javbusja',
'scrape-jav321',
'scrape-actress-en',
'scrape-actress-ja',
'move-to-folder',
'rename-file',
'regex-match',
'create-nfo',
'create-nfo-per-file',
'download-thumb-img',
'download-poster-img',
'download-trailer-vid',
'download-actress-img',
'translate-description',
'add-series-as-tag',
'first-last-name-order',
'convert-alias-to-originalname'
'normalize-genres',
'set-owned',
'check-updates',
'verbose-shell-output',
'debug-shell-output'
)

$stringSettings = @(
'rename-file-string',
'rename-folder-string',
'cms-displayname-string',
'poster-file-string',
'thumbnail-file-string',
'trailer-file-string',
'nfo-file-string',
'screenshot-folder-string',
'screenshot-img-string',
'actor-folder-string'
)

$javlibrarySettings = @(
'set-owned',
'username',
'session-cookie',
'requeste-timeout-sec'
)

$validTags = @(
'ID',
'TITLE',
'STUDIO',
'YEAR',
'RELEASEDATE',
'RUNTIME',
'ACTORS',
'LABEL',
'ORIGINALTITLE',
'SET'
)

$errorObject = @()

$settingObject = @()
$settingsContent = Get-Content -LiteralPath $Path
$settingsContent | ForEach-Object {
if ($_ -match '^[a-zA-Z]') {
$entry = [pscustomobject]@{
Name = ($_ -split '=')[0]
Value = ($_ -split '=')[1]
}

$settingObject += $entry
}
}

foreach ($setting in $settingObject) {
if ($setting.Name -in $boolSettings) {
if ($setting.Value -ne 'True' -and $setting.Value -ne 'False') {
$entry = [PSCustomObject]@{
Name = $setting.Name
Value = $setting.Value
Type = 'Boolean'
}
$errorObject += $entry
}
}

<#
if ($setting.Name -in $stringSettings) {
$tags = (($Setting.Value | Select-String '<(.*?)>' -AllMatches).Matches.Groups | Where-Object { $_.Name -eq 1 }).Value
foreach ($tag in $tags) {
if ($tag -notin $validTags) {
$entry = [PSCustomObject]@{
Name = $setting.Name
Value = $setting.Value
Type = 'String'
}
$errorObject += $entry
}
}
}
#>

if ($setting.Name -eq 'multi-sort-throttle-limit') {
if ([int]$setting.Value -lt 1 -or [int]$setting.Value -gt 15) {
$entry = [PSCustomObject]@{
Name = $setting.Name
Value = $setting.Value
Type = 'Multi'
}
$errorObject += $entry
}
}
}

foreach ($err in $errorObject) {
if ($err.Type -eq 'Boolean') {
Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error validating setting [$($err.Name)] with value [$($err.Value)], value must match [True / False]"
}

if ($err.Type -eq 'String') {
Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error validating setting [$($err.Name)] with value [$($err.Value)], value must match available tags"
}

if ($err.Type -eq 'Multi') {
Write-Error "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Error validating setting [$($err.Name)] with value [$($err.Value)], value must match [MIN: 1 / MAX: 15]"
}
}
}
6 changes: 4 additions & 2 deletions src/Javinizer/Public/Javinizer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ function Javinizer {
throw "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Unable to load settings from path: $settingsPath"
}

Test-Settings -Path $settingsPath -ErrorAction Stop

if (($settings.Other.'log-path' -eq '') -or ($null -eq $settings.Other.'log-path')) {
$global:javinizerLogPath = Join-Path -Path $ScriptRoot -ChildPath javinizer.log
} else {
Expand Down Expand Up @@ -427,7 +429,7 @@ function Javinizer {
}

process {
# Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Function started"
Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Function started"
Write-Debug "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] R18: [$R18]; R18Zh: [$R18Zh] Dmm: [$Dmm]; Javlibrary: [$Javlibrary]; JavlibraryZh: [$JavlibraryZh]; JavlibraryJa: [$JavlibraryJa]; Javbus: [$Javbus]; JavbusJa: [$JavbusJa]; Jav321: [$Jav321]"
switch ($PsCmdlet.ParameterSetName) {
'Info' {
Expand Down Expand Up @@ -868,6 +870,6 @@ function Javinizer {
}

end {
# Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Function ended"
Write-Host "[$(Get-TimeStamp)][$($MyInvocation.MyCommand.Name)] Function ended"
}
}

0 comments on commit d7d5b34

Please sign in to comment.