-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
107 lines (97 loc) · 3.07 KB
/
info.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
package main
import (
"fmt"
"os"
"time"
"github.com/mohan3d/my-we/we"
"github.com/olekukonko/tablewriter"
)
// createTable returns data in table format.
func createTable(data [][]string) *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(data)
return table
}
// profileInfo returns profile info in rows format.
func profileInfo(v *we.CustomerInfo) [][]string {
data := [][]string{
{"Customer name", fmt.Sprintf("%v", v.CustomerInformationDto.CustomerName)},
{"Customer number", fmt.Sprintf("%v", v.CustomerInformationDto.CustomerNumber)},
{"Email address", fmt.Sprintf("%v", v.CustomerInformationDto.EmailAddress)},
{"Mobile number", fmt.Sprintf("%v", v.CustomerInformationDto.MobileNumber)},
{"ADSL number", fmt.Sprintf("%v", v.CustomerInformationDto.ADSLNumber)},
{"ADSL area code", fmt.Sprintf("%v", v.CustomerInformationDto.ADSLAreaCode)},
{"ADSL speed", fmt.Sprintf("%v", v.CustomerInformationDto.ADSLSpeed)},
{"City", fmt.Sprintf("%v", v.CustomerInformationDto.City)},
{"District", fmt.Sprintf("%v", v.CustomerInformationDto.District)},
}
return data
}
// usageInfo returns usage info in rows format.
func usageInfo(v *we.UsageInfo) [][]string {
t := time.Unix(v.AdslUsage.StartDate/1000, 0)
data := [][]string{
{"Start date", fmt.Sprintf("%v", t.Format("02/01/2006"))},
{"Quota", fmt.Sprintf("%v GB", v.AdslUsage.Quota)},
{"Total Used", fmt.Sprintf("%v GB", v.AdslUsage.TotalUsed)},
}
return data
}
// daysInfo returns days info in rows format.
func daysInfo(v *we.RemainingDaysInfo) [][]string {
data := [][]string{
{"Expiry date", fmt.Sprintf("%v", v.RemainingDays.ADSLExpiryDate)},
{"Amount due", fmt.Sprintf("%v", v.RemainingDays.AmountDue)},
{"Package name", fmt.Sprintf("%v", v.RemainingDays.PackageName)},
{"Remaining day", fmt.Sprintf("%v", v.RemainingDays.RemainingDays)},
}
return data
}
// displayPoints returns 4U points info in rows format.
func pointsInfo(v *we.LoyaltyPointsInfo) [][]string {
data := [][]string{
{"4U points", fmt.Sprintf("%v", v.LoyaltyPoints)},
}
return data
}
// handleError displays error and exists.
func handleError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// getInfo extracts and returns specific info in rows format.
// If which is not a valid option, return all info (profile, usage, days and points).
func getInfo(client *we.Client, which string, login bool) [][]string {
var c *we.CustomerInfo
var err error
var info [][]string
if login {
c, err = client.Login()
handleError(err)
}
switch which {
case profile:
info = profileInfo(c)
case usage:
u, err := client.Usage()
handleError(err)
info = usageInfo(u)
case days:
d, err := client.RemainingDays()
handleError(err)
info = daysInfo(d)
case points:
p, err := client.LoyaltyPoints()
handleError(err)
info = pointsInfo(p)
default:
info = profileInfo(c)
info = append(info, getInfo(client, usage, false)...)
info = append(info, getInfo(client, days, false)...)
info = append(info, getInfo(client, points, false)...)
}
return info
}