Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created October 24, 2014 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rklemme/cc930f9f373489c0d9ce to your computer and use it in GitHub Desktop.
Save rklemme/cc930f9f373489c0d9ce to your computer and use it in GitHub Desktop.
Script extracting group information from a text file
#!/usr/bin/ruby -w
Group = Struct.new :name, :primary, :secondary, :editors
GET_UID = /uid=([^,]*)/
EXTRACT = {
"dn" => [:name, /cn=([^,]*)/],
"owner" => [:primary, GET_UID],
"seeAlso" => [:secondary, GET_UID],
"uniquemember" => [lambda {|group, ed| (group.editors ||= []) << ed}, GET_UID],
}
groups = []
def groups.update(group)
self << group if group
nil
end
current = nil
ARGF.each_line do |line|
if /^\s*$/ =~ line
# empty line
current = groups.update(current)
else
instructions = EXTRACT[line[/^\w+/]]
if instructions
where, rx = *instructions
data = line[rx, 1]
current ||= Group.new
if Proc === where
where[current, data]
else
current[where] = data
end
end
end
end
current = groups.update(current)
groups.sort_by!(&:name)
# debug:
# require 'pp'
# pp groups
# proper output:
groups.each do |group|
puts "Group: #{group.name}",
"Primary: #{group.primary}",
"Secondary: #{group.secondary}"
group.editors.sort.each {|ed| puts "Editor: #{ed}"}
puts
end
dn: cn=group2,ou=groups,dc=example,dc=com
owner: uid=user1,ou=people,dc=example,dc=com
seeAlso: uid=user2,ou=people,dc=example,dc=com
uniquemember: uid=user1,ou=people,dc=example,dc=com
uniquemember: uid=user2,ou=people,dc=example,dc=com
dn: cn=group1,ou=groups,dc=example,dc=com
owner: uid=user8,ou=people,dc=example,dc=com
seeAlso: uid=user6,ou=people,dc=example,dc=com
uniquemember: uid=user8,ou=people,dc=example,dc=com
uniquemember: uid=user11,ou=people,dc=example,dc=com
uniquemember: uid=user20,ou=people,dc=example,dc=com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment