fail gracefully if user does no exist
authorChristoph <ckluente@thoughtworks.com>
Mon, 20 Jun 2016 12:05:06 +0000 (14:05 +0200)
committerChristoph <ckluente@thoughtworks.com>
Mon, 20 Jun 2016 12:05:06 +0000 (14:05 +0200)
destroy-user-db

index 00b17f1..cd17eb5 100755 (executable)
@@ -6,16 +6,16 @@ def main()
   parse_opts
   server   = LEAP::Server.new
   users_db = LEAP::Users.new(server)
-  identities_db = server.database('identities')
-  all_users_db = server.database('users')
   record   = users_db.find_by_login($username)
-  user_id = record.id
   return_code = 0
   if record.nil?
     puts "Username #{$username} does not exist."
-    return_code=1
+    exit 1
   end
+  user_id = record.id
   dbname   = "user-#{user_id}"
+  identities_db = server.database('identities')
+  all_users_db = server.database('users')
   begin
     server.database(dbname).delete!
     puts "Deleted DB #{dbname}"
@@ -25,24 +25,24 @@ def main()
   end
   if $destroy_identities == TRUE
     identities_db.view('Identity/by_user_id', :reduce => false, :startkey => user_id, :endkey => user_id)['rows'].each do |row|
-           begin
-             doc = identities_db.get(row['id'])
-             identities_db.delete_doc(doc)
-             puts "  deleted #{identities_db.name}/#{row['id']}"
-           rescue RestClient::ResourceNotFound
-             puts "identities DB does not exist: #{doc}"
-              return_code=1
-           end
+      begin
+        doc = identities_db.get(row['id'])
+        identities_db.delete_doc(doc)
+        puts "  deleted #{identities_db.name}/#{row['id']}"
+      rescue RestClient::ResourceNotFound
+        puts "identities DB does not exist: #{doc}"
+        return_code=1
+      end
     end 
     all_users_db.view('User/by_login', :reduce => false, :startkey => $username, :endkey => $username)['rows'].each do |row|
       begin
-             doc = all_users_db.get(user_id)
-             all_users_db.delete_doc(doc)
-             puts "  deleted #{user_id} from user db"
-           rescue RestClient::ResourceNotFound
-             puts "user not found in user db: #{user_id}"
-        return_code=1
-           end
+        doc = all_users_db.get(user_id)
+        all_users_db.delete_doc(doc)
+        puts "  deleted #{user_id} from user db"
+      rescue RestClient::ResourceNotFound
+        puts "user not found in user db: #{user_id}"
+          return_code=1
+      end
     end
   end
   exit return_code