Skip to content

Commit f010de9

Browse files
authored
chore(progress): add a suffixWriter to align deployment table (#1899)
Before: ![before](https://user-images.githubusercontent.com/879348/106534553-38324000-64a9-11eb-809f-954f6e5c33c0.png) After: ![after](https://user-images.githubusercontent.com/879348/106534565-3ec0b780-64a9-11eb-82de-75a2f677e6b7.png) You can see that the items under the deployment times have the columns aligned. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 4d99c32 commit f010de9

File tree

6 files changed

+83
-5
lines changed

6 files changed

+83
-5
lines changed

e2e/multi-svc-app/back-end/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o e2e-service ./
2020

2121
# To make our images smaller, use alpine and copy in the service binary.
2222
FROM alpine:latest
23+
# curl is needed for container healthchecks.
24+
RUN apk update
25+
RUN apk add curl
2326
# Install certs
2427
RUN apk --no-cache add ca-certificates
2528
# Copy the binary from the builder image

internal/pkg/stream/stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
const (
14-
streamerFetchIntervalDuration = 2 * time.Second // How long to wait until Fetch is called again for a Streamer.
14+
streamerFetchIntervalDuration = 3 * time.Second // How long to wait until Fetch is called again for a Streamer.
1515
)
1616

1717
// Streamer is the interface that groups methods to periodically retrieve events,

internal/pkg/term/progress/cloudformation.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ func (c *ecsServiceResourceComponent) Render(out io.Writer) (numLines int, err e
295295
deploymentRenderer = c.deploymentRenderer
296296
}
297297

298-
nl, err = deploymentRenderer.Render(buf)
298+
sw := &suffixWriter{
299+
buf: buf,
300+
suffix: []byte{'\t', '\t'}, // Add two columns to the deployment renderer so that it aligns with resources.
301+
}
302+
nl, err = deploymentRenderer.Render(sw)
299303
if err != nil {
300304
return 0, err
301305
}

internal/pkg/term/progress/cloudformation_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,7 @@ func TestEcsServiceResourceComponent_Render(t *testing.T) {
498498
// THEN
499499
require.Nil(t, err)
500500
require.Equal(t, 2, nl)
501-
require.Equal(t, `resource
502-
deployment
503-
`, buf.String())
501+
require.Equal(t, "resource\n"+
502+
"deployment\t\t\n", buf.String())
504503
})
505504
}

internal/pkg/term/progress/writer.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,25 @@ func NewTabbedFileWriter(fw FileWriter) *TabbedFileWriter {
4747
WriteFlusher: tabwriter.NewWriter(fw, minCellWidth, tabWidth, cellPaddingWidth, paddingChar, noAdditionalFormatting),
4848
}
4949
}
50+
51+
// suffixWriter is an io.Writer that adds the suffix before a new line character.
52+
type suffixWriter struct {
53+
buf io.Writer
54+
suffix []byte
55+
}
56+
57+
// Write adds a suffix before each new line character in p.
58+
func (w *suffixWriter) Write(p []byte) (n int, err error) {
59+
var withSuffix []byte
60+
for _, b := range p {
61+
suffix := []byte{b}
62+
if b == '\n' {
63+
suffix = append(w.suffix, b)
64+
}
65+
withSuffix = append(withSuffix, suffix...)
66+
}
67+
if _, err := w.buf.Write(withSuffix); err != nil {
68+
return 0, err
69+
}
70+
return len(p), nil
71+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package progress
5+
6+
import (
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestSuffixWriter_Write(t *testing.T) {
14+
testCases := map[string]struct {
15+
inText string
16+
suffix []byte
17+
18+
wantedText string
19+
}{
20+
"should wrap every new line with the suffix": {
21+
inText: `The AWS Copilot CLI is a tool for developers to build, release
22+
and operate production ready containerized applications
23+
on Amazon ECS and AWS Fargate.
24+
`,
25+
suffix: []byte{'\t', '\t'},
26+
27+
wantedText: "The AWS Copilot CLI is a tool for developers to build, release\t\t\n" +
28+
"and operate production ready containerized applications\t\t\n" +
29+
"on Amazon ECS and AWS Fargate.\t\t\n",
30+
},
31+
}
32+
33+
for name, tc := range testCases {
34+
t.Run(name, func(t *testing.T) {
35+
// GIVEN
36+
buf := new(strings.Builder)
37+
sw := &suffixWriter{
38+
buf: buf,
39+
suffix: tc.suffix,
40+
}
41+
42+
// WHEN
43+
_, err := sw.Write([]byte(tc.inText))
44+
45+
// THEN
46+
require.NoError(t, err)
47+
require.Equal(t, tc.wantedText, buf.String())
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)