diff options
author | Tor Magnus Rakvåg <tm@intility.no> | 2018-06-13 10:38:07 +0200 |
---|---|---|
committer | Tor Magnus Rakvåg <tm@intility.no> | 2018-06-13 10:38:07 +0200 |
commit | 4b4cd8ae20cc56e2881ef38b9d5679a0552796ab (patch) | |
tree | 7e02b73c25c13b614627a6f6802b32279dd8267b | |
parent | b49ca16636b77426e615adb30cc106d961575572 (diff) | |
download | chef-4b4cd8ae20cc56e2881ef38b9d5679a0552796ab.tar.gz |
refactor state fetching to use json
Signed-off-by: Tor Magnus Rakvåg <tm@intility.no>
-rw-r--r-- | lib/chef/resource/powershell_package_source.rb | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/lib/chef/resource/powershell_package_source.rb b/lib/chef/resource/powershell_package_source.rb index 58a831a698..095adcb292 100644 --- a/lib/chef/resource/powershell_package_source.rb +++ b/lib/chef/resource/powershell_package_source.rb @@ -15,6 +15,8 @@ # limitations under the License. # +require "chef/json_compat" + class Chef class Resource class PowershellPackageSource < Chef::Resource @@ -55,13 +57,9 @@ class Chef load_current_value do cmd = load_resource_state_script(name) repo = powershell_out!(cmd) - status = {} - repo.stdout.split(/\r\n/).each do |line| - kv = line.strip.split(/\s*:\s*/, 2) - status[kv[0]] = kv[1] if kv.length == 2 - end + status = Chef::JSONCompat.from_json(repo.stdout) url status["url"].nil? ? "not_set" : status["url"] - trusted (status["trusted"] == "True" ? true : false) + trusted status["trusted"] provider_name status["provider_name"] publish_location status["publish_location"] script_source_location status["script_source_location"] @@ -145,14 +143,19 @@ class Chef def load_resource_state_script(name) <<-EOH - if ((Get-PackageSource -Name '#{name}' -ErrorAction SilentlyContinue).ProviderName -eq 'PowerShellGet') { - (Get-PSRepository -Name '#{name}') | Select @{n='name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}}, - @{n='trusted';e={$_.Trusted}}, @{n='provider_name';e={$_.PackageManagementProvider}}, @{n='publish_location';e={$_.PublishLocation}}, - @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}} | fl + if(Get-PackageSource -Name '#{name}' -ErrorAction SilentlyContinue) { + if ((Get-PackageSource -Name '#{name}').ProviderName -eq 'PowerShellGet') { + (Get-PSRepository -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.SourceLocation}}, + @{n='trusted';e={$_.Trusted}}, @{n='provider_name';e={$_.PackageManagementProvider}}, @{n='publish_location';e={$_.PublishLocation}}, + @{n='script_source_location';e={$_.ScriptSourceLocation}}, @{n='script_publish_location';e={$_.ScriptPublishLocation}} | ConvertTo-Json + } + else { + (Get-PackageSource -Name '#{name}') | Select @{n='source_name';e={$_.Name}}, @{n='url';e={$_.Location}}, + @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} | ConvertTo-Json + } } else { - (Get-PackageSource -Name '#{name}'-ErrorAction SilentlyContinue) | Select @{n='name';e={$_.Name}}, @{n='url';e={$_.Location}}, - @{n='provider_name';e={$_.ProviderName}}, @{n='trusted';e={$_.IsTrusted}} | fl + "" | Select source_name, url, provider_name, trusted | ConvertTo-Json } EOH end |