@@ -90,4 +90,90 @@ function CompatibleConvertFrom-Yaml {
90
90
else {
91
91
return ConvertFrom-Yaml $content
92
92
}
93
+ }
94
+
95
+ <#
96
+ . SYNOPSIS
97
+ Common function that will verify that the YmlFile being loaded exists, load the raw file and
98
+ return the results of CompatibleConvertFrom-Yaml or report an exception and return null if
99
+ there's a problem loading the yml file. The return is the PowerShell HashTable object.
100
+
101
+ . DESCRIPTION
102
+ Common function that will verify that the YmlFile being loaded exists, load the raw file and
103
+ return the results of CompatibleConvertFrom-Yaml or report an exception and return null if
104
+ there's a problem loading the yml file. This is just to save anyone needing to load yml from
105
+ having to deal with checking the file's existence and ensure that the CompatibleConvertFrom-Yaml
106
+ is made within a try/catch. The return is the PowerShell HashTable object from the
107
+ CompatibleConvertFrom-Yaml call or $null if there was an issue with the convert.
108
+
109
+ . PARAMETER YmlFile
110
+ The full path of the yml file to load.
111
+
112
+ . EXAMPLE
113
+ LoadFrom-Yaml -YmlFile path/to/file.yml
114
+ #>
115
+ function LoadFrom-Yaml {
116
+ param (
117
+ [Parameter (Mandatory = $true )]
118
+ [string ]$YmlFile
119
+ )
120
+ if (Test-Path - Path $YmlFile ) {
121
+ try {
122
+ return Get-Content - Raw - Path $YmlFile | CompatibleConvertFrom- Yaml
123
+ }
124
+ catch {
125
+ Write-Host " LoadFrom-Yaml::Exception while parsing yml file $ ( $YmlFile ) : $_ "
126
+ }
127
+ }
128
+ else {
129
+ Write-Host " LoadFrom-Yaml::YmlFile '$YmlFile ' does not exist."
130
+ }
131
+ return $null
132
+ }
133
+
134
+ <#
135
+ . SYNOPSIS
136
+ Given the Hashtable contents of a Yml file and an array of strings representing the keys
137
+ return the value if it exist or null if it doesn't.
138
+
139
+ . DESCRIPTION
140
+ The Yaml file needs to be loaded via CompatibleConvertFrom-Yaml which returns the file as
141
+ as hashtable. The Keys are basically the path in the yaml file whose value to return, or
142
+ null if it doesn't exist. This function safely traverses the path, outputting an error
143
+ if there's an issue or returning the object representing the result if successful. This
144
+ function loops through the Keys safely trying to get values, checking each piece of the
145
+ path to ensure it exists. Normally one would just do
146
+ $Yml["extends"]["parameters"]["artifacts"]
147
+ but if something was off it would throw. Doing it this way allows more succinct error
148
+ reporting if a piece of the path didn't exist
149
+
150
+ . PARAMETER YamlContentAsHashtable
151
+ The hashtable representing the yaml file contents loaded through LoadFrom-Yaml
152
+ or CompatibleConvertFrom-Yaml, which is what LoadFrom-Yaml calls.
153
+
154
+ . PARAMETER Keys
155
+ String array representation of the path in the yaml file whose value we're trying to retrieve.
156
+
157
+ . EXAMPLE
158
+ GetValueSafelyFrom-Yaml -YamlContentAsHashtable $YmlFileContent -Keys @("extends", "parameters", "Artifacts")
159
+ #>
160
+ function GetValueSafelyFrom-Yaml {
161
+ param (
162
+ [Parameter (Mandatory = $true )]
163
+ $YamlContentAsHashtable ,
164
+ [Parameter (Mandatory = $true )]
165
+ [string []]$Keys
166
+ )
167
+ $current = $YamlContentAsHashtable
168
+ foreach ($key in $Keys ) {
169
+ if ($current.ContainsKey ($key ) -or $current [$key ]) {
170
+ $current = $current [$key ]
171
+ }
172
+ else {
173
+ Write-Host " The '$key ' part of the path $ ( $Keys -join " /" ) doesn't exist or is null."
174
+ return $null
175
+ }
176
+ }
177
+
178
+ return [object ]$current
93
179
}
0 commit comments