-
Notifications
You must be signed in to change notification settings - Fork 3
/
parameters.go
83 lines (71 loc) · 2.18 KB
/
parameters.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"os"
"strconv"
"strings"
"time"
docopt "github.com/docopt/docopt-go"
)
type Parameters struct {
displays StringSet
interval time.Duration
message string
threshold int
uevent string
}
var (
usage = `
Shows a message (using swaynag) when battery percentage is less then specified
value.
Usage:
swaynag-battery [options]
swaynag-battery -h | --help
swaynag-battery --version
Options:
--displays <display-list> Comma separated list of displays to show the
alert - the default is to show in all displays.
--threshold <int> Percentual threshold to show notification.
[default: 15]
--interval <duration> Check battery at every interval. [default: 5m]
--uevent <path> Uevent path for reading battery stats.
[default: /sys/class/power_supply/BAT0/uevent]
--message <message> Override the low battery message.
-h --help Show this screen.
--version Show version.
`
)
func CommandLineParameters(arguments []string) Parameters {
args, err := docopt.ParseArgs(usage, arguments, version)
if err != nil {
logAndExit(18, "Unable to parse input arguments.")
}
interval, err := time.ParseDuration(args["--interval"].(string))
if err != nil {
logAndExit(28, "Unable to parse '--interval %s': the value must be a duration.", args["--interval"])
}
threshold, err := strconv.Atoi(args["--threshold"].(string))
if err != nil {
logAndExit(38, "Unable to parse '--threshold %s': the value must be an integer number.", args["--threshold"])
}
displays := []string{}
d, ok := args["--displays"].(string)
if ok {
displays = strings.Split(d, ",")
}
var message string = "Your battery is running low. Please plug in a power adapter"
if args["--message"] != nil {
message = args["--message"].(string)
}
uevent := args["--uevent"].(string)
file, err := os.Open(uevent)
if err != nil {
logAndExit(42, "Could not load battery file '%s'.", uevent)
}
file.Close()
return Parameters{
displays: SetFrom(displays),
interval: interval,
message: message,
threshold: threshold,
uevent: uevent}
}