Apply gofmt & minor refactoring

This commit is contained in:
Alex 2020-01-26 17:45:04 +01:00
parent a7ccdad378
commit 2ad9bce75c

22
main.go
View file

@ -35,8 +35,8 @@ func dnToConsul(dn string) (string, error) {
return strings.Join(rdns, "/"), nil
}
func consulToDN(pair *consul.KVPair) (string, string, []byte) {
path := strings.Split(pair.Key, "/")
func consulToDN(key string) (string, string) {
path := strings.Split(key, "/")
dn := ""
for _, cpath := range path {
if cpath == "" {
@ -44,14 +44,14 @@ func consulToDN(pair *consul.KVPair) (string, string, []byte) {
}
kv := strings.Split(cpath, "=")
if len(kv) == 2 && kv[0] == "attribute" {
return dn, kv[1], pair.Value
return dn, kv[1]
}
if dn != "" {
dn = "," + dn
}
dn = cpath + dn
}
panic("Consul key " + pair.Key + " does not end with attribute=something")
panic("Consul key " + key + " does not end with attribute=something")
}
func parseValue(value []byte) ([]string, error) {
@ -75,14 +75,11 @@ func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) {
for _, kv := range data {
log.Printf("(parseConsulResult) %s %s", kv.Key, string(kv.Value))
dn, attr, val := consulToDN(kv)
if attr == "" || val == nil {
continue
}
dn, attr := consulToDN(kv.Key)
if _, exists := aggregator[dn]; !exists {
aggregator[dn] = Entry{}
}
value, err := parseValue(val)
value, err := parseValue(kv.Value)
if err != nil {
return nil, err
}
@ -682,7 +679,7 @@ func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest)
return ldap.LDAPResultNoSuchObject, fmt.Errorf("Not found: %s", dn)
}
for _, item := range items {
itemDN, _, _ := consulToDN(item)
itemDN, _ := consulToDN(item.Key)
if itemDN != dn {
return ldap.LDAPResultNotAllowedOnNonLeaf, fmt.Errorf(
"Cannot delete %d as it has children", dn)
@ -728,7 +725,6 @@ func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest)
return ldap.LDAPResultSuccess, nil
}
func (server *Server) handleModify(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
state := s.(*State)
r := m.GetModifyRequest()
@ -769,11 +765,11 @@ func (server *Server) handleModifyInternal(state *State, r *message.ModifyReques
prevEntry := Entry{}
for _, item := range items {
itemDN, attr, val := consulToDN(item)
itemDN, attr := consulToDN(item.Key)
if itemDN != dn {
panic("itemDN != dn in handleModifyInternal")
}
vals, err := parseValue(val)
vals, err := parseValue(item.Value)
if err != nil {
return ldap.LDAPResultOperationsError, err
}