commit 38e5c5db43a5c6082ff3f271426f5889810e5b04
Author: Marc Dequènes (Duck) <Duck@DuckCorp.org>
Date:   Sun Oct 25 00:22:04 2009 +0200

    [evol] introduce item 'full-handle' and reorganize command arguments, and renamed commands

diff --git a/bin/shadowwalker b/bin/shadowwalker
index 26672f1..7a5f375 100755
--- a/bin/shadowwalker
+++ b/bin/shadowwalker
@@ -89,22 +89,25 @@ class Command < CmdParse::Command
 
   protected
 
-  def params_shift_location(args)
-    raise SyntaxError, _("no location name given") if args.empty?
-
-    location = args.shift.downcase
-    raise SyntaxError, _("bad location") unless location =~ /^([a-zA-Z]+)\/(.+)$/
-    loc_obj_hdl = $1
-    loc_item_hdl = $2
-
-    split_args = [loc_obj_hdl, loc_item_hdl]
-    loc_obj_klass = params_shift_object(split_args)
-    loc_item = params_shift_item(loc_obj_klass, split_args)
-
-    [loc_obj_klass, loc_item]
+  def params_shift_item_handle_full_handle(args, fetch_item = true, name = "item full-handle")
+    raise SyntaxError, _("no %s given") % name if args.empty?
+
+    full_handle = args.shift.downcase
+    raise SyntaxError, _("bad %s") % name unless full_handle =~ /^([a-zA-Z]+)\/(.+)$/
+    obj_hdl = $1
+    item_hdl = $2
+
+    split_args = [obj_hdl, item_hdl]
+    obj_klass = params_shift_object_handle(split_args)
+    if fetch_item
+      item = params_shift_item_handle(obj_klass, split_args)
+      [obj_klass, item]
+    else
+      [obj_klass, item_hdl]
+    end
   end
 
-  def params_shift_object(args)
+  def params_shift_object_handle(args)
     raise SyntaxError, _("no object name given") if args.empty?
 
     obj_hdl = args.shift.downcase.singularize
@@ -113,7 +116,7 @@ class Command < CmdParse::Command
     obj_klass
   end
 
-  def params_shift_item(obj_klass, args)
+  def params_shift_item_handle(obj_klass, args)
     raise SyntaxError, _("no item name given") if args.empty?
 
     attr_list = ["*"]
@@ -156,7 +159,7 @@ class ListCommand < Command
         puts "  - #{obj_name}"
       end
     else
-      obj_klass = params_shift_object([obj_hdl])
+      obj_klass = params_shift_object_handle([obj_hdl])
 
       if args.empty?
         obj_human_name = Translator.translate_object_name(obj_hdl)
@@ -208,9 +211,9 @@ class ShowCommand < Command
 
     self.short_desc = "show item information"
     self.usages_params = [
-                          "<object> <item>",
-                          "<object> <item> <field>#? [<filename>]",
-                          "<object> <item> <field>[#<index>] [<filename>]"
+                          "<object>/<item>",
+                          "<object>/<item> <field>#? [<filename>]",
+                          "<object>/<item> <field>[#<index>] [<filename>]"
                          ]
     self.options = CmdParse::OptionParserWrapper.new do |opt|
       opt.on( '-f', '--family', 'Display family members.' ) { $program_options[:show_family_members] = true }
@@ -220,8 +223,7 @@ class ShowCommand < Command
   def execute(args)
     super
 
-    obj_klass = params_shift_object(args)
-    item = params_shift_item(obj_klass, args)
+    obj_klass, item = params_shift_item_handle_full_handle(args)
 
     if args.empty?
       $program_options[:skip_binary] = true
@@ -234,8 +236,8 @@ class ShowCommand < Command
         raise PreProcessingError, _("No such field '%s' in object '%s'") % [field_name, obj_klass.handle]
       end
       unless item.attribute_present?(field_name)
-        raise PreProcessingError, _("Field '%s' in item '%s/%s' is not present") %
-          [field_name, obj_klass.handle, item.name]
+        raise PreProcessingError, _("Field '%s' in item '%s' is not present") %
+          [field_name, item.full_handle]
       end
 
       field_data = item.send(field_name, true)
@@ -303,7 +305,7 @@ class TreeCommand < Command
 
     self.short_desc = "show skeleton items tree"
     self.usages_params = [
-                          "[<location>]"
+                          "[<location-item-full-handle>]"
                           ]
     self.options = CmdParse::OptionParserWrapper.new do |opt|
       opt.on( '-a', '--all', 'Display all objects in the tree.' ) { $program_options[:tree_all_objects] = true }
@@ -317,7 +319,7 @@ class TreeCommand < Command
 
     search_base = nil
     unless args.empty?
-      loc_obj_klass, loc_item = params_shift_location(args)
+      loc_obj_klass, loc_item = params_shift_item_handle_full_handle(args, true, "location full-handle")
       search_base = loc_item.dn
     end
 
@@ -328,21 +330,20 @@ class TreeCommand < Command
 end
 cmdparser.add_command(TreeCommand.new)
 
-class ModCommand < Command
+class ModifyCommand < Command
   def initialize
-    super('mod', false)
+    super('modify', false)
 
     self.short_desc = "Modify attributes of an item"
     self.usages_params = [
-                          "<object> <item> (<field>|<relation>|:aspects)(=|+=|-=)<value> [(<field>|<relation>|:aspects)(=|+=|-=)<value>] ..."
+                          "<item-full-handle> (<field>|<relation>|:aspects)(=|+=|-=)<value> [(<field>|<relation>|:aspects)(=|+=|-=)<value>] ..."
                          ]
   end
 
   def execute(args)
     super
 
