Last active
October 8, 2024 11:51
-
-
Save lmammino/a4dc917f66c6c10e415250304a4a692f to your computer and use it in GitHub Desktop.
Ldap filter beautifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"./ldap" | |
) | |
var filter = "(&(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) )))(objectCategory=user)(department=IT)(physicalDeliveryOfficeName=Amsterdam)))" | |
func main() { | |
fmt.Printf(ldap.FormatFilter(filter)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ldap | |
import ( | |
"strings" | |
) | |
// inspired by https://blogs.technet.microsoft.com/389thoughts/2016/11/24/ldap-query-prettyfier/ | |
func FormatFilter(filter string) string { | |
buffer := new(strings.Builder) | |
noSpaces := strings.Replace(filter, " ", "", -1) | |
noSpaces = strings.Replace(noSpaces, "\n", "", -1) | |
tabLevel := 0 | |
tabString := " " | |
inline := false | |
for _, char := range noSpaces { | |
switch char { | |
case '(': | |
buffer.WriteString("\n") | |
buffer.WriteString(strings.Repeat(tabString, tabLevel)) | |
buffer.WriteString("(") | |
tabLevel = tabLevel + 1 | |
inline = false | |
case ')': | |
tabLevel = tabLevel - 1 | |
if !inline { | |
buffer.WriteString(strings.Repeat(tabString, tabLevel)) | |
} | |
buffer.WriteString(")\n") | |
inline = false | |
default: | |
inline = true | |
buffer.WriteRune(char) | |
} | |
} | |
return buffer.String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was looking for PowerShell in the first place, but the original inspiration cited has disappeared from MS site. So here is a re-re-write in PowerShell: