Skip to content

Commit 43335c6

Browse files
devin-ai-integration[bot]AmberAlstonsquizzi
authored
Fix: Make vm port ls and cluster port ls output single-spaced (#546)
* Fix: Make vm port ls and cluster port ls output single-spaced Co-Authored-By: amber@replicated.com <amberlalston@gmail.com> * bug: Fix port expose and port list commands hanging Co-Authored-By: amber@replicated.com <amberlalston@gmail.com> * bug: Fix template formatting and remove unused parameters Co-Authored-By: amber@replicated.com <amberlalston@gmail.com> * bug: Fix template formatting to remove double-spacing Co-Authored-By: amber@replicated.com <amberlalston@gmail.com> * bug: Fix template formatting to ensure proper column spacing Co-Authored-By: amber@replicated.com <amberlalston@gmail.com> * bug: Remove trailing % character from port listing output Co-Authored-By: amber@replicated.com <amberlalston@gmail.com> * Remove unused tabwriter.Writer var Signed-off-by: Kyle Squizzato <kyle@replicated.com> * Fix trailing % character in port ls Signed-off-by: Kyle Squizzato <kyle@replicated.com> --------- Signed-off-by: Kyle Squizzato <kyle@replicated.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: amber@replicated.com <amberlalston@gmail.com> Co-authored-by: Kyle Squizzato <kyle@replicated.com>
1 parent 71a4527 commit 43335c6

File tree

8 files changed

+43
-31
lines changed

8 files changed

+43
-31
lines changed

cli/cmd/cluster_port_expose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ func (r *runners) clusterPortExpose(_ *cobra.Command, args []string) error {
6464
return err
6565
}
6666

67-
return print.ClusterPort(r.outputFormat, r.w, port)
67+
return print.ClusterPort(r.outputFormat, port)
6868
}

cli/cmd/cluster_port_ls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ func (r *runners) clusterPortList(_ *cobra.Command, args []string) error {
4343
return err
4444
}
4545

46-
return print.ClusterPorts(r.outputFormat, r.w, ports, true)
46+
return print.ClusterPorts(r.outputFormat, ports, true)
4747
}

cli/cmd/cluster_port_rm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (r *runners) clusterPortRemove(_ *cobra.Command, args []string) error {
7171
return err
7272
}
7373

74-
return print.ClusterPorts(r.outputFormat, r.w, ports, true)
74+
return print.ClusterPorts(r.outputFormat, ports, true)
7575
}
7676

7777
if len(r.args.clusterPortRemoveProtocols) == 0 {
@@ -83,5 +83,5 @@ func (r *runners) clusterPortRemove(_ *cobra.Command, args []string) error {
8383
return err
8484
}
8585

86-
return print.ClusterPorts(r.outputFormat, r.w, ports, true)
86+
return print.ClusterPorts(r.outputFormat, ports, true)
8787
}

cli/cmd/vm_port_expose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ func (r *runners) vmPortExpose(_ *cobra.Command, args []string) error {
5959
return err
6060
}
6161

62-
return print.VMPort(r.outputFormat, r.w, port)
62+
return print.VMPort(r.outputFormat, port)
6363
}

cli/cmd/vm_port_ls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ func (r *runners) vmPortList(_ *cobra.Command, args []string) error {
4242
return err
4343
}
4444

45-
return print.VMPorts(r.outputFormat, r.w, ports, true)
45+
return print.VMPorts(r.outputFormat, ports, true)
4646
}

cli/cmd/vm_port_rm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ func (r *runners) vmPortRemove(_ *cobra.Command, args []string) error {
4949
return err
5050
}
5151

52-
return print.VMPorts(r.outputFormat, r.w, ports, true)
52+
return print.VMPorts(r.outputFormat, ports, true)
5353
}

cli/print/cluster_ports.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ import (
1010
"github.com/replicatedhq/replicated/pkg/types"
1111
)
1212

13-
var portsTmplHeaderSrc = `ID CLUSTER PORT PROTOCOL EXPOSED PORT WILDCARD STATUS`
14-
var portsTmplRowSrc = `{{- range . }}
13+
var (
14+
portsTmplHeaderSrc = `ID CLUSTER PORT PROTOCOL EXPOSED PORT WILDCARD STATUS`
15+
portsTmplRowSrc = `{{- range . }}
1516
{{- $id := .AddonID }}
1617
{{- $upstreamPort := .UpstreamPort }}
1718
{{- $hostname := .Hostname }}
1819
{{- $isWildcard := .IsWildcard }}
1920
{{- $state := .State }}
2021
{{- range .ExposedPorts }}
21-
{{ $id }} {{ $upstreamPort }} {{ .Protocol }} {{ formatURL .Protocol $hostname }} {{ $isWildcard }} {{ printf "%-12s" $state }}
22-
{{ end }}
23-
{{ end }}`
24-
var portsTmplSrc = fmt.Sprintln(portsTmplHeaderSrc) + portsTmplRowSrc
25-
var portsTmpl = template.Must(template.New("ports").Funcs(funcs).Parse(portsTmplSrc))
26-
var portsTmplNoHeader = template.Must(template.New("ports").Funcs(funcs).Parse(portsTmplRowSrc))
22+
{{ $id }} {{ $upstreamPort }} {{ .Protocol }} {{ formatURL .Protocol $hostname }} {{ $isWildcard }} {{ $state }}
23+
{{- end }}
24+
{{- end }}
25+
`
26+
)
27+
28+
var (
29+
portsTmplSrc = fmt.Sprint(portsTmplHeaderSrc) + "\n" + portsTmplRowSrc
30+
portsTmpl = template.Must(template.New("ports").Funcs(funcs).Parse(portsTmplSrc))
31+
portsTmplNoHeader = template.Must(template.New("ports").Funcs(funcs).Parse(portsTmplRowSrc))
32+
)
2733

