Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit d9fe745

Browse files
committed
avoid use of channels in API for gRPC compatibility
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 752edcc commit d9fe745

File tree

11 files changed

+80
-21
lines changed

11 files changed

+80
-21
lines changed

api/compose/api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type CreateOptions struct {
6666
// StartOptions group options of the Start API
6767
type StartOptions struct {
6868
// Attach will attach to service containers and pipe stdout/stderr to channel
69-
Attach chan ContainerEvent
69+
Attach ContainerEventListener
7070
}
7171

7272
// UpOptions group options of the Up API
@@ -186,6 +186,9 @@ type LogConsumer interface {
186186
Status(service, container, msg string)
187187
}
188188

189+
// ContainerEventListener is a callback to process ContainerEvent from services
190+
type ContainerEventListener func(event ContainerEvent)
191+
189192
// ContainerEvent notify an event has been collected on Source container implementing Service
190193
type ContainerEvent struct {
191194
Type int

cli/cmd/compose/start.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func runStart(ctx context.Context, opts startOptions, services []string) error {
7070
queue: queue,
7171
}
7272
err = c.ComposeService().Start(ctx, project, compose.StartOptions{
73-
Attach: queue,
73+
Attach: func(event compose.ContainerEvent) {
74+
queue <- event
75+
},
7476
})
7577
if err != nil {
7678
return err

cli/cmd/compose/up.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/docker/compose-cli/api/compose"
2929
"github.com/docker/compose-cli/api/context/store"
3030
"github.com/docker/compose-cli/api/progress"
31+
"github.com/docker/compose-cli/cli/cmd"
3132
"github.com/docker/compose-cli/cli/formatter"
3233

3334
"github.com/compose-spec/compose-go/types"
@@ -178,14 +179,18 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
178179
}()
179180

180181
err = c.ComposeService().Start(ctx, project, compose.StartOptions{
181-
Attach: queue,
182+
Attach: func(event compose.ContainerEvent) {
183+
queue <- event
184+
},
182185
})
183186
if err != nil {
184187
return err
185188
}
186189

187-
_, err = printer.run(ctx, opts.cascadeStop, opts.exitCodeFrom, stopFunc)
188-
// FIXME os.Exit
190+
exitCode, err := printer.run(ctx, opts.cascadeStop, opts.exitCodeFrom, stopFunc)
191+
if exitCode != 0 {
192+
return cmd.ExitCodeError{ExitCode: exitCode}
193+
}
189194
return err
190195
}
191196

cli/cmd/exit.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import "strconv"
20+
21+
// ExitCodeError reports an exit code set by command.
22+
type ExitCodeError struct {
23+
ExitCode int
24+
}
25+
26+
func (e ExitCodeError) Error() string {
27+
return strconv.Itoa(e.ExitCode)
28+
}

cli/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ func main() {
170170
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", opts.LogLevel)
171171
os.Exit(1)
172172
}
173+
logrus.SetFormatter(&logrus.TextFormatter{
174+
DisableTimestamp: true,
175+
DisableLevelTruncation: true,
176+
})
173177
logrus.SetLevel(level)
174178
if opts.Debug {
175179
logrus.SetLevel(logrus.DebugLevel)
@@ -241,6 +245,11 @@ $ docker context create %s <name>`, cc.Type(), store.EcsContextType), ctype)
241245
}
242246

243247
func exit(ctx string, err error, ctype string) {
248+
if exit, ok := err.(cmd.ExitCodeError); ok {
249+
metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus)
250+
os.Exit(exit.ExitCode)
251+
}
252+
244253
metrics.Track(ctype, os.Args[1:], metrics.FailureStatus)
245254

246255
if errors.Is(err, errdefs.ErrLoginRequired) {

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2
485485
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
486486
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
487487
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
488+
github.com/fvbommel/sortorder v1.0.1 h1:dSnXLt4mJYH25uDDGa3biZNQsozaUWDSWeKJ0qqFfzE=
488489
github.com/fvbommel/sortorder v1.0.1/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
489490
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7 h1:LofdAjjjqCSXMwLGgOgnE+rdPuvX9DxCqaHwKy7i/ko=
490491
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=

local/compose/attach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/docker/docker/pkg/stdcopy"
3131
)
3232

33-
func (s *composeService) attach(ctx context.Context, project *types.Project, consumer chan compose.ContainerEvent) (Containers, error) {
33+
func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener) (Containers, error) {
3434
containers, err := s.getContainers(ctx, project)
3535
if err != nil {
3636
return nil, err
@@ -51,7 +51,7 @@ func (s *composeService) attach(ctx context.Context, project *types.Project, con
5151
return containers, nil
5252
}
5353

54-
func (s *composeService) attachContainer(ctx context.Context, container moby.Container, consumer chan compose.ContainerEvent, project *types.Project) error {
54+
func (s *composeService) attachContainer(ctx context.Context, container moby.Container, consumer compose.ContainerEventListener, project *types.Project) error {
5555
serviceName := container.Labels[serviceLabel]
5656
w := getWriter(serviceName, getContainerNameWithoutProject(container), consumer)
5757

local/compose/logs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
9090
type splitBuffer struct {
9191
service string
9292
container string
93-
consumer chan compose.ContainerEvent
93+
consumer compose.ContainerEventListener
9494
}
9595

9696
// getWriter creates a io.Writer that will actually split by line and format by LogConsumer
97-
func getWriter(service, container string, events chan compose.ContainerEvent) io.Writer {
97+
func getWriter(service, container string, events compose.ContainerEventListener) io.Writer {
9898
return splitBuffer{
9999
service: service,
100100
container: container,
@@ -106,12 +106,12 @@ func (s splitBuffer) Write(b []byte) (n int, err error) {
106106
split := bytes.Split(b, []byte{'\n'})
107107
for _, line := range split {
108108
if len(line) != 0 {
109-
s.consumer <- compose.ContainerEvent{
109+
s.consumer(compose.ContainerEvent{
110110
Type: compose.ContainerEventLog,
111111
Service: s.service,
112112
Source: s.container,
113113
Line: string(line),
114-
}
114+
})
115115
}
116116
}
117117
return len(b), nil

local/compose/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti
5353
statusC, errC := s.apiClient.ContainerWait(context.Background(), c.ID, container.WaitConditionNotRunning)
5454
select {
5555
case status := <-statusC:
56-
options.Attach <- compose.ContainerEvent{
56+
options.Attach(compose.ContainerEvent{
5757
Type: compose.ContainerEventExit,
5858
Source: getCanonicalContainerName(c),
5959
Service: c.Labels[serviceLabel],
6060
ExitCode: int(status.StatusCode),
61-
}
61+
})
6262
case err := <-errC:
6363
logrus.Warnf("Unexpected API error for %s : %s\n", getCanonicalContainerName(c), err.Error())
6464
}

local/e2e/compose/cascade_stop_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,21 @@ func TestCascadeStop(t *testing.T) {
2929

3030
const projectName = "compose-e2e-logs"
3131

32-
res := c.RunDockerCmd("compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--abort-on-container-exit")
33-
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
34-
res.Assert(t, icmd.Expected{Out: `/does_not_exist: No such file or directory`})
35-
res.Assert(t, icmd.Expected{Out: `should_fail_1 exited with code 1`})
36-
res.Assert(t, icmd.Expected{Out: `Aborting on container exit...`})
37-
// FIXME res.Assert(t, icmd.Expected{ExitCode: 1})
32+
t.Run("abort-on-container-exit", func(t *testing.T) {
33+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--abort-on-container-exit")
34+
res.Assert(t, icmd.Expected{Out: `/does_not_exist: No such file or directory`})
35+
res.Assert(t, icmd.Expected{Out: `should_fail_1 exited with code 1`})
36+
res.Assert(t, icmd.Expected{Out: `Aborting on container exit...`})
37+
res.Assert(t, icmd.Expected{Out: `ERROR 1`})
38+
res.Assert(t, icmd.Expected{ExitCode: 1})
39+
})
40+
41+
t.Run("exit-code-from", func(t *testing.T) {
42+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--exit-code-from=sleep")
43+
res.Assert(t, icmd.Expected{Out: `/does_not_exist: No such file or directory`})
44+
res.Assert(t, icmd.Expected{Out: `should_fail_1 exited with code 1`})
45+
res.Assert(t, icmd.Expected{Out: `Aborting on container exit...`})
46+
res.Assert(t, icmd.Expected{Out: `ERROR 143`})
47+
res.Assert(t, icmd.Expected{ExitCode: 143})
48+
})
3849
}

0 commit comments

Comments
 (0)