# File lib/puppet/application/resource.rb, line 38
 38:   def main
 39:     args = command_line.args
 40:     type = args.shift or raise "You must specify the type to display"
 41:     typeobj = Puppet::Type.type(type) or raise "Could not find type #{type}"
 42:     name = args.shift
 43:     params = {}
 44:     args.each do |setting|
 45:       if setting =~ /^(\w+)=(.+)$/
 46:         params[$1] = $2
 47:       else
 48:         raise "Invalid parameter setting #{setting}"
 49:       end
 50:     end
 51: 
 52:     raise "You cannot edit a remote host" if options[:edit] and @host
 53: 
 54:     properties = typeobj.properties.collect { |s| s.name }
 55: 
 56:     format = proc {|trans|
 57:       trans.dup.collect do |param, value|
 58:         if value.nil? or value.to_s.empty?
 59:           trans.delete(param)
 60:         elsif value.to_s == "absent" and param.to_s != "ensure"
 61:           trans.delete(param)
 62:         end
 63: 
 64:         trans.delete(param) unless properties.include?(param) or @extra_params.include?(param)
 65:       end
 66:       trans.to_manifest
 67:     }
 68: 
 69:     if @host
 70:       Puppet::Resource.indirection.terminus_class = :rest
 71:       port = Puppet[:puppetport]
 72:       key = ["https://#{host}:#{port}", "production", "resources", type, name].join('/')
 73:     else
 74:       key = [type, name].join('/')
 75:     end
 76: 
 77:     text = if name
 78:       if params.empty?
 79:         [ Puppet::Resource.find( key ) ]
 80:       else
 81:         [ Puppet::Resource.new( type, name, :parameters => params ).save( key ) ]
 82:       end
 83:     else
 84:       Puppet::Resource.search( key, {} )
 85:     end.map(&format).join("\n")
 86: 
 87:     if options[:edit]
 88:       file = "/tmp/x2puppet-#{Process.pid}.pp"
 89:       begin
 90:         File.open(file, "w") do |f|
 91:           f.puts text
 92:         end
 93:         ENV["EDITOR"] ||= "vi"
 94:         system(ENV["EDITOR"], file)
 95:         system("puppet -v #{file}")
 96:       ensure
 97:         #if FileTest.exists? file
 98:         #    File.unlink(file)
 99:         #end
100:       end
101:     else
102:       puts text
103:     end
104:   end