|
| 1 | +package print |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "text/tabwriter" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + "github.com/replicatedhq/replicated/pkg/types" |
| 11 | +) |
| 12 | + |
| 13 | +var portsTmplHeaderSrc = `CLUSTER PORT PROTOCOL EXPOSED PORT STATUS` |
| 14 | +var portsTmplRowSrc = `{{- range . }} |
| 15 | +{{- $upstreamPort := .UpstreamPort }} |
| 16 | +{{- $hostname := .Hostname }} |
| 17 | +{{- $state := .State }} |
| 18 | +{{- range .ExposedPorts }} |
| 19 | +{{ $upstreamPort }} {{ .Protocol }} {{ formatURL .Protocol $hostname }} {{ printf "%-12s" $state }} |
| 20 | +{{ end }} |
| 21 | +{{ end }}` |
| 22 | +var portsTmplSrc = fmt.Sprintln(portsTmplHeaderSrc) + portsTmplRowSrc |
| 23 | +var portsTmpl = template.Must(template.New("ports").Funcs(funcs).Parse(portsTmplSrc)) |
| 24 | +var portsTmplNoHeader = template.Must(template.New("ports").Funcs(funcs).Parse(portsTmplRowSrc)) |
| 25 | + |
| 26 | +const ( |
| 27 | + clusterPortsMinWidth = 16 |
| 28 | + clusterPortsTabWidth = 8 |
| 29 | + clusterPortsPadding = 4 |
| 30 | + clusterPortsPadChar = ' ' |
| 31 | +) |
| 32 | + |
| 33 | +func ClusterPorts(outputFormat string, w *tabwriter.Writer, ports []*types.ClusterPort, header bool) error { |
| 34 | + // we need a custom tab writer here because our column widths are large |
| 35 | + portsWriter := tabwriter.NewWriter(os.Stdout, clusterPortsMinWidth, clusterPortsTabWidth, clusterPortsPadding, clusterPortsPadChar, tabwriter.TabIndent) |
| 36 | + |
| 37 | + switch outputFormat { |
| 38 | + case "table": |
| 39 | + if header { |
| 40 | + if err := portsTmpl.Execute(portsWriter, ports); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + } else { |
| 44 | + if err := portsTmplNoHeader.Execute(portsWriter, ports); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + } |
| 48 | + case "json": |
| 49 | + cAsByte, err := json.MarshalIndent(ports, "", " ") |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + if _, err := fmt.Fprintln(portsWriter, string(cAsByte)); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + default: |
| 57 | + return fmt.Errorf("unsupported output format: %s", outputFormat) |
| 58 | + } |
| 59 | + return w.Flush() |
| 60 | +} |
| 61 | + |
| 62 | +func ClusterPort(outputFormat string, w *tabwriter.Writer, port *types.ClusterPort) error { |
| 63 | + // we need a custom tab writer here because our column widths are large |
| 64 | + portsWriter := tabwriter.NewWriter(os.Stdout, clusterPortsMinWidth, clusterPortsTabWidth, clusterPortsPadding, clusterPortsPadChar, tabwriter.TabIndent) |
| 65 | + |
| 66 | + switch outputFormat { |
| 67 | + case "table": |
| 68 | + if err := portsTmpl.Execute(portsWriter, []*types.ClusterPort{port}); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + case "json": |
| 72 | + cAsByte, err := json.MarshalIndent(port, "", " ") |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + if _, err := fmt.Fprintln(portsWriter, string(cAsByte)); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + default: |
| 80 | + return fmt.Errorf("unsupported output format: %s", outputFormat) |
| 81 | + } |
| 82 | + return w.Flush() |
| 83 | +} |
0 commit comments