commit af19cdc59052827405ad1a56e347687caba69f41
Author: Marc Dequènes (Duck) <Duck@DuckCorp.org>
Date:   Wed Jul 29 02:23:09 2009 +0200

    [fix] code cleanup and a few fixes

diff --git a/bin/shadowwalker b/bin/shadowwalker
index 6ceee51..61d1828 100755
--- a/bin/shadowwalker
+++ b/bin/shadowwalker
@@ -97,7 +97,12 @@ class Command < CmdParse::Command
   protected
 
   def params_shift_location(args)
-    location = args.shift
+    if args.empty?
+      STDERR.puts "syntax error: no location name given"
+      exit 1
+    end
+
+    location = args.shift.downcase
     if location =~ /^([a-zA-Z]+)\/(.+)$/
       loc_obj_hdl = $1
       loc_item_hdl = $2
@@ -105,19 +110,46 @@ class Command < CmdParse::Command
       STDERR.puts "syntax error: bad location"
       exit 1
     end
-    loc_obj_klass = $ldapctl.find_klass(loc_obj_hdl)
-    if loc_obj_klass.nil?
-      STDERR.puts "No such location object '#{loc_obj_hdl}'."
+
+    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]
+  end
+
+  def params_shift_object(args)
+    if args.empty?
+      STDERR.puts "syntax error: no object name given"
+      exit 1
+    end
+
+    obj_hdl = args.shift.downcase.singularize
+    obj_klass = $ldapctl.find_klass(obj_hdl)
+    if obj_klass.nil?
+      STDERR.puts "No such object '#{obj_hdl}'."
       exit 2
     end
+    obj_klass
+  end
+
+  def params_shift_item(obj_klass, args, with_expert = false)
+    if args.empty?
+      STDERR.puts "syntax error: no item name given"
+      exit 1
+    end
+
+    attr_list = ["*"]
+    attr_list << "+" if $program_options[:expert] and with_expert # add operational attributes if expert
+
+    item_hdl = args.shift.downcase
     begin
-      loc_item = loc_obj_klass.find(loc_item_hdl)
+      item = obj_klass.find(item_hdl, :attributes => attr_list)
     rescue ActiveLdap::EntryNotFound
-      STDERR.puts "No such item '#{loc_obj_hdl}/#{loc_item_hdl}'"
+      STDERR.puts "No such item '#{obj_klass.handle}/#{item_hdl}'"
       exit 2
     end
-
-    [loc_obj_klass, loc_item]
+    item
   end
 end
 
@@ -149,15 +181,11 @@ class ListCommand < Command
         puts "  - #{obj_name}"
       end
     else
-      obj_klass = $ldapctl.find_klass(obj_hdl)
-      if obj_klass.nil?
-        STDERR.puts "No such object '#{obj_hdl}'."
-        exit 2
-      end
+      obj_klass = params_shift_object([obj_hdl])
 
       if args.empty?
         obj_human_name = Translator.translate_object_name(obj_hdl)
-        Display.display_item_list("List of #{obj_human_name.pluralize}", obj_klass.find(:all))
+        Display.display_item_list("List of #{obj_human_name.pluralize}", obj_klass.find(:all), $program_options)
       else
         subobj_hdl = args.shift.downcase.singularize
 
@@ -213,29 +241,8 @@ class ShowCommand < Command
   end
 
   def execute (args)
-    if args.size < 1
-      STDERR.puts "syntax error: no object name given"
-      exit 1
-    end
-    if args.size < 2
-      STDERR.puts "syntax error: no item name given"
-      exit 1
-    end
-
-    obj_hdl = args.shift
-    obj_klass = $ldapctl.find_klass(obj_hdl)
-    if obj_klass.nil?
-      STDERR.puts "No such object '#{obj_hdl}'."
-      exit 2
-    end
-
-    item_hdl = args.shift
-    begin
-      item = obj_klass.find(item_hdl, :attributes => ["*", "+"])
-    rescue ActiveLdap::EntryNotFound
-      STDERR.puts "No such item '#{obj_hdl}/#{item_hdl}'"
-      exit 2
-    end
+    obj_klass = params_shift_object(args)
+    item = params_shift_item(obj_klass, args, true)
 
     if args.empty?
       $program_options[:skip_binary] = true
