/
formatter.go
66 lines (57 loc) · 1.3 KB
/
formatter.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
package main
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/logrusorgru/aurora/v3"
"github.com/olekukonko/tablewriter"
"github.com/Scalingo/link/v2/api"
)
func formatIPs(ips []api.IP) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "IP", "Status", "CHECKS"})
for _, ip := range ips {
status := formatStatus(ip)
checks := "None"
if len(ip.Checks) > 0 {
var c []string
for _, check := range ip.IP.Checks {
c = append(c, fmt.Sprintf("%s - %s", check.Type, net.JoinHostPort(check.Host, strconv.Itoa(check.Port))))
}
checks = strings.Join(c, ",")
}
table.Append([]string{
ip.ID,
ip.IP.IP,
status,
checks,
})
}
table.Render()
}
func formatIP(ip api.IP) {
fmt.Printf("ID:\t%s\n", ip.ID)
fmt.Printf("Status:\t%s\n", formatStatus(ip))
if len(ip.Checks) == 0 {
fmt.Printf("Checks:\tNone\n")
} else {
fmt.Println("Checks:")
for _, check := range ip.Checks {
fmt.Printf(" - Type: %s, Host: %s, Port: %v\n", check.Type, check.Host, check.Port)
}
}
}
func formatStatus(ip api.IP) string {
switch ip.Status {
case api.Activated:
return aurora.Green("ACTIVATED").String()
case api.Standby:
return aurora.Yellow("STANDBY").String()
case api.Failing:
return aurora.Red("FAILING").String()
default:
return ip.Status
}
}