Skip to content

Commit 7036cda

Browse files
authored
Merge pull request docker#9198 from ndeloof/dockerCli_stdout
composeService to use dockerCli's In/Out/Err streams
2 parents 540ad83 + 22b8c73 commit 7036cda

37 files changed

+475
-147
lines changed

cmd/compose/exec.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ package compose
1818

1919
import (
2020
"context"
21-
"fmt"
22-
"os"
2321

2422
"github.com/compose-spec/compose-go/types"
25-
"github.com/containerd/console"
2623
"github.com/docker/cli/cli"
2724
"github.com/docker/compose/v2/pkg/api"
2825
"github.com/docker/compose/v2/pkg/compose"
@@ -105,27 +102,8 @@ func runExec(ctx context.Context, backend api.Service, opts execOpts) error {
105102
Index: opts.index,
106103
Detach: opts.detach,
107104
WorkingDir: opts.workingDir,
108-
109-
Stdin: os.Stdin,
110-
Stdout: os.Stdout,
111-
Stderr: os.Stderr,
112105
}
113106

114-
if execOpts.Tty {
115-
con := console.Current()
116-
if err := con.SetRaw(); err != nil {
117-
return err
118-
}
119-
defer func() {
120-
if err := con.Reset(); err != nil {
121-
fmt.Println("Unable to close the console")
122-
}
123-
}()
124-
125-
execOpts.Stdin = con
126-
execOpts.Stdout = con
127-
execOpts.Stderr = con
128-
}
129107
exitCode, err := backend.Exec(ctx, projectName, execOpts)
130108
if exitCode != 0 {
131109
errMsg := ""

cmd/compose/run.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
2322
"strings"
2423

2524
cgo "github.com/compose-spec/compose-go/cli"
@@ -207,9 +206,6 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
207206
Command: opts.Command,
208207
Detach: opts.Detach,
209208
AutoRemove: opts.Remove,
210-
Stdin: os.Stdin,
211-
Stdout: os.Stdout,
212-
Stderr: os.Stderr,
213209
Tty: !opts.noTty,
214210
WorkingDir: opts.workdir,
215211
User: opts.user,

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func pluginMain() {
4646
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
4747
return err
4848
}
49-
lazyInit.WithService(compose.NewComposeService(dockerCli.Client(), dockerCli.ConfigFile()))
49+
lazyInit.WithService(compose.NewComposeService(dockerCli))
5050
if originalPreRun != nil {
5151
return originalPreRun(cmd, args)
5252
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
github.com/spf13/cobra v1.3.0
3333
github.com/spf13/pflag v1.0.5
3434
github.com/stretchr/testify v1.7.0
35+
github.com/theupdateframework/notary v0.6.1
3536
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
3637
gotest.tools v2.2.0+incompatible
3738
gotest.tools/v3 v3.1.0
@@ -94,7 +95,6 @@ require (
9495
github.com/qri-io/jsonpointer v0.1.0 // indirect
9596
github.com/qri-io/jsonschema v0.1.1 // indirect
9697
github.com/sergi/go-diff v1.1.0 // indirect
97-
github.com/theupdateframework/notary v0.6.1 // indirect
9898
github.com/tonistiigi/fsutil v0.0.0-20210818161904-4442383b5028 // indirect
9999
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirect
100100
github.com/tonistiigi/vt100 v0.0.0-20210615222946-8066bb97264f // indirect

pkg/api/api.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package api
1919
import (
2020
"context"
2121
"fmt"
22-
"io"
2322
"strings"
2423
"time"
2524

@@ -216,9 +215,6 @@ type RunOptions struct {
216215
Entrypoint []string
217216
Detach bool
218217
AutoRemove bool
219-
Stdin io.ReadCloser
220-
Stdout io.WriteCloser
221-
Stderr io.WriteCloser
222218
Tty bool
223219
WorkingDir string
224220
User string

pkg/compose/attach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
137137
func (s *composeService) getContainerStreams(ctx context.Context, container string) (io.WriteCloser, io.ReadCloser, error) {
138138
var stdout io.ReadCloser
139139
var stdin io.WriteCloser
140-
cnx, err := s.apiClient.ContainerAttach(ctx, container, moby.ContainerAttachOptions{
140+
cnx, err := s.apiClient().ContainerAttach(ctx, container, moby.ContainerAttachOptions{
141141
Stream: true,
142142
Stdin: true,
143143
Stdout: true,
@@ -151,7 +151,7 @@ func (s *composeService) getContainerStreams(ctx context.Context, container stri
151151
}
152152

153153
// Fallback to logs API
154-
logs, err := s.apiClient.ContainerLogs(ctx, container, moby.ContainerLogsOptions{
154+
logs, err := s.apiClient().ContainerLogs(ctx, container, moby.ContainerLogsOptions{
155155
ShowStdout: true,
156156
ShowStderr: true,
157157
Follow: true,

pkg/compose/build.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22-
"os"
2322
"path/filepath"
2423

2524
"github.com/compose-spec/compose-go/types"
@@ -193,7 +192,7 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
193192
}
194193

195194
func (s *composeService) serverInfo(ctx context.Context) (command.ServerInfo, error) {
196-
ping, err := s.apiClient.Ping(ctx)
195+
ping, err := s.apiClient().Ping(ctx)
197196
if err != nil {
198197
return command.ServerInfo{}, err
199198
}
@@ -258,7 +257,7 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
258257
NetworkMode: service.Build.Network,
259258
ExtraHosts: service.Build.ExtraHosts,
260259
Session: []session.Attachable{
261-
authprovider.NewDockerAuthProvider(os.Stderr),
260+
authprovider.NewDockerAuthProvider(s.stderr()),
262261
},
263262
}, nil
264263
}

pkg/compose/build_buildkit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929

3030
func (s *composeService) doBuildBuildkit(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
3131
const drivername = "default"
32-
d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, s.configFile, nil, nil, nil, nil, nil, project.WorkingDir)
32+
d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient(), s.configFile(), nil, nil, nil, nil, nil, project.WorkingDir)
3333
if err != nil {
3434
return nil, err
3535
}
@@ -48,7 +48,7 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, project *types.Pro
4848
w := xprogress.NewPrinter(progressCtx, os.Stdout, mode)
4949

5050
// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
51-
response, err := build.Build(ctx, driverInfo, opts, nil, filepath.Dir(s.configFile.Filename), w)
51+
response, err := build.Build(ctx, driverInfo, opts, nil, filepath.Dir(s.configFile().Filename), w)
5252
errW := w.Wait()
5353
if err == nil {
5454
err = errW

pkg/compose/build_classic.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
6969

7070
dockerfileName := options.Inputs.DockerfilePath
7171
specifiedContext := options.Inputs.ContextPath
72-
progBuff := os.Stdout
73-
buildBuff := os.Stdout
72+
progBuff := s.stdout()
73+
buildBuff := s.stdout()
7474
if options.ImageIDFile != "" {
7575
// Avoid leaving a stale file if we eventually fail
7676
if err := os.Remove(options.ImageIDFile); err != nil && !os.IsNotExist(err) {
@@ -155,7 +155,7 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
155155
body = progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
156156
}
157157

158-
configFile := s.configFile
158+
configFile := s.configFile()
159159
creds, err := configFile.GetAllCredentials()
160160
if err != nil {
161161
return "", err
@@ -171,7 +171,7 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
171171

172172
ctx, cancel := context.WithCancel(ctx)
173173
defer cancel()
174-
response, err := s.apiClient.ImageBuild(ctx, body, buildOptions)
174+
response, err := s.apiClient().ImageBuild(ctx, body, buildOptions)
175175
if err != nil {
176176
return "", err
177177
}
@@ -181,13 +181,13 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
181181
aux := func(msg jsonmessage.JSONMessage) {
182182
var result dockertypes.BuildResult
183183
if err := json.Unmarshal(*msg.Aux, &result); err != nil {
184-
fmt.Fprintf(os.Stderr, "Failed to parse aux message: %s", err)
184+
fmt.Fprintf(s.stderr(), "Failed to parse aux message: %s", err)
185185
} else {
186186
imageID = result.ID
187187
}
188188
}
189189

190-
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, progBuff.Fd(), true, aux)
190+
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, progBuff.FD(), true, aux)
191191
if err != nil {
192192
if jerr, ok := err.(*jsonmessage.JSONError); ok {
193193
// If no error code is set, default to 1
@@ -203,7 +203,7 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
203203
// daemon isn't running Windows.
204204
if response.OSType != "windows" && runtime.GOOS == "windows" {
205205
// if response.OSType != "windows" && runtime.GOOS == "windows" && !options.quiet {
206-
fmt.Fprintln(os.Stdout, "SECURITY WARNING: You are building a Docker "+
206+
fmt.Fprintln(s.stdout(), "SECURITY WARNING: You are building a Docker "+
207207
"image from Windows against a non-Windows Docker host. All files and "+
208208
"directories added to build context will have '-rwxr-xr-x' permissions. "+
209209
"It is recommended to double check and reset permissions for sensitive "+

pkg/compose/compose.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import (
2121
"context"
2222
"encoding/json"
2323
"fmt"
24+
"io"
2425
"strings"
2526

2627
"github.com/docker/compose/v2/pkg/api"
2728
"github.com/pkg/errors"
2829

2930
"github.com/compose-spec/compose-go/types"
31+
"github.com/docker/cli/cli/command"
3032
"github.com/docker/cli/cli/config/configfile"
33+
"github.com/docker/cli/cli/streams"
3134
moby "github.com/docker/docker/api/types"
3235
"github.com/docker/docker/client"
3336
"github.com/sanathkr/go-yaml"
@@ -37,19 +40,41 @@ import (
3740
var Separator = "-"
3841

3942
// NewComposeService create a local implementation of the compose.Service API
40-
func NewComposeService(apiClient client.APIClient, configFile *configfile.ConfigFile) api.Service {
43+
func NewComposeService(dockerCli command.Cli) api.Service {
4144
return &composeService{
42-
apiClient: apiClient,
43-
configFile: configFile,
45+
dockerCli: dockerCli,
4446
}
4547
}
4648

4749
type composeService struct {
48-
apiClient client.APIClient
49-
configFile *configfile.ConfigFile
50+
dockerCli command.Cli
51+
}
52+
53+
func (s *composeService) apiClient() client.APIClient {
54+
return s.dockerCli.Client()
55+
}
56+
57+
func (s *composeService) configFile() *configfile.ConfigFile {
58+
return s.dockerCli.ConfigFile()
59+
}
60+
61+
func (s *composeService) stdout() *streams.Out {
62+
return s.dockerCli.Out()
63+
}
64+
65+
func (s *composeService) stdin() *streams.In {
66+
return s.dockerCli.In()
67+
}
68+
69+
func (s *composeService) stderr() io.Writer {
70+
return s.dockerCli.Err()
5071
}
5172

5273
func getCanonicalContainerName(c moby.Container) string {
74+
if len(c.Names) == 0 {
75+
// corner case, sometime happens on removal. return short ID as a safeguard value
76+
return c.ID[:12]
77+
}
5378
// Names return container canonical name /foo + link aliases /linked_by/foo
5479
for _, name := range c.Names {
5580
if strings.LastIndex(name, "/") == 0 {

0 commit comments

Comments
 (0)