@@ -245,11 +252,11 @@ class ShowCommand < Command
       field_name, file_no = field_info.split("#")
 
       unless item.has_field?(field_name)
-        STDERR.puts "No such field '#{field_name}' in object '#{obj_hdl}'."
+        STDERR.puts "No such field '#{field_name}' in object '#{obj_klass.handle}'."
         exit 2
       end
       unless item.attribute_present?(field_name)
-        STDERR.puts "Field '#{field_name}' in item '#{obj_hdl}/#{item_hdl}' is not present."
+        STDERR.puts "Field '#{field_name}' in item '#{obj_klass.handle}/#{item.name}' is not present."
         exit 2
       end
 
@@ -266,17 +273,17 @@ class ShowCommand < Command
 
         field_single = attr_info.single_value?
         if file_no.nil? and not field_single
-          STDERR.puts "Field '#{field_name}' in object '#{obj_hdl}' has multiple values, but you didn't select one."
+          STDERR.puts "Field '#{field_name}' in object '#{obj_klass.handle}' has multiple values, but you didn't select one."
           exit 2
         end
 
         file_no = 0 if file_no.nil?
         if file_no > 0 and field_single
-          STDERR.puts "Field '#{field_name}' in object '#{obj_hdl}' has only a single value"
+          STDERR.puts "Field '#{field_name}' in object '#{obj_klass.handle}' has only a single value"
           exit 2
         end
         if file_no < 0 or file_no >= field_data.size
-          STDERR.puts "Field '#{field_name}' in object '#{obj_hdl}' doesn't have such file number '#{file_no}' (select in range [0, #{field_data.size - 1}])"
+          STDERR.puts "Field '#{field_name}' in object '#{obj_klass.handle}' doesn't have such file number '#{file_no}' (select in range [0, #{field_data.size - 1}])"
           exit 2
         end
 
@@ -345,7 +352,7 @@ class TreeCommand < Command
       gconfig = $ldapctl.get_global_config()
 
       (gconfig[:tree_objects] || []).each do |obj_hdl|
-        obj_klass = $ldapctl.find_klass(obj_hdl)
+        obj_klass = $ldapctl.find_klass(obj_hdl.downcase)
         unless obj_klass
           STDERR.puts "Location object '#{obj_hdl}' not defined."
           exit 2
@@ -387,29 +394,8 @@ class ModCommand < Command
   end
 
   def execute (args)
-    if args.size < 1
-      STDERR.puts "syntax error: no object name given"
-      exit 1
-    end
-    if args.size < 2
-      STDERR.puts "syntax error: no item name given"
-      exit 1
-    end
-
-    obj_hdl = args.shift
-    obj_klass = $ldapctl.find_klass(obj_hdl)
-    if obj_klass.nil?
-      STDERR.puts "No such object '#{obj_hdl}'."
-      exit 2
-    end
-
-    item_hdl = args.shift
-    begin
-      item = obj_klass.find(item_hdl)
-    rescue ActiveLdap::EntryNotFound
-      STDERR.puts "No such item '#{obj_hdl}/#{item_hdl}'"
-      exit 2
-    end
+    obj_klass = params_shift_object(args)
+    item = params_shift_item(obj_klass, args)
 
     modification_done = false
     args.each do |mod_info|
@@ -433,7 +419,7 @@ class ModCommand < Command
       case type
       when nil
         unless item.has_field?(field)
-          STDERR.puts "No such field '#{field}' in object '#{obj_hdl}'." 
+          STDERR.puts "No such field '#{field}' in object '#{obj_klass.handle}'." 
           exit 2
         end
 
@@ -456,11 +442,10 @@ class ModCommand < Command
             exit 2
           end
         end
