/
util.go
231 lines (211 loc) · 5.93 KB
/
util.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package cli
import (
"encoding/json"
"fmt"
"os"
"strings"
"text/template"
"time"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/hcloud/schema"
"github.com/spf13/cobra"
)
func yesno(b bool) string {
if b {
return "yes"
}
return "no"
}
func na(s string) string {
if s == "" {
return "-"
}
return s
}
func datetime(t time.Time) string {
return t.Local().Format(time.UnixDate)
}
func chainRunE(fns ...func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
for _, fn := range fns {
if fn == nil {
continue
}
if err := fn(cmd, args); err != nil {
return err
}
}
return nil
}
}
func exactlyOneSet(s string, ss ...string) bool {
set := s != ""
for _, s := range ss {
if set && s != "" {
return false
}
set = set || s != ""
}
return set
}
var outputDescription = `Output can be controlled with the -o flag. Use -o noheader to suppress the
table header. Displayed columns and their order can be set with
-o columns=%s (see available columns below).`
func listLongDescription(intro string, columns []string) string {
var colExample []string
if len(columns) > 2 {
colExample = columns[0:2]
} else {
colExample = columns
}
return fmt.Sprintf(
"%s\n\n%s\n\nColumns:\n - %s",
intro,
fmt.Sprintf(outputDescription, strings.Join(colExample, ",")),
strings.Join(columns, "\n - "),
)
}
func splitLabel(label string) []string {
return strings.SplitN(label, "=", 2)
}
func labelsToString(labels map[string]string) string {
var labelsString []string
for key, value := range labels {
if value == "" {
labelsString = append(labelsString, key)
} else {
labelsString = append(labelsString, fmt.Sprintf("%s=%s", key, value))
}
}
return strings.Join(labelsString, ", ")
}
func describeFormat(object interface{}, format string) error {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
t, err := template.New("").Parse(format)
if err != nil {
return err
}
return t.Execute(os.Stdout, object)
}
func describeJSON(object interface{}) error {
enc := json.NewEncoder(os.Stdout)
return enc.Encode(object)
}
func locationToSchema(location hcloud.Location) schema.Location {
return schema.Location{
ID: location.ID,
Name: location.Name,
Description: location.Description,
Country: location.Country,
City: location.City,
Latitude: location.Latitude,
Longitude: location.Longitude,
NetworkZone: string(location.NetworkZone),
}
}
func datacenterToSchema(datacenter hcloud.Datacenter) schema.Datacenter {
datacenterSchema := schema.Datacenter{
ID: datacenter.ID,
Name: datacenter.Name,
Description: datacenter.Description,
Location: locationToSchema(*datacenter.Location),
}
for _, st := range datacenter.ServerTypes.Supported {
datacenterSchema.ServerTypes.Supported = append(datacenterSchema.ServerTypes.Supported, st.ID)
}
for _, st := range datacenter.ServerTypes.Available {
datacenterSchema.ServerTypes.Available = append(datacenterSchema.ServerTypes.Available, st.ID)
}
return datacenterSchema
}
func serverTypeToSchema(serverType hcloud.ServerType) schema.ServerType {
serverTypeSchema := schema.ServerType{
ID: serverType.ID,
Name: serverType.Name,
Description: serverType.Description,
Cores: serverType.Cores,
Memory: serverType.Memory,
Disk: serverType.Disk,
StorageType: string(serverType.StorageType),
CPUType: string(serverType.CPUType),
}
for _, pricing := range serverType.Pricings {
serverTypeSchema.Prices = append(serverTypeSchema.Prices, schema.PricingServerTypePrice{
Location: pricing.Location.Name,
PriceHourly: schema.Price{
Net: pricing.Hourly.Net,
Gross: pricing.Hourly.Gross,
},
PriceMonthly: schema.Price{
Net: pricing.Monthly.Net,
Gross: pricing.Monthly.Gross,
},
})
}
return serverTypeSchema
}
func imageToSchema(image hcloud.Image) schema.Image {
imageSchema := schema.Image{
ID: image.ID,
Name: hcloud.String(image.Name),
Description: image.Description,
Status: string(image.Status),
Type: string(image.Type),
ImageSize: &image.ImageSize,
DiskSize: image.DiskSize,
Created: image.Created,
OSFlavor: image.OSFlavor,
OSVersion: hcloud.String(image.OSVersion),
RapidDeploy: image.RapidDeploy,
Protection: schema.ImageProtection{
Delete: image.Protection.Delete,
},
Deprecated: image.Deprecated,
Labels: image.Labels,
}
if image.CreatedFrom != nil {
imageSchema.CreatedFrom = &schema.ImageCreatedFrom{
ID: image.CreatedFrom.ID,
Name: image.CreatedFrom.Name,
}
}
if image.BoundTo != nil {
imageSchema.BoundTo = hcloud.Int(image.BoundTo.ID)
}
return imageSchema
}
func isoToSchema(iso hcloud.ISO) schema.ISO {
return schema.ISO{
ID: iso.ID,
Name: iso.Name,
Description: iso.Description,
Deprecated: iso.Deprecated,
}
}
func loadBalancerTypeToSchema(loadBalancerType hcloud.LoadBalancerType) schema.LoadBalancerType {
loadBalancerTypeSchema := schema.LoadBalancerType{
ID: loadBalancerType.ID,
Name: loadBalancerType.Name,
Description: loadBalancerType.Description,
MaxConnections: loadBalancerType.MaxConnections,
MaxServices: loadBalancerType.MaxServices,
MaxTargets: loadBalancerType.MaxTargets,
MaxAssignedCertificates: loadBalancerType.MaxAssignedCertificates,
}
for _, pricing := range loadBalancerType.Pricings {
loadBalancerTypeSchema.Prices = append(loadBalancerTypeSchema.Prices, schema.PricingLoadBalancerTypePrice{
Location: pricing.Location.Name,
PriceHourly: schema.Price{
Net: pricing.Hourly.Net,
Gross: pricing.Hourly.Gross,
},
PriceMonthly: schema.Price{
Net: pricing.Monthly.Net,
Gross: pricing.Monthly.Gross,
},
})
}
return loadBalancerTypeSchema
}