-
Notifications
You must be signed in to change notification settings - Fork 23
/
customers.go
68 lines (59 loc) · 2.04 KB
/
customers.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
package print
import (
"encoding/json"
"fmt"
"text/tabwriter"
"text/template"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/types"
)
var customersTmplSrc = `ID NAME CHANNELS EXPIRES TYPE
{{ range . -}}
{{ .ID }} {{ .Name }} {{range .Channels}} {{.Name}}{{end}} {{if not .Expires}}Never{{else}}{{.Expires}}{{end}} {{.Type}}
{{ end }}`
var customersTmpl = template.Must(template.New("channels").Parse(customersTmplSrc))
func Customers(outputFormat string, w *tabwriter.Writer, customers []types.Customer) error {
if outputFormat == "table" {
if err := customersTmpl.Execute(w, customers); err != nil {
return err
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(customers, "", " ")
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
}
return w.Flush()
}
func CustomersWithInstances(outputFormat string, w *tabwriter.Writer, customers []types.Customer) error {
// Define a new template for use in this function
var customersTmplSrc = `CUSTOMER NAME INSTANCE ID LAST ACTIVE VERSION
{{ range . -}}
{{ .Name }} {{range .Instances}}{{.InstanceId}} {{.LastActive}} {{range .VersionHistory}}{{.VersionLabel}}{{end}}{{end}}
{{ end }}`
var customersTmpl = template.Must(template.New("customers").Parse(customersTmplSrc))
if outputFormat == "table" {
if err := customersTmpl.Execute(w, customers); err != nil {
return err
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(customers, "", " ")
if _, err := w.Write(cAsByte); err != nil {
return err
}
}
return w.Flush()
}
func Customer(outputFormat string, w *tabwriter.Writer, customer *types.Customer) error {
if outputFormat == "table" {
if err := customersTmpl.Execute(w, []types.Customer{*customer}); err != nil {
return errors.Wrap(err, "execute template")
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(customer, "", " ")
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return errors.Wrap(err, "write json")
}
}
return errors.Wrap(w.Flush(), "flush")
}