internal/configurationschemata/default.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
Register-PSFConfigSchema -Name Default -Schema {
    param (
        [string]
        $Resource,
        
        [System.Collections.Hashtable]
        $Settings
    )
    
    #region Converting parameters
    $Peek = $Settings["Peek"]
    $ExcludeFilter = $Settings["ExcludeFilter"]
    $IncludeFilter = $Settings["IncludeFilter"]
    $AllowDelete = $Settings["AllowDelete"]
    $EnableException = $Settings["EnableException"]
    Set-Location -Path $Settings["Path"]
    $PassThru = $Settings["PassThru"]
    #endregion Converting parameters
    
    #region Utility Function
    function Read-PsfConfigFile
    {
<#
    .SYNOPSIS
        Reads a configuration file and parses it.
     
    .DESCRIPTION
        Reads a configuration file and parses it.
     
    .PARAMETER Path
        The path to the file to parse.
     
    .PARAMETER WebLink
        The link to a website to download straight as raw json.
     
    .PARAMETER RawJson
        Raw json data to interpret.
     
    .EXAMPLE
        PS C:\> Read-PsfConfigFile -Path config.json
     
        Reads the config.json file and returns interpreted configuration objects.
#>

        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true, ParameterSetName = 'Path')]
            [string]
            $Path,
            
            [Parameter(Mandatory = $true, ParameterSetName = 'Weblink')]
            [string]
            $Weblink,
            
            [Parameter(Mandatory = $true, ParameterSetName = 'RawJson')]
            [string]
            $RawJson
        )
        
        #region Utility Function
        function New-ConfigItem
        {
            [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
            [CmdletBinding()]
            param (
                $FullName,
                
                $Value,
                
                $Type,
                
                [switch]
                $KeepPersisted,
                
                [switch]
                $Enforced,
                
                [switch]
                $Policy
            )
            
            [pscustomobject]@{
                FullName      = $FullName
                Value          = $Value
                Type          = $Type
                KeepPersisted = $KeepPersisted
                Enforced      = $Enforced
                Policy          = $Policy
            }
        }
        
        function Get-WebContent
        {
            [CmdletBinding()]
            param (
                [string]
                $WebLink
            )
            
            $webClient = New-Object System.Net.WebClient
            $webClient.Encoding = [System.Text.Encoding]::UTF8
            $webClient.DownloadString($WebLink)
        }
        #endregion Utility Function
        
        if ($Path)
        {
            if (-not (Test-Path $Path)) { return }
            $data = Get-Content -Path $Path -Encoding UTF8 -Raw | ConvertFrom-Json -ErrorAction Stop
        }
        if ($Weblink)
        {
            $data = Get-WebContent -WebLink $Weblink | ConvertFrom-Json -ErrorAction Stop
        }
        if ($RawJson)
        {
            $data = $RawJson | ConvertFrom-Json -ErrorAction Stop
        }
        
        foreach ($item in $data)
        {
            #region No Version
            if (-not $item.Version)
            {
                New-ConfigItem -FullName $item.FullName -Value ([PSFramework.Configuration.ConfigurationHost]::ConvertFromPersistedValue($item.Value, $item.Type))
            }
            #endregion No Version
            
            #region Version One
            if ($item.Version -eq 1)
            {
                if ((-not $item.Style) -or ($item.Style -eq "Simple")) { New-ConfigItem -FullName $item.FullName -Value $item.Data }
                else
                {
                    if (($item.Type -eq "Object") -or ($item.Type -eq 12))
                    {
                        New-ConfigItem -FullName $item.FullName -Value $item.Value -Type "Object" -KeepPersisted
                    }
                    else
                    {
                        New-ConfigItem -FullName $item.FullName -Value ([PSFramework.Configuration.ConfigurationHost]::ConvertFromPersistedValue($item.Value, $item.Type))
                    }
                }
            }
            #endregion Version One
        }
    }
    #endregion Utility Function
    
    try
    {
        if ($Resource -like "http*") { $data = Read-PsfConfigFile -Weblink $Resource -ErrorAction Stop }
        else
        {
            $pathItem = $null
            try { $pathItem = Resolve-PSFPath -Path $Resource -SingleItem -Provider FileSystem }
            catch { }
            if ($pathItem) { $data = Read-PsfConfigFile -Path $pathItem -ErrorAction Stop }
            else { $data = Read-PsfConfigFile -RawJson $Resource -ErrorAction Stop }
        }
    }
    catch { Stop-PSFFunction -String 'Configuration.Schema.Default.ImportFailed' -StringValues $Resource -EnableException $EnableException -Tag 'fail', 'import' -ErrorRecord $_ -Continue -Target $Resource -Cmdlet $Settings["Cmdlet"] }
    
    :element foreach ($element in $data)
    {
        #region Exclude Filter
        foreach ($exclusion in $ExcludeFilter)
        {
            if ($element.FullName -like $exclusion)
            {
                continue element
            }
        }
        #endregion Exclude Filter
        
        #region Include Filter
        if ($IncludeFilter)
        {
            $isIncluded = $false
            foreach ($inclusion in $IncludeFilter)
            {
                if ($element.FullName -like $inclusion)
                {
                    $isIncluded = $true
                    break
                }
            }
            
            if (-not $isIncluded) { continue }
        }
        #endregion Include Filter
        
        if ($Peek) { $element }
        else
        {
            try
            {
                if (-not $element.KeepPersisted) { Set-PSFConfig -FullName $element.FullName -Value $element.Value -EnableException -AllowDelete:$AllowDelete -PassThru:$PassThru }
                else { Set-PSFConfig -FullName $element.FullName -PersistedValue $element.Value -PersistedType $element.Type -AllowDelete:$AllowDelete -PassThru:$PassThru }
            }
            catch
            {
                Stop-PSFFunction -String 'Configuration.Schema.Default.SetFailed' -StringValues $element.FullName -ErrorRecord $_ -EnableException $EnableException -Tag 'fail', 'import' -Continue -Target $Resource -Cmdlet $Settings["Cmdlet"]
            }
        }
    }
}