-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathget.go
More file actions
133 lines (118 loc) · 3.42 KB
/
Copy pathget.go
File metadata and controls
133 lines (118 loc) · 3.42 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
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
package proxies
import (
"context"
"fmt"
"github.com/onkernel/cli/pkg/table"
"github.com/onkernel/cli/pkg/util"
"github.com/onkernel/kernel-go-sdk"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
func (p ProxyCmd) Get(ctx context.Context, in ProxyGetInput) error {
item, err := p.proxies.Get(ctx, in.ID)
if err != nil {
return util.CleanedUpSdkError{Err: err}
}
// Display proxy details
rows := pterm.TableData{{"Property", "Value"}}
rows = append(rows, []string{"ID", item.ID})
name := item.Name
if name == "" {
name = "-"
}
rows = append(rows, []string{"Name", name})
rows = append(rows, []string{"Type", string(item.Type)})
// Display protocol (default to https if not set)
protocol := string(item.Protocol)
if protocol == "" {
protocol = "https"
}
rows = append(rows, []string{"Protocol", protocol})
// Display type-specific config details
rows = append(rows, getProxyConfigRows(item)...)
// Display status with color
status := string(item.Status)
if status == "" {
status = "-"
} else if status == "available" {
status = pterm.Green(status)
} else if status == "unavailable" {
status = pterm.Red(status)
}
rows = append(rows, []string{"Status", status})
// Display last checked timestamp
lastChecked := util.FormatLocal(item.LastChecked)
rows = append(rows, []string{"Last Checked", lastChecked})
table.PrintTableNoPad(rows, true)
return nil
}
func getProxyConfigRows(proxy *kernel.ProxyGetResponse) [][]string {
var rows [][]string
config := &proxy.Config
switch proxy.Type {
case kernel.ProxyGetResponseTypeDatacenter, kernel.ProxyGetResponseTypeIsp:
if config.Country != "" {
rows = append(rows, []string{"Country", config.Country})
}
case kernel.ProxyGetResponseTypeResidential:
if config.Country != "" {
rows = append(rows, []string{"Country", config.Country})
}
if config.City != "" {
rows = append(rows, []string{"City", config.City})
}
if config.State != "" {
rows = append(rows, []string{"State", config.State})
}
if config.Zip != "" {
rows = append(rows, []string{"ZIP", config.Zip})
}
if config.Asn != "" {
rows = append(rows, []string{"ASN", config.Asn})
}
if config.Os != "" {
rows = append(rows, []string{"OS", config.Os})
}
case kernel.ProxyGetResponseTypeMobile:
if config.Country != "" {
rows = append(rows, []string{"Country", config.Country})
}
if config.City != "" {
rows = append(rows, []string{"City", config.City})
}
if config.State != "" {
rows = append(rows, []string{"State", config.State})
}
if config.Zip != "" {
rows = append(rows, []string{"ZIP", config.Zip})
}
if config.Asn != "" {
rows = append(rows, []string{"ASN", config.Asn})
}
if config.Carrier != "" {
rows = append(rows, []string{"Carrier", config.Carrier})
}
case kernel.ProxyGetResponseTypeCustom:
if config.Host != "" {
rows = append(rows, []string{"Host", config.Host})
}
if config.Port != 0 {
rows = append(rows, []string{"Port", fmt.Sprintf("%d", config.Port)})
}
if config.Username != "" {
rows = append(rows, []string{"Username", config.Username})
}
hasPassword := "No"
if config.HasPassword {
hasPassword = "Yes"
}
rows = append(rows, []string{"Has Password", hasPassword})
}
return rows
}
func runProxiesGet(cmd *cobra.Command, args []string) error {
client := util.GetKernelClient(cmd)
svc := client.Proxies
p := ProxyCmd{proxies: &svc}
return p.Get(cmd.Context(), ProxyGetInput{ID: args[0]})
}