Skip to content

Instantly share code, notes, and snippets.

@badvision
Created March 8, 2016 17:17
Show Gist options
  • Save badvision/24c99a523002e16d1f5a to your computer and use it in GitHub Desktop.
Save badvision/24c99a523002e16d1f5a to your computer and use it in GitHub Desktop.
Part 2 of building user information (See getGroupInfo.groovy for instructions)
import groovy.json.*
def slurper = new JsonSlurper()
slurper.setType(JsonParserType.INDEX_OVERLAY);
def groupDir = '.' as File
def output = 'matrix.txt' as File
def users = new TreeMap();
def userGroups = new HashMap();
def allGroups = new TreeSet();
groupDir.list({d, f-> f ==~ /.*.json/ } as FilenameFilter).each{jsonFile ->
def data = slurper.parse(new File(groupDir, jsonFile))
def groupName = data.authorizableId;
if (data.members && data.members.size() > 0) {
allGroups += groupName;
}
data.members.each{user ->
def userName = user.authorizableId;
if (!users.containsKey(userName)) {
userGroups[userName] = new HashSet();
users[userName] = user;
}
userGroups[userName] += groupName;
}
}
FileWriter fileWriter = new FileWriter(output, false)
BufferedWriter buffWriter = new BufferedWriter(fileWriter)
buffWriter.write("User\tHome\tCount")
allGroups.each{buffWriter.write("\t${it}")}
users.each{userName,userObject ->
def groups = userGroups[userName]
buffWriter.write("\n${userName}\t${userObject.home}\t${groups.size()}")
allGroups.each{
buffWriter.write("\t${groups.contains(it)?'X':''}")
}
}
buffWriter.flush()
buffWriter.close()
""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment