Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update docs, add logger #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ package ldif
import (
"fmt"
"log"
"os"

"gopkg.in/ldap.v2"
)

// 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:
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions ldif.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"strconv"
"strings"
Expand All @@ -34,6 +35,7 @@ type LDIF struct {
changeType string
FoldWidth int
Controls bool
Logger *log.Logger
firstEntry bool
}

Expand Down
5 changes: 3 additions & 2 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down