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