forked from kovetskiy/yota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (130 loc) · 2.99 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/docopt/docopt-go"
"github.com/kovetskiy/go-yota"
"github.com/zazab/zhash"
)
const (
usage = `yota-cli 2.2
Usage:
yota-cli [options] -C -c <code>
yota-cli [options] -C -s <speed>
yota-cli [options] -L
yota-cli [options] -B
yota-cli -h | --help
yota-cli -v | --version
Options:
-C Change tariff.
-c <code> Specify tariff by code like 'POS-1234-567'
-s <speed> Specify tariff by speed (float like '1.0' or just 'max')
-L List all tariffs.
-B Show balance.
-f <config> Specify configuratin file [default: ~/.config/yotarc]
-h --help Show this screen.
-v --version Show version.
`
)
func main() {
args, err := docopt.Parse(usage, nil, true, "2.2", true)
if err != nil {
panic(err)
}
config, err := getConfig(args["-f"].(string))
if err != nil {
log.Fatalf("can't read config: %s", err)
}
username, err := config.GetString("username")
if err != nil {
log.Fatal(err)
}
password, err := config.GetString("password")
if err != nil {
log.Fatal(err)
}
yotaClient := yota.NewClient(username, password, nil)
err = yotaClient.Login()
if err != nil {
log.Fatalf("can't login: %s", err)
}
switch {
case args["-L"].(bool):
err = listTariffs(yotaClient)
case args["-C"].(bool):
var (
code, _ = args["-c"].(string)
speed, _ = args["-s"].(string)
)
err = searchAndChangeTariff(yotaClient, code, speed)
case args["-B"].(bool):
err = showBalance(yotaClient)
}
if err != nil {
log.Fatal(err)
}
}
func listTariffs(yotaClient *yota.Client) error {
tariffs, err := yotaClient.GetTariffs()
if err != nil {
return err
}
format := "%s %-12s %-5s %s\n"
fmt.Printf(format, " ", "code", "speed", "name")
for _, tariff := range tariffs {
label := " "
if tariff.Active {
label = "*"
}
fmt.Printf(format, label, tariff.Code, tariff.Speed, tariff.Name)
}
return err
}
func showBalance(yotaClient *yota.Client) error {
balance, err := yotaClient.GetBalance()
if err != nil {
return err
}
fmt.Printf("%d\n", balance)
return nil
}
func searchAndChangeTariff(
yotaClient *yota.Client, code, speed string,
) error {
tariffs, err := yotaClient.GetTariffs()
if err != nil {
return err
}
found := false
newTariff := yota.Tariff{}
for _, tariff := range tariffs {
if tariff.Code == code || tariff.Speed == speed {
found = true
newTariff = tariff
break
}
}
if found {
err = yotaClient.ChangeTariff(newTariff)
if err != nil {
log.Fatal(err)
}
fmt.Println("tariff successfully changed")
return nil
}
return fmt.Errorf("can't find specified tariff")
}
func getConfig(path string) (zhash.Hash, error) {
var configData map[string]interface{}
if strings.HasPrefix(path, "~/") {
path = strings.Replace(path, "~/", os.Getenv("HOME")+"/", 1)
}
_, err := toml.DecodeFile(path, &configData)
if err != nil {
return zhash.Hash{}, err
}
return zhash.HashFromMap(configData), nil
}