-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (66 loc) · 1.54 KB
/
main.go
File metadata and controls
75 lines (66 loc) · 1.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
var formatValues bool
func main() {
parseFlags()
response, err := http.Get("http://localhost:3490/v1/unpulsed")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var whatpulse Whatpulse
json.Unmarshal(responseData, &whatpulse)
fmt.Println("Keys:", whatpulse.Keys)
fmt.Println("Clicks:", whatpulse.Clicks)
if formatValues {
fmt.Println("Download:", formatDataValue(whatpulse.Download))
fmt.Println("Upload:", formatDataValue(whatpulse.Upload))
fmt.Println("Uptime:", formatTimeValue(whatpulse.Uptime))
} else {
fmt.Println("Download:", whatpulse.Download)
fmt.Println("Upload:", whatpulse.Upload)
fmt.Println("Uptime:", whatpulse.Uptime)
}
}
func parseFlags() {
flag.BoolVar(&formatValues, "format", false, "Determine if values printed should be formatted")
flag.BoolVar(&formatValues, "f", false, "Determine if values printed should be formatted")
flag.Parse()
}
func formatDataValue(dataValue int) string {
var value float32
divisionCount := 0
var unit string
value = float32(dataValue)
for value >= 1024 {
value = value / 1024
divisionCount++
}
switch divisionCount {
case 0:
unit = "B"
case 1:
unit = "kB"
case 2:
unit = "MB"
case 3:
unit = "GB"
}
return fmt.Sprintf("%.2f", value) + unit
}
func formatTimeValue(timeValue int) string {
return fmt.Sprint(time.Duration(timeValue) * time.Second)
}