-
Notifications
You must be signed in to change notification settings - Fork 23
/
channel_adoption.go
79 lines (66 loc) · 2.13 KB
/
channel_adoption.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
package print
import (
"fmt"
"text/tabwriter"
"text/template"
channels "github.com/replicatedhq/replicated/gen/go/v1"
)
var channelAdoptionTmplSrc = `
LICENSE_TYPE CURRENT PREVIOUS OTHER
ACTIVE/ALL ACTIVE/ALL ACTIVE/ALL
{{ range $licenseType, $counts := . -}}
{{ $licenseType }} {{ $counts.Current.Active }}/{{ $counts.Current.All }} {{ $counts.Previous.Active }}/{{ $counts.Previous.All }} {{ $counts.Other.Active }}/{{ $counts.Other.All }}
{{ end }}
`
var channelAdoptionTmpl = template.Must(template.New("ChannelAdoption").Parse(channelAdoptionTmplSrc))
type allActiveCounts struct {
All int64
Active int64
}
type licenseAdoption struct {
Current allActiveCounts
Previous allActiveCounts
Other allActiveCounts
}
func ChannelAdoption(w *tabwriter.Writer, adoption *channels.ChannelAdoption) error {
countsByLicense := make(map[string]*licenseAdoption)
var getOrSetLicenseAdoption = func(licenseType string) *licenseAdoption {
la, ok := countsByLicense[licenseType]
if !ok {
la = &licenseAdoption{}
countsByLicense[licenseType] = la
}
return la
}
// current
for licenseType, count := range adoption.CurrentVersionCountActive {
getOrSetLicenseAdoption(licenseType).Current.Active = count
}
for licenseType, count := range adoption.CurrentVersionCountAll {
getOrSetLicenseAdoption(licenseType).Current.All = count
}
// previous
for licenseType, count := range adoption.PreviousVersionCountActive {
getOrSetLicenseAdoption(licenseType).Previous.Active = count
}
for licenseType, count := range adoption.PreviousVersionCountAll {
getOrSetLicenseAdoption(licenseType).Previous.All = count
}
// other
for licenseType, count := range adoption.OtherVersionCountActive {
getOrSetLicenseAdoption(licenseType).Other.Active = count
}
for licenseType, count := range adoption.OtherVersionCountAll {
getOrSetLicenseAdoption(licenseType).Other.All = count
}
if len(countsByLicense) == 0 {
if _, err := fmt.Fprintln(w, "No active licenses in channel"); err != nil {
return err
}
return w.Flush()
}
if err := channelAdoptionTmpl.Execute(w, countsByLicense); err != nil {
return err
}
return w.Flush()
}