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

Commit 634ade5

Browse files
authored
Merge pull request #1609 from ndeloof/exec_exitcode
2 parents 962dc94 + 15eab93 commit 634ade5

File tree

11 files changed

+95
-32
lines changed

11 files changed

+95
-32
lines changed

aci/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ func (cs *aciComposeService) Remove(ctx context.Context, project *types.Project,
227227
return nil, errdefs.ErrNotImplemented
228228
}
229229

230-
func (cs *aciComposeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
231-
return errdefs.ErrNotImplemented
230+
func (cs *aciComposeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
231+
return 0, errdefs.ErrNotImplemented
232232
}
233233
func (cs *aciComposeService) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) {
234234
return nil, errdefs.ErrNotImplemented

api/client/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ func (c *composeService) Remove(ctx context.Context, project *types.Project, opt
9292
return nil, errdefs.ErrNotImplemented
9393
}
9494

95-
func (c *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
96-
return errdefs.ErrNotImplemented
95+
func (c *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
96+
return 0, errdefs.ErrNotImplemented
9797
}
9898

9999
func (c *composeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error {

api/compose/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type Service interface {
6161
// Remove executes the equivalent to a `compose rm`
6262
Remove(ctx context.Context, project *types.Project, options RemoveOptions) ([]string, error)
6363
// Exec executes a command in a running service container
64-
Exec(ctx context.Context, project *types.Project, opts RunOptions) error
64+
Exec(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
6565
// Pause executes the equivalent to a `compose pause`
6666
Pause(ctx context.Context, project string, options PauseOptions) error
6767
// UnPause executes the equivalent to a `compose unpause`

api/compose/delegator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (s *ServiceDelegator) Remove(ctx context.Context, project *types.Project, o
108108
}
109109

110110
//Exec implements Service interface
111-
func (s *ServiceDelegator) Exec(ctx context.Context, project *types.Project, options RunOptions) error {
111+
func (s *ServiceDelegator) Exec(ctx context.Context, project *types.Project, options RunOptions) (int, error) {
112112
return s.Delegate.Exec(ctx, project, options)
113113
}
114114

api/compose/noimpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ func (s NoImpl) Remove(ctx context.Context, project *types.Project, options Remo
108108
}
109109

110110
//Exec implements Service interface
111-
func (s NoImpl) Exec(ctx context.Context, project *types.Project, options RunOptions) error {
112-
return errdefs.ErrNotImplemented
111+
func (s NoImpl) Exec(ctx context.Context, project *types.Project, opts RunOptions) (int, error) {
112+
return 0, errdefs.ErrNotImplemented
113113
}
114114

115115
//Pause implements Service interface

cli/cmd/compose/exec.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323

2424
"github.com/containerd/console"
25+
"github.com/docker/cli/cli"
2526
"github.com/spf13/cobra"
2627

2728
"github.com/docker/compose-cli/api/compose"
@@ -108,5 +109,13 @@ func runExec(ctx context.Context, backend compose.Service, opts execOpts) error
108109
execOpts.Writer = con
109110
execOpts.Reader = con
110111
}
111-
return backend.Exec(ctx, project, execOpts)
112+
exitCode, err := backend.Exec(ctx, project, execOpts)
113+
if exitCode != 0 {
114+
errMsg := ""
115+
if err != nil {
116+
errMsg = err.Error()
117+
}
118+
return cli.StatusError{StatusCode: exitCode, Status: errMsg}
119+
}
120+
return err
112121
}

ecs/exec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ import (
2525
"github.com/docker/compose-cli/api/errdefs"
2626
)
2727

28-
func (b *ecsAPIService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
29-
return errdefs.ErrNotImplemented
28+
func (b *ecsAPIService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
29+
return 0, errdefs.ErrNotImplemented
3030
}

ecs/local/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ func (e ecsLocalSimulation) Remove(ctx context.Context, project *types.Project,
184184
return e.compose.Remove(ctx, project, options)
185185
}
186186

187-
func (e ecsLocalSimulation) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
188-
return errdefs.ErrNotImplemented
187+
func (e ecsLocalSimulation) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
188+
return 0, errdefs.ErrNotImplemented
189189
}
190190

191191
func (e ecsLocalSimulation) Pause(ctx context.Context, project string, options compose.PauseOptions) error {

kube/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ func (s *composeService) Remove(ctx context.Context, project *types.Project, opt
248248
}
249249

250250
// Exec executes a command in a running service container
251-
func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
252-
return errdefs.ErrNotImplemented
251+
func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
252+
return 0, errdefs.ErrNotImplemented
253253
}
254254

255255
func (s *composeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error {

local/compose/exec.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import (
2828
"github.com/docker/compose-cli/api/compose"
2929
)
3030

31-
func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
31+
func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
3232
service, err := project.GetService(opts.Service)
3333
if err != nil {
34-
return err
34+
return 0, err
3535
}
3636

3737
containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
@@ -42,10 +42,10 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
4242
),
4343
})
4444
if err != nil {
45-
return err
45+
return 0, err
4646
}
4747
if len(containers) < 1 {
48-
return fmt.Errorf("container %s not running", getContainerName(project.Name, service, opts.Index))
48+
return 0, fmt.Errorf("container %s not running", getContainerName(project.Name, service, opts.Index))
4949
}
5050
container := containers[0]
5151

@@ -63,11 +63,11 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
6363
AttachStderr: true,
6464
})
6565
if err != nil {
66-
return err
66+
return 0, err
6767
}
6868

6969
if opts.Detach {
70-
return s.apiClient.ContainerExecStart(ctx, exec.ID, apitypes.ExecStartCheck{
70+
return 0, s.apiClient.ContainerExecStart(ctx, exec.ID, apitypes.ExecStartCheck{
7171
Detach: true,
7272
Tty: opts.Tty,
7373
})
@@ -78,19 +78,19 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
7878
Tty: opts.Tty,
7979
})
8080
if err != nil {
81-
return err
81+
return 0, err
8282
}
8383
defer resp.Close()
8484

8585
if opts.Tty {
8686
s.monitorTTySize(ctx, exec.ID, s.apiClient.ContainerExecResize)
8787
if err != nil {
88-
return err
88+
return 0, err
8989
}
9090
}
9191

92-
readChannel := make(chan error, 10)
93-
writeChannel := make(chan error, 10)
92+
readChannel := make(chan error)
93+
writeChannel := make(chan error)
9494

9595
go func() {
9696
_, err := io.Copy(opts.Writer, resp.Reader)
@@ -102,12 +102,23 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
102102
writeChannel <- err
103103
}()
104104

105-
for {
106-
select {
107-
case err := <-readChannel:
108-
return err
109-
case err := <-writeChannel:
110-
return err
111-
}
105+
select {
106+
case err = <-readChannel:
107+
break
108+
case err = <-writeChannel:
109+
break
110+
}
111+
112+
if err != nil {
113+
return 0, err
114+
}
115+
return s.getExecExitStatus(ctx, exec.ID)
116+
}
117+
118+
func (s *composeService) getExecExitStatus(ctx context.Context, execID string) (int, error) {
119+
resp, err := s.apiClient.ContainerExecInspect(ctx, execID)
120+
if err != nil {
121+
return 0, err
112122
}
123+
return resp.ExitCode, nil
113124
}

0 commit comments

Comments
 (0)