-
         old_val = item.send(field, true)
       when 'rel'
         unless item.relations.include?(field)
-          STDERR.puts "No such relation '#{field}' for object '#{obj_hdl}'." 
+          STDERR.puts "No such relation '#{field}' for object '#{obj_klass.handle}'." 
           exit 2
         end
 
@@ -485,7 +470,7 @@ class ModCommand < Command
         case field
         when 'aspects'
           unless item.class.possible_aspects.include?(val)
-            STDERR.puts "No such aspect '#{val}' for object '#{obj_hdl}'."
+            STDERR.puts "No such aspect '#{val}' for object '#{obj_klass.handle}'."
             exit 2
           end
         else
@@ -590,29 +575,8 @@ class DelCommand < Command
   end
 
   def execute (args)
-    if args.size < 1
-      STDERR.puts "syntax error: no object name given"
-      exit 1
-    end
-    if args.size < 2
-      STDERR.puts "syntax error: no item name given"
-      exit 1
-    end
-
-    obj_hdl = args.shift
-    obj_klass = $ldapctl.find_klass(obj_hdl)
-    if obj_klass.nil?
-      STDERR.puts "No such object '#{obj_hdl}'."
-      exit 2
-    end
-
-    item_hdl = args.shift
-    begin
-      item = obj_klass.find(item_hdl, :attributes => ["*", "+"])
-    rescue ActiveLdap::EntryNotFound
-      STDERR.puts "No such item '#{obj_hdl}/#{item_hdl}'"
-      exit 2
-    end
+    obj_klass = params_shift_object(args)
+    item = params_shift_item(obj_klass, args)
 
     if $program_options[:recursive_delete]
       item.class.delete_all(nil, :scope => :sub, :base => item.dn)
@@ -636,27 +600,13 @@ class CreateCommand < Command
   end
 
   def execute (args)
-    if args.size < 1
-      STDERR.puts "syntax error: no object name given"
-      exit 1
-    end
-    if args.size < 2
+    obj_klass = params_shift_object(args)
+
+    if args.empty?
       STDERR.puts "syntax error: no item name given"
       exit 1
     end
-    if args.size < 3
-      STDERR.puts "syntax error: no location name given"
-      exit 1
-    end
-
-    obj_hdl = args.shift
-    obj_klass = $ldapctl.find_klass(obj_hdl)
-    if obj_klass.nil?
-      STDERR.puts "No such object '#{obj_hdl}'."
-      exit 2
-    end
-
-    item_hdl = args.shift
+    item_hdl = args.shift.downcase
 
     loc_obj_klass, loc_item = params_shift_location(args)
 
@@ -678,7 +628,7 @@ class CreateCommand < Command
       field = key
 
       unless item.has_field?(field)
-        STDERR.puts "No such field '#{field}' in object '#{obj_hdl}'."
+        STDERR.puts "No such field '#{field}' in object '#{obj_klass.handle}'."
         exit 2
       end
 
diff --git a/lib/ldap_shadows/display_utils.rb b/lib/ldap_shadows/display_utils.rb
index 37901e1..74160ab 100644
--- a/lib/ldap_shadows/display_utils.rb
+++ b/lib/ldap_shadows/display_utils.rb
@@ -51,7 +51,7 @@ module LdapShadows
   module Display
     def self.display_fields(attr_data, options = {})
       attr_data.each_pair do |key, val|
-        next if attr_data[:expert] and options[:expert]
+        next if val[:expert] and not options[:expert]
 
         field_name = Translator.translate_field_name(key)
 
@@ -99,11 +99,11 @@ module LdapShadows
       end
     end
 
-    def self.display_item_list(title, item_list)
+    def self.display_item_list(title, item_list, options)
       puts "=== #{title} (#{item_list.size}) ==="
       item_list.each do |item|
         str = item.human_name
-        str += " [#{item.name}]" if $program_options[:handles]
+        str += " [#{item.name}]" if options[:handles]
         str += ": #{item.description}" unless item.description.empty?
         puts str
       end
