Skip to content

Commit

Permalink
Extract checkForTag function #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mbehlendorf committed Dec 12, 2017
1 parent 0d52df2 commit aa149f0
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions confd/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func convertToServices(kapi client.KeysAPI, tag string, key string) (Services, e
return services, nil
}

// ConvertToService converts a json into a service if it contains the passed tag
func ConvertToService(tag string, value string) (*Service, error) {
// ConvertToService converts a json into a service if it contains the passed serviceTag
func ConvertToService(serviceTag string, value string) (*Service, error) {
raw := confd.RawData{}
err := json.Unmarshal([]byte(value), &raw)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshall service json")
}

if tag != "" {
return createServiceIfNecessary(raw, tag), nil
if serviceTag != "" {
return createServiceIfNecessary(raw, serviceTag), nil
} else {
return createServiceFromRaw(raw), nil
}
Expand All @@ -84,25 +84,32 @@ func createServiceIfNecessary(data map[string]interface{}, serviceTag string) (*
if !castSuccessful {
continue
}
if tag == serviceTag {
return createServiceFromRaw(data)
}
if strings.HasPrefix(tag, serviceTag) {
port, err := findPortInTag(tag, serviceTag)
if err != nil {
log.Println(err)
continue
}
if port == fmt.Sprintf("%v",data["port"]) {
name := strings.TrimSuffix(data["name"].(string), "-"+port)
address := data["service"].(string)
return createService(name, address)
}
service := checkForTag(tag, serviceTag, data)
if service != nil {
return service
}
}
}
return nil
}
func checkForTag(tag string, serviceTag string, data map[string]interface{} ) (*Service) {
if tag == serviceTag {
return createServiceFromRaw(data)
}
if strings.HasPrefix(tag, serviceTag) {
port, err := findPortInTag(tag, serviceTag)
if err != nil {
log.Println(err)
return nil
}
if port == fmt.Sprintf("%v",data["port"]) {
name := strings.TrimSuffix(data["name"].(string), "-"+port)
address := data["service"].(string)
return createService(name, address)
}
}
return nil
}

func findPortInTag(tag string, tagPrefix string) (string, error) {
// searches for <port> in strings like 'webapp:port=<port>'
Expand Down

0 comments on commit aa149f0

Please sign in to comment.