2834
const (
2935
clusterPortsMinWidth = 16
@@ -32,7 +38,7 @@ const (
3238
clusterPortsPadChar = ' '
3339
)
3440

35-
func ClusterPorts(outputFormat string, w *tabwriter.Writer, ports []*types.ClusterPort, header bool) error {
41+
func ClusterPorts(outputFormat string, ports []*types.ClusterPort, header bool) error {
3642
// we need a custom tab writer here because our column widths are large
3743
portsWriter := tabwriter.NewWriter(os.Stdout, clusterPortsMinWidth, clusterPortsTabWidth, clusterPortsPadding, clusterPortsPadChar, tabwriter.TabIndent)
3844

@@ -58,10 +64,10 @@ func ClusterPorts(outputFormat string, w *tabwriter.Writer, ports []*types.Clust
5864
default:
5965
return fmt.Errorf("unsupported output format: %s", outputFormat)
6066
}
61-
return w.Flush()
67+
return portsWriter.Flush()
6268
}
6369

64-
func ClusterPort(outputFormat string, w *tabwriter.Writer, port *types.ClusterPort) error {
70+
func ClusterPort(outputFormat string, port *types.ClusterPort) error {
6571
// we need a custom tab writer here because our column widths are large
6672
portsWriter := tabwriter.NewWriter(os.Stdout, clusterPortsMinWidth, clusterPortsTabWidth, clusterPortsPadding, clusterPortsPadChar, tabwriter.TabIndent)
6773

@@ -81,5 +87,5 @@ func ClusterPort(outputFormat string, w *tabwriter.Writer, port *types.ClusterPo
8187
default:
8288
return fmt.Errorf("unsupported output format: %s", outputFormat)
8389
}
84-
return w.Flush()
90+
return portsWriter.Flush()
8591
}

cli/print/vm_ports.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@ import (
1010
"github.com/replicatedhq/replicated/pkg/types"
1111
)
1212

13-
var vmPortsTmplHeaderSrc = `ID VM PORT PROTOCOL EXPOSED PORT STATUS`
14-
var vmPortsTmplRowSrc = `{{- range . }}
13+
var (
14+
vmPortsTmplHeaderSrc = `ID VM PORT PROTOCOL EXPOSED PORT STATUS`
15+
vmPortsTmplRowSrc = `{{- range . }}
1516
{{- $id := .AddonID }}
1617
{{- $upstreamPort := .UpstreamPort }}
1718
{{- $hostname := .Hostname }}
1819
{{- $state := .State }}
1920
{{- range .ExposedPorts }}
20-
{{ $id }} {{ $upstreamPort }} {{ .Protocol }} {{ formatURL .Protocol $hostname }} {{ printf "%-12s" $state }}
21-
{{ end }}
22-
{{ end }}`
23-
var vmPortsTmplSrc = fmt.Sprintln(vmPortsTmplHeaderSrc) + vmPortsTmplRowSrc
24-
var vmPortsTmpl = template.Must(template.New("ports").Funcs(funcs).Parse(vmPortsTmplSrc))
25-
var vmPortsTmplNoHeader = template.Must(template.New("ports").Funcs(funcs).Parse(vmPortsTmplRowSrc))
21+
{{ $id }} {{ $upstreamPort }} {{ .Protocol }} {{ formatURL .Protocol $hostname }} {{ $state }}
22+
{{- end }}
23+
{{- end }}
24+
`
25+
)
26+
27+
var (
28+
vmPortsTmplSrc = fmt.Sprint(vmPortsTmplHeaderSrc) + "\n" + vmPortsTmplRowSrc
29+
vmPortsTmpl = template.Must(template.New("ports").Funcs(funcs).Parse(vmPortsTmplSrc))
30+
vmPortsTmplNoHeader = template.Must(template.New("ports").Funcs(funcs).Parse(vmPortsTmplRowSrc))
31+
)
2632

2733
const (
2834
vmPortsMinWidth = 16
@@ -31,7 +37,7 @@ const (
3137
vmPortsPadChar = ' '
3238
)
3339

34-
func VMPorts(outputFormat string, w *tabwriter.Writer, ports []*types.VMPort, header bool) error {
40+
func VMPorts(outputFormat string, ports []*types.VMPort, header bool) error {
3541
// we need a custom tab writer here because our column widths are large
3642
portsWriter := tabwriter.NewWriter(os.Stdout, vmPortsMinWidth, vmPortsTabWidth, vmPortsPadding, vmPortsPadChar, tabwriter.TabIndent)
3743

@@ -57,10 +63,10 @@ func VMPorts(outputFormat string, w *tabwriter.Writer, ports []*types.VMPort, he
5763
default:
5864
return fmt.Errorf("unsupported output format: %s", outputFormat)
5965
}
60-
return w.Flush()
66+
return portsWriter.Flush()
6167
}
6268

63-
func VMPort(outputFormat string, w *tabwriter.Writer, port *types.VMPort) error {
69+
func VMPort(outputFormat string, port *types.VMPort) error {
6470
// we need a custom tab writer here because our column widths are large
6571
portsWriter := tabwriter.NewWriter(os.Stdout, vmPortsMinWidth, vmPortsTabWidth, vmPortsPadding, vmPortsPadChar, tabwriter.TabIndent)
6672

@@ -80,5 +86,5 @@ func VMPort(outputFormat string, w *tabwriter.Writer, port *types.VMPort) error
8086
default:
8187
return fmt.Errorf("unsupported output format: %s", outputFormat)
8288
}
83-
return w.Flush()
89+
return portsWriter.Flush()
8490
}

0 commit comments

Comments
 (0)