-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
248 lines (219 loc) · 4.62 KB
/
cli.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"database/sql"
"encoding/base64"
"flag"
"fmt"
"log"
"os"
"os/user"
"gopkg.in/yaml.v2"
)
func getUser() *user.User {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return usr
}
var (
User = getUser()
DBFileDefault = fmt.Sprintf(
"%s/watchmon.sqlite",
User.HomeDir)
bashCompletion string
gitTag string
gitRef string
outputFormat string
Cli *CLI
measureCmd = flag.NewFlagSet("measure", flag.ExitOnError)
batteryDevice = measureCmd.String(
"bat",
"BAT0",
"specify battery device from '/sys/class/power_supply/BAT*'",
)
intendedFormat = measureCmd.String("output", "plain", "choose from plain,json,yaml")
noStore = measureCmd.Bool("no-store", false, "choose to not store a measurement")
dbCmd = flag.NewFlagSet("db", flag.ExitOnError)
dbCreate = flag.Bool("create", false, "wether to create the sqlite file")
completionCmd = flag.NewFlagSet("completion", flag.ExitOnError)
)
type CLI struct {
BatteryDevice *string
SqliteFile *string
CreateDBToggle *bool
DumpRowsToggle *bool
NoStoreToggle *bool
OutputFormat *string
DB *sql.DB
Last *BatteryDataRow
}
func (c *CLI) String() string {
cfgBytes, err := yaml.Marshal(c)
checkErr(err)
return string(cfgBytes)
}
func (cli *CLI) Init() {
db, err := sql.Open("sqlite3", *cli.SqliteFile)
checkErr(err)
cli.DB = db
if *cli.CreateDBToggle {
CreateDatabaseAndTables(cli.DB)
}
}
func (cli *CLI) DumpRows() {
rows, err := cli.DB.Query("SELECT * FROM batteryinfo")
checkErr(err)
var id int
var chargeNow int
var chargeFull int
var chargeDesign int
var currentNow int
var voltageNow int
var charging int
var timestamp int
var cycleCount int
fmt.Println("id|charge_now|charge_full|charge_design|current_now|voltage_now|charging|timestamp|cycle_count")
for rows.Next() {
err = rows.Scan(
&id,
&chargeNow,
&chargeFull,
&chargeDesign,
¤tNow,
&voltageNow,
&charging,
×tamp,
&cycleCount,
)
checkErr(err)
fmt.Printf(
"%d|%d|%d|%d|%d|%d|%d|%d|%d\n",
id,
chargeNow,
chargeFull,
chargeDesign,
currentNow,
voltageNow,
charging,
timestamp,
cycleCount,
)
}
}
func (cli *CLI) Measure() *BatteryDataComputed {
var batInfo *BatteryDataRow = CreateBatteryData(cli.BatteryDevice)
if !*cli.NoStoreToggle {
stmt, err := cli.DB.Prepare(`
INSERT INTO batteryinfo(
charge_now,
charge_full,
charge_design,
current_now,
voltage_now,
charging,
timestamp,
cycle_count)
values(?,?,?,?,?,?,?,?)`,
)
checkErr(err)
_, errSQL := stmt.Exec(
batInfo.ChargeNow,
batInfo.ChargeFull,
batInfo.ChargeFullDesign,
batInfo.CurrentNow,
batInfo.VoltageNow,
batInfo.Charging,
batInfo.Timestamp,
batInfo.Cycles,
)
checkErr(errSQL)
}
return batInfo.Compute()
}
func (cli *CLI) LastMeasure() *BatteryDataComputed {
var id int64
var chargeNow int64
var chargeFull int64
var chargeDesign int64
var currentNow int64
var voltageNow int64
var charging int64
var timestamp int64
var cycleCount int64
err := cli.DB.QueryRow(
"SELECT * FROM batteryinfo ORDER BY id DESC LIMIT 1",
).Scan(
&id,
&chargeNow,
&chargeFull,
&chargeDesign,
¤tNow,
&voltageNow,
&charging,
×tamp,
&cycleCount,
)
checkErr(err)
bdw := BatteryDataRow{
Id: id,
ChargeNow: chargeNow,
ChargeFull: chargeFull,
ChargeFullDesign: chargeDesign,
CurrentNow: currentNow,
VoltageNow: voltageNow,
Charging: charging,
Timestamp: timestamp,
Cycles: cycleCount,
}
return bdw.Compute()
}
func (cli *CLI) Completions() string {
data, err := base64.StdEncoding.DecodeString(bashCompletion)
if err != nil {
panic("error: the shell completions script could not be decoded")
}
return string(data)
}
func usage() {
fmt.Fprintf(
os.Stderr,
`watchmon %s-%s
supports measure, dump, completions sub commands`,
gitTag, gitRef,
)
}
func main() {
flag.Parse()
Cli = &CLI{
BatteryDevice: batteryDevice,
SqliteFile: &DBFileDefault,
NoStoreToggle: noStore,
CreateDBToggle: dbCreate,
OutputFormat: intendedFormat,
}
Cli.Init()
defer Cli.DB.Close()
if len(os.Args) >= 2 {
switch os.Args[1] {
case "measure":
measureCmd.Parse(os.Args[2:])
Cli.Measure()
fmt.Fprintf(os.Stdout, "%+v", Cli.LastMeasure())
case "dump":
dbCmd.Parse(os.Args)
fmt.Println(dbCmd.Args())
Cli.DumpRows()
case "completions":
completionCmd.Parse(os.Args)
fmt.Fprintf(os.Stdout,
"%s\n",
Cli.Completions())
default:
usage()
os.Exit(1)
}
} else {
usage()
}
}