-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbattery.go
More file actions
61 lines (56 loc) · 1.2 KB
/
battery.go
File metadata and controls
61 lines (56 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// This file handles battery info checks
package main
import (
"errors"
"strconv"
"github.com/oalders/is/battery"
"github.com/oalders/is/compare"
"github.com/oalders/is/types"
)
// Run "is battery ...".
//
//nolint:cyclop
func (r *BatteryCmd) Run(ctx *types.Context) error {
attr, err := battery.GetAttr(ctx, r.Attr, r.Nth)
ctx.Success = false
if err != nil {
return err
}
switch got := attr.(type) {
case float64:
want, err := strconv.ParseFloat(r.Val, 64)
if err != nil {
return errors.Join(
errors.New("wanted result could not be converted to a float"),
err,
)
}
ok, err := compare.IntegersOrFloats(ctx, r.Op, got, want)
if err != nil {
return err
}
ctx.Success = ok
case int:
want, err := strconv.ParseInt(r.Val, 0, 32)
if err != nil {
return errors.Join(
errors.New("wanted result could not be converted to an integer"),
err,
)
}
ok, err := compare.IntegersOrFloats(ctx, r.Op, got, int(want))
if err != nil {
return err
}
ctx.Success = ok
case string:
ok, err := compare.Strings(ctx, r.Op, got, r.Val)
if err != nil {
return err
}
ctx.Success = ok
default:
return errors.New("unexpected type for " + r.Val)
}
return nil
}