Skip to content

Commit

Permalink
fix linter: cyclomatic complexity of ServeHTTP is 16
Browse files Browse the repository at this point in the history
Error: cyclomatic complexity 16 of func `(*InventoryServer).ServeHTTP` is high (> 15) (gocyclo)

Move code to update labels from headers to subfunction

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
  • Loading branch information
fgiudici committed Aug 31, 2022
1 parent a72e04b commit 71d2a3e
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions pkg/server/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,12 @@ func (i *InventoryServer) ServeHTTP(resp http.ResponseWriter, req *http.Request)
logrus.Infof("new machine inventory created: %s", inventory.Name)
}

labels, err := getLabels(req)
invUpdated, err := fillLabels(req, inventory)
if err != nil {
logrus.Warn("failed to parse labels header: ", err)
logrus.Warn("error extracting labels from headers: ", err)
}

if len(labels) > 0 {
if inventory.Labels == nil {
inventory.Labels = labels
} else {
for k, v := range labels {
inventory.Labels[k] = v
}
}
if invUpdated {
inventory, err = i.machineClient.Update(inventory)
if err != nil {
logrus.Error("failed to update inventory labels: ", err)
Expand Down Expand Up @@ -328,3 +321,26 @@ func getLabels(req *http.Request) (map[string]string, error) {
var labels map[string]string
return labels, json.Unmarshal(labelString, &labels)
}

func fillLabels(req *http.Request, inventory *elm.MachineInventory) (bool, error) {
updated := false
labels, err := getLabels(req)

if err != nil {
return updated, fmt.Errorf("extracting labels from headers: %w", err)
}
if len(labels) == 0 {
return updated, nil
}

updated = true
logrus.Debugf("found labels in request headers: %#v", labels)
if inventory.Labels == nil {
inventory.Labels = labels
} else {
for k, v := range labels {
inventory.Labels[k] = v
}
}
return updated, nil
}

0 comments on commit 71d2a3e

Please sign in to comment.