added script destroy-user-db
[couchdb_scripts.git] / destroy-user-db
1 #!/usr/bin/ruby
2
3 require_relative "lib/leap"
4
5 def main()
6   parse_opts
7   server   = LEAP::Server.new
8   users_db = LEAP::Users.new(server)
9   record   = users_db.find_by_login($username)
10   if record.nil?
11     puts "Username #{$username} does not exist."
12     exit 1
13   end
14   dbname   = "user-#{record.id}"
15   begin
16     server.database(dbname).delete!
17     puts "Deleted DB #{dbname}"
18     exit 0
19   rescue RestClient::ResourceNotFound
20     puts "DB does not exist: #{dbname}"
21     exit 1
22   end
23 end
24
25 def parse_opts()
26   $username = nil
27   while cmd = ARGV.shift
28     case cmd
29       when "--username"
30         $username = ARGV.shift
31       else
32         usage
33     end
34   end
35   unless $username
36     usage
37   end
38 end
39
40 def usage()
41   puts "#{$0} COMMAND"
42   puts "Commands:"
43   puts "  --username USERNAME - destroy the user db corresponding to USERNAME."
44   exit(1)
45 end
46
47 main()