-    obj_klass = params_shift_object(args)
-    item = params_shift_item(obj_klass, args)
+    obj_klass, item = params_shift_item_handle_full_handle(args)
 
     modification_done = false
     args.each do |mod_info|
@@ -358,15 +359,15 @@ class ModCommand < Command
     end
   end
 end
-cmdparser.add_command(ModCommand.new)
+cmdparser.add_command(ModifyCommand.new)
 
-class DelCommand < Command
+class DeleteCommand < Command
   def initialize
-    super('del', false)
+    super('delete', false)
 
     self.short_desc = "Delete an item"
     self.usages_params = [
-                          "<object> <item>"
+                          "<item-full-handle>"
                          ]
     self.options = CmdParse::OptionParserWrapper.new do |opt|
       opt.on( '-r', '--recursive', 'Delete object and all children recursively.' ) { $program_options[:recursive_delete] = true }
@@ -376,8 +377,7 @@ class DelCommand < Command
   def execute(args)
     super
 
-    obj_klass = params_shift_object(args)
-    item = params_shift_item(obj_klass, args)
+    obj_klass, item = params_shift_item_handle_full_handle(args)
 
     if $program_options[:recursive_delete]
       item.delete_recursive
@@ -388,7 +388,7 @@ class DelCommand < Command
     puts "Deletion done."
   end
 end
-cmdparser.add_command(DelCommand.new)
+cmdparser.add_command(DeleteCommand.new)
 
 class CreateCommand < Command
   def initialize
@@ -396,19 +396,15 @@ class CreateCommand < Command
 
     self.short_desc = "Create an item"
     self.usages_params = [
-                          "<object> <new_item> <location> [<field>=<value>] ..."
+                          "<new-item-full-handle> <parent-item-full-handle> [<field>=<value>] ..."
                          ]
   end
 
   def execute(args)
     super
 
-    obj_klass = params_shift_object(args)
-
-    raise SyntaxError, _("no item name given") if args.empty?
-    item_hdl = args.shift.downcase
-
-    loc_obj_klass, loc_item = params_shift_location(args)
+    obj_klass, item_hdl = params_shift_item_handle_full_handle(args)
+    loc_obj_klass, loc_item = params_shift_item_handle_full_handle(args, true, "parent full-handle")
 
     item = obj_klass.new(item_hdl)
     item.base = loc_item.dn_obj - obj_klass.base_obj
diff --git a/lib/ldap_shadows/display_utils.rb b/lib/ldap_shadows/display_utils.rb
index 3362073..51dad27 100644
--- a/lib/ldap_shadows/display_utils.rb
+++ b/lib/ldap_shadows/display_utils.rb
@@ -70,7 +70,7 @@ module LdapShadows
     def self.display_item(item, options = {})
       obj_human_name = Translator.translate_object_name(item.class.handle)
       name = item.human_name
-      name += " [#{item.name}]" if options[:handles]
+      name += " [#{item.full_handle}]" if options[:handles]
       puts "=== #{obj_human_name}: #{name} ==="
 
       if options[:debug]
@@ -123,7 +123,7 @@ module LdapShadows
       puts "=== #{title} (#{item_list.size}) ==="
       item_list.each do |item|
         str = item.human_name
-        str += " [#{item.name}]" if options[:handles]
+        str += " [#{item.full_handle}]" if options[:handles]
         str += ": #{item.human_description}" unless item.human_description.empty?
         puts str
       end
diff --git a/lib/ldap_shadows/elements/object.rb b/lib/ldap_shadows/elements/object.rb
index 9e586f6..20b6a44 100755
--- a/lib/ldap_shadows/elements/object.rb
+++ b/lib/ldap_shadows/elements/object.rb
@@ -31,6 +31,16 @@ module Elements
     # default
     ldap_mapping :prefix => '', :classes => ['top'], :scope => :sub
 
+    def handle
+      name = self[dn_attribute] || self.attributes[dn_attribute] || self.dn
+      name = name[0] if name.is_a? Array
+      name.strip
+    end
+
+    def full_handle
+      "#{self.class.handle}/#{self.handle}"
+    end
+
     def self.cast
       super
 
@@ -78,12 +88,6 @@ module Elements
       instance_variable_set(:@relations_info, object_relations_info)
     end
 
-    def name
-      name = self[dn_attribute] || self.attributes[dn_attribute] || self.dn
-      name = name[0] if name.is_a? Array
-      name.strip
-    end
-
     def has_field?(field)
       return false if field.downcase == "objectclass"
       has_attribute?(field)
@@ -520,7 +524,7 @@ module Elements
       if obj_hdl
         obj_klass = shadow.get_object(obj_hdl)
         item = obj_klass.new(raw_item.dn)
-        {:name => "#{obj_hdl}/#{item.name}", :item => item, :object => obj_klass}
+        {:name => item.full_handle, :item => item, :object => obj_klass}
       else
         {:name => "unknown/#{raw_item.dn}"}
       end
@@ -708,13 +712,13 @@ module Elements
           if data.empty?
             value = nil
           else
-            value = data.collect{|g| g.name }
+            value = data.collect{|g| g.handle }
             multiple = true
           end
         else
           # the exists? method also ensure the object is loaded
           if data.exists?
-            value = data.name
+            value = data.handle
           else
             value = nil
           end
