diff --git a/apply.go b/apply.go index bdbdb92..650607d 100644 --- a/apply.go +++ b/apply.go @@ -3,6 +3,7 @@ package ldif import ( "fmt" "log" + "os" "gopkg.in/ldap.v2" ) @@ -10,12 +11,17 @@ import ( // Apply sends the LDIF entries to the server and does the changes as // given by the entries. // -// All *ldap.Entry are converted to an *ldap.AddRequest. +// All *ldap.Entry are converted to an *ldap.AddRequest, NOTE: this +// modifies the *LDIF... // // By default, it returns on the first error. To continue with applying the // LDIF, set the continueOnErr argument to true - in this case the errors // are logged with log.Printf() func (l *LDIF) Apply(conn ldap.Client, continueOnErr bool) error { + if l.Logger == nil { + l.Logger = log.New(os.Stderr, "", log.Flags()) + defer func() { l.Logger = nil }() + } for _, entry := range l.Entries { switch { case entry.Entry != nil: @@ -28,7 +34,7 @@ func (l *LDIF) Apply(conn ldap.Client, continueOnErr bool) error { case entry.Add != nil: if err := conn.Add(entry.Add); err != nil { if continueOnErr { - log.Printf("ERROR: Failed to add %s: %s", entry.Add.DN, err) + l.Logger.Printf("ERROR: Failed to add %s: %s", entry.Add.DN, err) continue } return fmt.Errorf("failed to add %s: %s", entry.Add.DN, err) @@ -37,7 +43,7 @@ func (l *LDIF) Apply(conn ldap.Client, continueOnErr bool) error { case entry.Del != nil: if err := conn.Del(entry.Del); err != nil { if continueOnErr { - log.Printf("ERROR: Failed to delete %s: %s", entry.Del.DN, err) + l.Logger.Printf("ERROR: Failed to delete %s: %s", entry.Del.DN, err) continue } return fmt.Errorf("failed to delete %s: %s", entry.Del.DN, err) @@ -46,7 +52,7 @@ func (l *LDIF) Apply(conn ldap.Client, continueOnErr bool) error { case entry.Modify != nil: if err := conn.Modify(entry.Modify); err != nil { if continueOnErr { - log.Printf("ERROR: Failed to modify %s: %s", entry.Modify.DN, err) + l.Logger.Printf("ERROR: Failed to modify %s: %s", entry.Modify.DN, err) continue } return fmt.Errorf("failed to modify %s: %s", entry.Modify.DN, err) diff --git a/ldif.go b/ldif.go index fd8fc6b..71da275 100644 --- a/ldif.go +++ b/ldif.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/url" "strconv" "strings" @@ -34,6 +35,7 @@ type LDIF struct { changeType string FoldWidth int Controls bool + Logger *log.Logger firstEntry bool } diff --git a/marshal.go b/marshal.go index 51c3c48..b9c044b 100644 --- a/marshal.go +++ b/marshal.go @@ -17,7 +17,7 @@ var ErrMixed = errors.New("cannot mix change records and content records") // Marshal returns an LDIF string from the given LDIF. // // The default line lenght is 76 characters. This can be changed by setting -// the fw parameter to something else than 0. +// the FoldWidth struct member of the *LDIF to something else than 0. // For a fold width < 0, no folding will be done, with 0, the default is used. func Marshal(l *LDIF) (data string, err error) { hasEntry := false @@ -178,7 +178,8 @@ func foldLine(line string, fw int) (folded string) { // *ldap.DelRequest, *ldap.ModifyRequest and *ldap.ModifyDNRequest or slices // of any of those. // -// See Marshal() for the fw argument. +// The fw parameter sets the FoldWidth of the internally used *LDIF, +// see Marshal() for a description. func Dump(fh io.Writer, fw int, entries ...interface{}) error { l, err := ToLDIF(entries...) if err != nil {