Skip to content

Instantly share code, notes, and snippets.

@ergoz
Forked from lmammino/example.go
Last active October 8, 2024 11:52
Show Gist options
  • Save ergoz/cbb1e9d91d9846f0ad7c9d15a61d6a11 to your computer and use it in GitHub Desktop.
Save ergoz/cbb1e9d91d9846f0ad7c9d15a61d6a11 to your computer and use it in GitHub Desktop.
Ldap filter beautifier
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))
}
# inspired by https://blogs.technet.microsoft.com/389thoughts/2016/11/24/ldap-query-prettyfier/
function Format-LDAPFilter($ldapFilterString) {
$tabLevel = 0
$indentationString = " " #you can do a single tab, two spaces, four spaces, etc.
$inline = $false
$ldapFilterStringRewritten = ""
#Eliminate whitespaces first.
$ldapFilterString = $ldapFilterString -replace "\s+",""
for ($i = 0; $i -lt $ldapFilterString.length; $i++) {
switch ($ldapFilterString[$i]) {
'('{
$ldapFilterStringRewritten += "`n"
$ldapFilterStringRewritten += $indentationString * $tabLevel
$ldapFilterStringRewritten += "("
$tabLevel += 1
$inline = $false
}
')'{
$tabLevel -= 1
if (-NOT $inline) {
$ldapFilterStringRewritten += $indentationString * $tabLevel
}
$ldapFilterStringRewritten += ")" #`n
if (($ldapFilterString.length - $i) -gt 1 -AND $ldapFilterString[$i+1] -ne '(') {
$ldapFilterStringRewritten += "`n"
}
$inline = $false
}
default{
$inline = $true
$ldapFilterStringRewritten += $ldapFilterString[$i]
}
}
}
$ldapFilterStringRewritten #implicit return
}
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()
}
function prettify($filter)
{
$buffer = "";
$noSpaces = str_replace(" ", "", $filter);
$noSpaces = str_replace("\n", "", $noSpaces);
$tabLevel = 0;
$tabString = " ";
$inline = false;
$chars = str_split($noSpaces);
foreach ($chars as $char) {
switch ($char) {
case '(':
$buffer .= "\n";
$buffer .= str_repeat($tabString, $tabLevel);
$buffer .= "(";
$tabLevel++;
$inline = false;
break;
case ')':
$tabLevel--;
if (!$inline) {
$buffer .= str_repeat($tabString, $tabLevel);
}
$buffer .= ")\n";
$inline = false;
break;
default:
$inline = true;
$buffer .= $char;
}
}
$buffer = preg_replace('/^[ \t]*[\r\n]+/m', '', $buffer);
return $buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment