Skip to content

Commit

Permalink
Reorganize if/else clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Oct 16, 2023
1 parent 9bd35da commit 836cda8
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions internal/factsengine/gatherers/systemd_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package gatherers
import (
"context"
"encoding/json"
"fmt"

"github.com/coreos/go-systemd/v22/dbus"
log "github.com/sirupsen/logrus"
"github.com/trento-project/agent/pkg/factsengine/entities"
)

const (
SystemDGathererNameV2 = "systemd@v2"
)

// nolint:gochecknoglobals
var (
SystemDUnitError = entities.FactGatheringError{
Expand Down Expand Up @@ -77,27 +74,30 @@ func (g *SystemDGathererV2) Gather(factsRequests []entities.FactRequest) ([]enti
log.Error(SystemDMissingArgument.Message)
fact := entities.NewFactGatheredWithError(factReq, &SystemDMissingArgument)
facts = append(facts, fact)
} else {
properties, err := g.dbusConnnector.GetUnitPropertiesContext(ctx, factReq.Argument)
if err != nil {
gatheringError := SystemDUnitError.Wrap(err.Error())
log.Error(gatheringError)
facts = append(facts, entities.NewFactGatheredWithError(factReq, gatheringError))
continue
}

var fact entities.Fact

factValue, err := unitPropertiesToFactValue(properties)
if err == nil {
fact = entities.NewFactGatheredWithRequest(factReq, factValue)
} else {
gatheringError := SystemDDecodingError.Wrap(err.Error())
log.Error(gatheringError)
fact = entities.NewFactGatheredWithError(factReq, gatheringError)
}
facts = append(facts, fact)
continue
}

properties, err := g.dbusConnnector.GetUnitPropertiesContext(ctx, factReq.Argument)
if err != nil {
gatheringError := SystemDUnitError.
Wrap(fmt.Sprintf("argument %s", factReq.Argument)).
Wrap(err.Error())
log.Error(gatheringError)
facts = append(facts, entities.NewFactGatheredWithError(factReq, gatheringError))
continue
}

factValue, err := unitPropertiesToFactValue(properties)
if err == nil {
facts = append(facts, entities.NewFactGatheredWithRequest(factReq, factValue))
continue
}

gatheringError := SystemDDecodingError.
Wrap(fmt.Sprintf("argument %s", factReq.Argument)).
Wrap(err.Error())
log.Error(gatheringError)
facts = append(facts, entities.NewFactGatheredWithError(factReq, gatheringError))
}

log.Infof("Requested %s v2 facts gathered", SystemDGathererName)
Expand Down

0 comments on commit 836cda8

Please sign in to comment.