/
channel_license_counts.go
60 lines (49 loc) · 1.61 KB
/
channel_license_counts.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
package print
import (
"fmt"
"text/tabwriter"
"text/template"
channels "github.com/replicatedhq/replicated/gen/go/v1"
)
var channelLicenseCountsTmplSrc = `
LICENSE_TYPE ACTIVE AIRGAP INACTIVE TOTAL
{{ range $licenseType, $counts := . -}}
{{ $licenseType }} {{ $counts.Active }} {{ $counts.Airgap }} {{ $counts.Inactive }} {{ $counts.Total }}
{{ end }}`
var channelLicenseCountsTmpl = template.Must(template.New("ChannelLicenseCounts").Parse(channelLicenseCountsTmplSrc))
type licenseTypeCounts struct {
Active, Airgap, Inactive, Total int64
}
func LicenseCounts(w *tabwriter.Writer, counts *channels.LicenseCounts) error {
countsByLicenseType := make(map[string]*licenseTypeCounts)
var getOrSetLicenseCounts = func(licenseType string) *licenseTypeCounts {
licenseCounts, ok := countsByLicenseType[licenseType]
if !ok {
licenseCounts = &licenseTypeCounts{}
countsByLicenseType[licenseType] = licenseCounts
}
return licenseCounts
}
for licenseType, count := range counts.Active {
getOrSetLicenseCounts(licenseType).Active = count
}
for licenseType, count := range counts.Airgap {
getOrSetLicenseCounts(licenseType).Airgap = count
}
for licenseType, count := range counts.Inactive {
getOrSetLicenseCounts(licenseType).Inactive = count
}
for licenseType, count := range counts.Total {
getOrSetLicenseCounts(licenseType).Total = count
}
if len(countsByLicenseType) == 0 {
if _, err := fmt.Fprintln(w, "No active licenses in channel"); err != nil {
return err
}
return w.Flush()
}
if err := channelLicenseCountsTmpl.Execute(w, countsByLicenseType); err != nil {
return err
}
return w.Flush()
}