Skip to content

Commit fc38208

Browse files
authored
Merge branch 'v2' into writer_fix
2 parents acb7d9e + 24f8327 commit fc38208

39 files changed

+288
-222
lines changed

cmd/compose/build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
)
3535

3636
type buildOptions struct {
37-
*projectOptions
37+
*ProjectOptions
3838
composeOptions
3939
quiet bool
4040
pull bool
@@ -73,16 +73,16 @@ var printerModes = []string{
7373
buildx.PrinterModeQuiet,
7474
}
7575

76-
func buildCommand(p *projectOptions, backend api.Service) *cobra.Command {
76+
func buildCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
7777
opts := buildOptions{
78-
projectOptions: p,
78+
ProjectOptions: p,
7979
}
8080
cmd := &cobra.Command{
8181
Use: "build [OPTIONS] [SERVICE...]",
8282
Short: "Build or rebuild services",
8383
PreRunE: Adapt(func(ctx context.Context, args []string) error {
8484
if opts.memory != "" {
85-
fmt.Println("WARNING --memory is ignored as not supported in buildkit.")
85+
fmt.Fprintln(streams.Err(), "WARNING --memory is ignored as not supported in buildkit.")
8686
}
8787
if opts.quiet {
8888
opts.progress = buildx.PrinterModeQuiet
@@ -129,7 +129,7 @@ func buildCommand(p *projectOptions, backend api.Service) *cobra.Command {
129129
}
130130

131131
func runBuild(ctx context.Context, backend api.Service, opts buildOptions, services []string) error {
132-
project, err := opts.toProject(services, cli.WithResolvedPaths(true))
132+
project, err := opts.ToProject(services, cli.WithResolvedPaths(true))
133133
if err != nil {
134134
return err
135135
}

cmd/compose/completion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ func noCompletion() validArgsFn {
3232
}
3333
}
3434

35-
func completeServiceNames(p *projectOptions) validArgsFn {
35+
func completeServiceNames(p *ProjectOptions) validArgsFn {
3636
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
37-
project, err := p.toProject(nil)
37+
project, err := p.ToProject(nil)
3838
if err != nil {
3939
return nil, cobra.ShellCompDirectiveNoFileComp
4040
}

cmd/compose/compose.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/docker/buildx/util/logutil"
3232
dockercli "github.com/docker/cli/cli"
3333
"github.com/docker/cli/cli-plugins/manager"
34-
"github.com/docker/cli/cli/command"
3534
"github.com/morikuni/aec"
3635
"github.com/pkg/errors"
3736
"github.com/sirupsen/logrus"
@@ -91,7 +90,7 @@ func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
9190
})
9291
}
9392

94-
type projectOptions struct {
93+
type ProjectOptions struct {
9594
ProjectName string
9695
Profiles []string
9796
ConfigPaths []string
@@ -108,16 +107,16 @@ type ProjectFunc func(ctx context.Context, project *types.Project) error
108107
type ProjectServicesFunc func(ctx context.Context, project *types.Project, services []string) error
109108

110109
// WithProject creates a cobra run command from a ProjectFunc based on configured project options and selected services
111-
func (o *projectOptions) WithProject(fn ProjectFunc) func(cmd *cobra.Command, args []string) error {
110+
func (o *ProjectOptions) WithProject(fn ProjectFunc) func(cmd *cobra.Command, args []string) error {
112111
return o.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
113112
return fn(ctx, project)
114113
})
115114
}
116115

117116
// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
118-
func (o *projectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Command, args []string) error {
117+
func (o *ProjectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Command, args []string) error {
119118
return Adapt(func(ctx context.Context, args []string) error {
120-
project, err := o.toProject(args, cli.WithResolvedPaths(true))
119+
project, err := o.ToProject(args, cli.WithResolvedPaths(true))
121120
if err != nil {
122121
return err
123122
}
@@ -126,7 +125,7 @@ func (o *projectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Co
126125
})
127126
}
128127

129-
func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
128+
func (o *ProjectOptions) addProjectFlags(f *pflag.FlagSet) {
130129
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
131130
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
132131
f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
@@ -137,11 +136,11 @@ func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
137136
_ = f.MarkHidden("workdir")
138137
}
139138

140-
func (o *projectOptions) projectOrName(services ...string) (*types.Project, string, error) {
139+
func (o *ProjectOptions) projectOrName(services ...string) (*types.Project, string, error) {
141140
name := o.ProjectName
142141
var project *types.Project
143142
if len(o.ConfigPaths) > 0 || o.ProjectName == "" {
144-
p, err := o.toProject(services)
143+
p, err := o.ToProject(services)
145144
if err != nil {
146145
envProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
147146
if envProjectName != "" {
@@ -155,7 +154,7 @@ func (o *projectOptions) projectOrName(services ...string) (*types.Project, stri
155154
return project, name, nil
156155
}
157156

158-
func (o *projectOptions) toProjectName() (string, error) {
157+
func (o *ProjectOptions) toProjectName() (string, error) {
159158
if o.ProjectName != "" {
160159
return o.ProjectName, nil
161160
}
@@ -165,14 +164,14 @@ func (o *projectOptions) toProjectName() (string, error) {
165164
return envProjectName, nil
166165
}
167166

168-
project, err := o.toProject(nil)
167+
project, err := o.ToProject(nil)
169168
if err != nil {
170169
return "", err
171170
}
172171
return project.Name, nil
173172
}
174173

175-
func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
174+
func (o *ProjectOptions) ToProject(services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
176175
options, err := o.toProjectOptions(po...)
177176
if err != nil {
178177
return nil, compose.WrapComposeError(err)
@@ -222,7 +221,7 @@ func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn
222221
return project, err
223222
}
224223

225-
func (o *projectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.ProjectOptions, error) {
224+
func (o *ProjectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.ProjectOptions, error) {
226225
return cli.NewProjectOptions(o.ConfigPaths,
227226
append(po,
228227
cli.WithWorkingDirectory(o.ProjectDir),
@@ -243,7 +242,7 @@ func RunningAsStandalone() bool {
243242
}
244243

245244
// RootCommand returns the compose command with its child commands
246-
func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //nolint:gocyclo
245+
func RootCommand(streams api.Streams, backend api.Service) *cobra.Command { //nolint:gocyclo
247246
// filter out useless commandConn.CloseWrite warning message that can occur
248247
// when using a remote context that is unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
249248
// https://github.com/docker/cli/blob/e1f24d3c93df6752d3c27c8d61d18260f141310c/cli/connhelper/commandconn/commandconn.go#L203-L215
@@ -254,7 +253,7 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
254253
"commandConn.CloseRead:",
255254
))
256255

257-
opts := projectOptions{}
256+
opts := ProjectOptions{}
258257
var (
259258
ansi string
260259
noAnsi bool
@@ -305,7 +304,7 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
305304
if verbose {
306305
logrus.SetLevel(logrus.TraceLevel)
307306
}
308-
formatter.SetANSIMode(ansi)
307+
formatter.SetANSIMode(streams, ansi)
309308
switch ansi {
310309
case "never":
311310
progress.Mode = progress.ModePlain
@@ -333,27 +332,27 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
333332
}
334333

335334
c.AddCommand(
336-
upCommand(&opts, backend),
335+
upCommand(&opts, streams, backend),
337336
downCommand(&opts, backend),
338337
startCommand(&opts, backend),
339338
restartCommand(&opts, backend),
340339
stopCommand(&opts, backend),
341-
psCommand(&opts, backend),
342-
listCommand(backend),
343-
logsCommand(&opts, backend),
344-
convertCommand(&opts, backend),
340+
psCommand(&opts, streams, backend),
341+
listCommand(streams, backend),
342+
logsCommand(&opts, streams, backend),
343+
convertCommand(&opts, streams, backend),
345344
killCommand(&opts, backend),
346-
runCommand(&opts, dockerCli, backend),
345+
runCommand(&opts, streams, backend),
347346
removeCommand(&opts, backend),
348-
execCommand(&opts, dockerCli, backend),
347+
execCommand(&opts, streams, backend),
349348
pauseCommand(&opts, backend),
350349
unpauseCommand(&opts, backend),
351-
topCommand(&opts, backend),
352-
eventsCommand(&opts, backend),
353-
portCommand(&opts, backend),
354-
imagesCommand(&opts, backend),
350+
topCommand(&opts, streams, backend),
351+
eventsCommand(&opts, streams, backend),
352+
portCommand(&opts, streams, backend),
353+
imagesCommand(&opts, streams, backend),
355354
versionCommand(),
356-
buildCommand(&opts, backend),
355+
buildCommand(&opts, streams, backend),
357356
pushCommand(&opts, backend),
358357
pullCommand(&opts, backend),
359358
createCommand(&opts, backend),
@@ -383,7 +382,7 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
383382
return c
384383
}
385384

386-
func setEnvWithDotEnv(prjOpts *projectOptions) error {
385+
func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
387386
options, err := prjOpts.toProjectOptions()
388387
if err != nil {
389388
return compose.WrapComposeError(err)

cmd/compose/convert.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
type convertOptions struct {
38-
*projectOptions
38+
*ProjectOptions
3939
Format string
4040
Output string
4141
quiet bool
@@ -50,9 +50,9 @@ type convertOptions struct {
5050
noConsistency bool
5151
}
5252

53-
func convertCommand(p *projectOptions, backend api.Service) *cobra.Command {
53+
func convertCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
5454
opts := convertOptions{
55-
projectOptions: p,
55+
ProjectOptions: p,
5656
}
5757
cmd := &cobra.Command{
5858
Aliases: []string{"config"},
@@ -73,22 +73,22 @@ func convertCommand(p *projectOptions, backend api.Service) *cobra.Command {
7373
}),
7474
RunE: Adapt(func(ctx context.Context, args []string) error {
7575
if opts.services {
76-
return runServices(opts)
76+
return runServices(streams, opts)
7777
}
7878
if opts.volumes {
79-
return runVolumes(opts)
79+
return runVolumes(streams, opts)
8080
}
8181
if opts.hash != "" {
82-
return runHash(opts)
82+
return runHash(streams, opts)
8383
}
8484
if opts.profiles {
85-
return runProfiles(opts, args)
85+
return runProfiles(streams, opts, args)
8686
}
8787
if opts.images {
88-
return runConfigImages(opts, args)
88+
return runConfigImages(streams, opts, args)
8989
}
9090

91-
return runConvert(ctx, backend, opts, args)
91+
return runConvert(ctx, streams, backend, opts, args)
9292
}),
9393
ValidArgsFunction: completeServiceNames(p),
9494
}
@@ -110,15 +110,14 @@ func convertCommand(p *projectOptions, backend api.Service) *cobra.Command {
110110
return cmd
111111
}
112112

113-
func runConvert(ctx context.Context, backend api.Service, opts convertOptions, services []string) error {
113+
func runConvert(ctx context.Context, streams api.Streams, backend api.Service, opts convertOptions, services []string) error {
114114
var content []byte
115-
project, err := opts.toProject(services,
115+
project, err := opts.ToProject(services,
116116
cli.WithInterpolation(!opts.noInterpolate),
117117
cli.WithResolvedPaths(true),
118118
cli.WithNormalization(!opts.noNormalize),
119119
cli.WithConsistency(!opts.noConsistency),
120120
cli.WithDiscardEnvFile)
121-
122121
if err != nil {
123122
return err
124123
}
@@ -140,7 +139,7 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s
140139
return nil
141140
}
142141

143-
var out io.Writer = os.Stdout
142+
var out io.Writer = streams.Out()
144143
if opts.Output != "" && len(content) > 0 {
145144
file, err := os.Create(opts.Output)
146145
if err != nil {
@@ -152,34 +151,34 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s
152151
return err
153152
}
154153

155-
func runServices(opts convertOptions) error {
156-
project, err := opts.toProject(nil)
154+
func runServices(streams api.Streams, opts convertOptions) error {
155+
project, err := opts.ToProject(nil)
157156
if err != nil {
158157
return err
159158
}
160159
return project.WithServices(project.ServiceNames(), func(s types.ServiceConfig) error {
161-
fmt.Println(s.Name)
160+
fmt.Fprintln(streams.Out(), s.Name)
162161
return nil
163162
})
164163
}
165164

166-
func runVolumes(opts convertOptions) error {
167-
project, err := opts.toProject(nil)
165+
func runVolumes(streams api.Streams, opts convertOptions) error {
166+
project, err := opts.ToProject(nil)
168167
if err != nil {
169168
return err
170169
}
171170
for n := range project.Volumes {
172-
fmt.Println(n)
171+
fmt.Fprintln(streams.Out(), n)
173172
}
174173
return nil
175174
}
176175

177-
func runHash(opts convertOptions) error {
176+
func runHash(streams api.Streams, opts convertOptions) error {
178177
var services []string
179178
if opts.hash != "*" {
180179
services = append(services, strings.Split(opts.hash, ",")...)
181180
}
182-
project, err := opts.toProject(services)
181+
project, err := opts.ToProject(services)
183182
if err != nil {
184183
return err
185184
}
@@ -188,14 +187,14 @@ func runHash(opts convertOptions) error {
188187
if err != nil {
189188
return err
190189
}
191-
fmt.Printf("%s %s\n", s.Name, hash)
190+
fmt.Fprintf(streams.Out(), "%s %s\n", s.Name, hash)
192191
}
193192
return nil
194193
}
195194

196-
func runProfiles(opts convertOptions, services []string) error {
195+
func runProfiles(streams api.Streams, opts convertOptions, services []string) error {
197196
set := map[string]struct{}{}
198-
project, err := opts.toProject(services)
197+
project, err := opts.ToProject(services)
199198
if err != nil {
200199
return err
201200
}
@@ -210,21 +209,21 @@ func runProfiles(opts convertOptions, services []string) error {
210209
}
211210
sort.Strings(profiles)
212211
for _, p := range profiles {
213-
fmt.Println(p)
212+
fmt.Fprintln(streams.Out(), p)
214213
}
215214
return nil
216215
}
217216

218-
func runConfigImages(opts convertOptions, services []string) error {
219-
project, err := opts.toProject(services)
217+
func runConfigImages(streams api.Streams, opts convertOptions, services []string) error {
218+
project, err := opts.ToProject(services)
220219
if err != nil {
221220
return err
222221
}
223222
for _, s := range project.Services {
224223
if s.Image != "" {
225-
fmt.Println(s.Image)
224+
fmt.Fprintln(streams.Out(), s.Image)
226225
} else {
227-
fmt.Printf("%s%s%s\n", project.Name, api.Separator, s.Name)
226+
fmt.Fprintf(streams.Out(), "%s%s%s\n", project.Name, api.Separator, s.Name)
228227
}
229228
}
230229
return nil

0 commit comments

Comments
 (0)