Module: Fog::Hyperv::Utils::Powershell
- Defined in:
- lib/fog/hyperv/utils/powershell.rb
Class Method Summary collapse
-
.build_call(cmdlet, args = {}, _ps_version: 5) ⇒ Object
Build a Powershell cmdlet call.
-
.build_optmap(_optmap_name: 'FogArgs', _ps_version: 5, _from_json: true, **_options) ⇒ Object
Build a Powershell option map from a set of keyword arguments.
-
.shell_quote(data, always: false) ⇒ Object
Convert Ruby data to PowerShell equivalents.
Class Method Details
.build_call(cmdlet, args = {}, _ps_version: 5) ⇒ Object
Build a Powershell cmdlet call
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fog/hyperv/utils/powershell.rb', line 41 def self.build_call(cmdlet, args = {}, _ps_version: 5) return [cmdlet.gsub('@Args', '')].flatten if args.empty? id = (cmdlet.hash ^ args.hash).abs.to_s(36) optmap = "Fog#{id}" commands = [] commands += build_optmap(_optmap_name: optmap, _ps_version:, **args) commands << if cmdlet.include? '@Args' cmdlet.gsub('@Args', "@#{optmap}") else "#{cmdlet} @#{optmap}" end commands end |
.build_optmap(_optmap_name: 'FogArgs', _ps_version: 5, _from_json: true, **_options) ⇒ Object
Build a Powershell option map from a set of keyword arguments
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fog/hyperv/utils/powershell.rb', line 9 def self.build_optmap(_optmap_name: 'FogArgs', _ps_version: 5, _from_json: true, **) ps_opts = Fog::Hyperv.camelize() if _from_json if _ps_version >= 6 ["$#{_optmap_name} = ConvertFrom-Json -AsHashtable '#{Fog::JSON.encode ps_opts}'"] else [ [ "$FogJsonObject = '#{Fog::JSON.encode ps_opts}'", '$FogJsonParameters = ConvertFrom-Json -InputObject $FogJsonObject', "$#{_optmap_name} = @{}", "$FogJsonParameters.psobject.properties | Foreach { $#{_optmap_name}[$_.Name] = $_.Value }" ].join('; ') ] end else args = ps_opts.map do |k, v| "'#{k}'=#{Fog::Hyperv.shell_quote(v, always: true)}" end ["$#{_optmap_name} = @{#{args.join ';'}}"] end end |
.shell_quote(data, always: false) ⇒ Object
Convert Ruby data to PowerShell equivalents
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 |
# File 'lib/fog/hyperv/utils/powershell.rb', line 60 def self.shell_quote(data, always: false) case data when String if !data.start_with?('$') && (data =~ /(^$)|\s/ || always) data.gsub('`', '``') .gsub(/\0/, '`0') .gsub("\n", '`n') .gsub("\r", '`r') .inspect .gsub('\"', '`"') .gsub('\\\\', '\\') else data end when Hash raise 'Hashes need to be run through build_optmap' when Array "@(#{data.map { |e| shell_quoted(e, always: true) }.join(', ')})" when FalseClass '$false' when TrueClass '$true' when nil '$null' else shell_quoted data.to_s end end |