Skip to content

Commit 713978e

Browse files
authored
Merge pull request #2 from nxadm/ldif_version
Ldif version
2 parents 93d304e + 60ae0c5 commit 713978e

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

input.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ func importLdifFile(file string, ignoreAttr []string) (entries, error) {
2828
func addLineToRecord(line *string, record *[]string, ignoreAttr []string, prevAttrSkipped *bool) error {
2929
var err error
3030

31-
// Skip comments
32-
if strings.HasPrefix(*line, "#") {
33-
return nil
34-
}
35-
3631
// Append continuation lines to previous line
3732
if strings.HasPrefix(*line, " ") {
3833
if *prevAttrSkipped { // but not lines from a skipped attribute
@@ -107,8 +102,28 @@ func readFile(file string, ignoreAttr []string, queue chan<- []string, wg *sync.
107102
record := []string{}
108103
scanner := bufio.NewScanner(fh)
109104
var prevAttrSkipped bool // use to skip continuation lines of skipped attr
105+
firstLine := true
110106
for scanner.Scan() {
107+
111108
line := scanner.Text()
109+
110+
// Skip comments
111+
if strings.HasPrefix(line, "#") {
112+
continue
113+
}
114+
115+
// Check if first line is a "version: *" line and skip it
116+
if firstLine {
117+
if strings.HasPrefix(line, "version: ") {
118+
firstLine = false
119+
continue
120+
} else if line == "" {
121+
continue
122+
}
123+
firstLine = false
124+
}
125+
126+
// Import lines as records
112127
*err = addLineToRecord(&line, &record, ignoreAttr, &prevAttrSkipped)
113128
if *err != nil {
114129
return
@@ -134,10 +149,23 @@ func readFile(file string, ignoreAttr []string, queue chan<- []string, wg *sync.
134149
func readStr(ldifStr string, ignoreAttr []string, queue chan<- []string, wg *sync.WaitGroup, err *error) {
135150
defer wg.Done()
136151
defer close(queue)
137-
for _, recordStr := range strings.Split(ldifStr, "\n\n") {
152+
153+
for idx, recordStr := range strings.Split(ldifStr, "\n\n") {
138154
var prevAttrSkipped bool
139155
record := []string{}
140156
for _, line := range strings.Split(recordStr, "\n") {
157+
158+
// Skip comments
159+
if strings.HasPrefix(line, "#") {
160+
continue
161+
}
162+
163+
if idx == 0 { // First record only
164+
if strings.HasPrefix(line, "version: ") {
165+
continue
166+
}
167+
}
168+
141169
*err = addLineToRecord(&line, &record, ignoreAttr, &prevAttrSkipped)
142170
if *err != nil {
143171
return

t/source.ldif

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# LDIF ends with an empty line
2-
# A comment
1+
version: 1
2+
33
dn: identical,ou=aAccounts,dc=domain,dc=ext
44
sambaSID: A samba id something
55
eduPersonEntitlement: urn:mace:domain.ext:en

0 commit comments

Comments
 (0)