fix(battery): map battery behavior before returning

This commit is contained in:
Jan De Dobbeleer 2021-09-20 07:01:00 +02:00 committed by Jan De Dobbeleer
parent 93b00ee0aa
commit b2515650c1
2 changed files with 27 additions and 4 deletions

View file

@ -368,7 +368,33 @@ func (env *environment) getArgs() *args {
func (env *environment) getBatteryInfo() ([]*battery.Battery, error) {
defer env.tracer.trace(time.Now(), "getBatteryInfo")
return battery.GetAll()
batteries, err := battery.GetAll()
// actual error, return it
if err != nil && len(batteries) == 0 {
return nil, err
}
// there are no batteries found
if len(batteries) == 0 {
return nil, &noBatteryError{}
}
// some batteries fail to get retrieved, filter them out if present
validBatteries := []*battery.Battery{}
for _, batt := range batteries {
if batt != nil {
validBatteries = append(validBatteries, batt)
}
}
unableToRetrieveBatteryInfo := "A device which does not exist was specified."
// when battery info fails to get retrieved but there is at least one valid battery, return it without error
if len(validBatteries) > 0 && err != nil && strings.Contains(err.Error(), unableToRetrieveBatteryInfo) {
return validBatteries, nil
}
// another error occurred (possibly unmapped use-case), return it
if err != nil {
return nil, err
}
// everything is fine
return validBatteries, nil
}
func (env *environment) getShellName() string {

View file

@ -48,9 +48,6 @@ func (b *batt) enabled() bool {
b.Battery = &battery.Battery{}
for _, bt := range batteries {
if bt == nil {
continue
}
b.Battery.Current += bt.Current
b.Battery.Full += bt.Full
b.Battery.State = b.mapMostLogicalState(b.Battery.State, bt.State)