-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud.go
111 lines (89 loc) · 2.25 KB
/
cloud.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
package cloud
import (
"bytes"
"strings"
"text/template"
"fmt"
"time"
"github.com/mrusme/cloudcash/lib"
)
type Cloud struct {
Config *lib.Config `json:"-"`
Services []lib.Service `json:"services"`
}
type WaybarOutput struct {
Text string `json:"text"`
Tooltip string `json:"tooltip"`
Alt string `json:"alt"`
Class string `json:"class"`
}
func New(config *lib.Config) (*Cloud) {
c := new(Cloud)
c.Config = config
return c
}
func (c *Cloud) AddService(id string, name string, client lib.ServiceClient) (error) {
c.Services = append(c.Services, lib.Service{
ID: id,
Name: name,
Client: client,
})
return nil
}
func (c *Cloud) RefreshAll() () {
for i := 0; i < len(c.Services); i++ {
status, err := c.Services[i].Client.GetServiceStatus()
if err == nil {
c.Services[i].Status = status
}
}
return
}
func (c *Cloud) JSON() (string) {
outputJson, _ := lib.JSONMarshal(c)
return string(outputJson)
}
func (c *Cloud) Waybar(t *template.Template) (string) {
waybarOutput := new(WaybarOutput)
var statuses []string
for _, service := range c.Services {
var status bytes.Buffer
if err := t.Execute(&status, service); err == nil {
statuses = append(statuses, status.String())
}
}
waybarOutput.Class = "cloudcash"
waybarOutput.Text = strings.Join(statuses, c.Config.Waybar.PangoJoiner)
waybarOutput.Tooltip = fmt.Sprintf(
"Updated %s",
time.Now().Format(time.RFC822),
)
outputJson, _ := lib.JSONMarshal(waybarOutput)
return string(outputJson)
}
func (c *Cloud) Text() (string) {
var text string = ""
for _, service := range c.Services {
text = fmt.Sprintf(
"%s%-20s$%8s [previous: $%8s / balance: $%8s]\n",
text,
service.Name,
service.Status.CurrentCharges,
service.Status.PreviousCharges,
service.Status.AccountBalance,
)
}
return text
}
func (c *Cloud) MenuText(t *template.Template) (string) {
var text string = ""
var statuses []string
for _, service := range c.Services {
var status bytes.Buffer
if err := t.Execute(&status, service); err == nil {
statuses = append(statuses, status.String())
}
}
text = strings.Join(statuses, c.Config.Waybar.PangoJoiner)
return text
}