-
Notifications
You must be signed in to change notification settings - Fork 2
/
ldifConvertor.go
69 lines (65 loc) · 1.91 KB
/
ldifConvertor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ldifdiff
import (
"bytes"
"errors"
"strings"
"sync"
)
func createModifyStr(actionEntry actionEntry) (string, error) {
var buffer bytes.Buffer
subActions := make(map[subAction]string)
subActions[subActionModifyAdd] = "add"
subActions[subActionModifyDelete] = "delete"
subActions[subActionModifyReplace] = "replace"
for idx, subActionList := range actionEntry.SubActionAttrs {
for subAction, attrList := range subActionList {
if subAction == subActionNone {
return "", errors.New(("Invalid Subaction subActionNone for action actionModify"))
}
for idxInner, attr := range attrList {
if idxInner != 0 || idx != 0 {
buffer.WriteString("-\n")
}
parts := strings.Split(attr, ":")
buffer.WriteString(subActions[subAction] +
": " + parts[0] + "\n")
buffer.WriteString(parts[0] + ":" +
strings.Join(parts[1:], ":") + "\n")
}
}
}
return buffer.String(), nil
}
func writeLdif(queue <-chan actionEntry, writer *bytes.Buffer, wg *sync.WaitGroup, err *error) {
defer wg.Done()
for actionEntry := range queue {
if *err != nil {
continue
}
switch actionEntry.Action {
case actionAdd:
writer.WriteString(actionEntry.Dn + "\n") //dn
writer.WriteString("changetype: add\n")
attrList := actionEntry.SubActionAttrs[0][subActionNone]
for _, attr := range attrList {
writer.WriteString(attr + "\n")
}
case actionDelete:
writer.WriteString(actionEntry.Dn + "\n") //dn
writer.WriteString("changetype: delete\n")
case actionModify:
writer.WriteString(actionEntry.Dn + "\n") //dn
writer.WriteString("changetype: modify\n")
modifyStr, modifyErr := createModifyStr(actionEntry)
if modifyErr != nil {
*err = modifyErr
continue
}
writer.WriteString(modifyStr)
default:
*err = errors.New("Unexpected LDIF action value: " + string(actionEntry.Action))
continue
}
writer.WriteString("\n") // empty line as record separator
}
}