-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
62 lines (52 loc) · 1.35 KB
/
image.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
package dto
import (
"bytes"
"fmt"
"github.com/flowswiss/cli/pkg/flow"
"regexp"
"strings"
)
type Image struct {
*flow.Image
Locations []*flow.Location
}
func (i *Image) Columns() []string {
return []string{"id", "operating system", "version", "type", "availability", "license"}
}
func (i *Image) Values() map[string]interface{} {
licenseBuf := &bytes.Buffer{}
for idx, license := range i.RequiredLicenses {
licenseBuf.WriteString(fmt.Sprintf("%s (%s)", license.Name, getPricePerHour(license.Price)))
if idx+1 < len(i.RequiredLicenses) {
licenseBuf.WriteString(", ")
}
}
availabilityBuf := &bytes.Buffer{}
for idx, availability := range i.AvailableLocations {
found := false
for _, location := range i.Locations {
if location.Id == availability {
availabilityBuf.WriteString(location.Name)
found = true
break
}
}
if found && idx+1 < len(i.AvailableLocations) {
availabilityBuf.WriteString(", ")
}
}
t := regexp.MustCompile("(?:_|^)[a-z]").ReplaceAllStringFunc(i.Type, func(s string) string {
if s[0] == '_' {
s = " " + s[1:]
}
return strings.ToUpper(s)
})
return map[string]interface{}{
"id": i.Id,
"operating system": i.OperatingSystem,
"version": i.Version,
"type": t,
"availability": availabilityBuf.String(),
"license": licenseBuf.String(),
}
}